Skip to content

Commit

Permalink
Implement PGH002 - deprecated use of logging.warn
Browse files Browse the repository at this point in the history
  • Loading branch information
squiddy committed Dec 15, 2022
1 parent 1ea2e93 commit 2015d87
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,7 @@ For more, see [pygrep-hooks](https://github.com/pre-commit/pygrep-hooks) on GitH
| Code | Name | Message | Fix |
| ---- | ---- | ------- | --- |
| PGH001 | NoEval | No builtin `eval()` allowed | |
| PGH002 | DeprecatedLogWarn | `warn` is deprecated in favor of `warning` | |

### Pylint (PLC, PLE, PLR, PLW)

Expand Down
14 changes: 14 additions & 0 deletions resources/test/fixtures/pygrep-hooks/PGH002_0.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import logging
import warnings
from warnings import warn

warnings.warn('this is ok')
warn("by itself is also ok")
logging.warning("this is fine")
log.warning("this is ok")

def foo():
from logging import warn
def warn(): pass

warn("this is also ok, warn has been redefined")
6 changes: 6 additions & 0 deletions resources/test/fixtures/pygrep-hooks/PGH002_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import logging
from logging import warn

logging.warn("this is not ok")
log.warn("this is also not ok")
warn('not ok')
3 changes: 3 additions & 0 deletions src/check_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1859,6 +1859,9 @@ where
if self.settings.enabled.contains(&CheckCode::PGH001) {
pygrep_hooks::checks::no_eval(self, func);
}
if self.settings.enabled.contains(&CheckCode::PGH002) {
pygrep_hooks::plugins::no_log_warn(self, func);
}

// pylint
if self.settings.enabled.contains(&CheckCode::PLC3002) {
Expand Down
8 changes: 8 additions & 0 deletions src/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ pub enum CheckCode {
RUF100,
// pygrep-hooks
PGH001,
PGH002,
}

#[derive(EnumIter, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -853,6 +854,7 @@ pub enum CheckKind {
BooleanPositionalValueInFunctionCall,
// pygrep-hooks
NoEval,
DeprecatedLogWarn,
// flake8-unused-arguments
UnusedFunctionArgument(String),
UnusedMethodArgument(String),
Expand Down Expand Up @@ -1209,6 +1211,7 @@ impl CheckCode {
CheckCode::FBT003 => CheckKind::BooleanPositionalValueInFunctionCall,
// pygrep-hooks
CheckCode::PGH001 => CheckKind::NoEval,
CheckCode::PGH002 => CheckKind::DeprecatedLogWarn,
// flake8-unused-arguments
CheckCode::ARG001 => CheckKind::UnusedFunctionArgument("...".to_string()),
CheckCode::ARG002 => CheckKind::UnusedMethodArgument("...".to_string()),
Expand Down Expand Up @@ -1421,6 +1424,7 @@ impl CheckCode {
CheckCode::N817 => CheckCategory::PEP8Naming,
CheckCode::N818 => CheckCategory::PEP8Naming,
CheckCode::PGH001 => CheckCategory::PygrepHooks,
CheckCode::PGH002 => CheckCategory::PygrepHooks,
CheckCode::PLC0414 => CheckCategory::Pylint,
CheckCode::PLC2201 => CheckCategory::Pylint,
CheckCode::PLC3002 => CheckCategory::Pylint,
Expand Down Expand Up @@ -1762,6 +1766,7 @@ impl CheckKind {
CheckKind::BooleanPositionalValueInFunctionCall => &CheckCode::FBT003,
// pygrep-hooks
CheckKind::NoEval => &CheckCode::PGH001,
CheckKind::DeprecatedLogWarn => &CheckCode::PGH002,
// flake8-unused-arguments
CheckKind::UnusedFunctionArgument(..) => &CheckCode::ARG001,
CheckKind::UnusedMethodArgument(..) => &CheckCode::ARG002,
Expand Down Expand Up @@ -2581,6 +2586,9 @@ impl CheckKind {
}
// pygrep-hooks
CheckKind::NoEval => "No builtin `eval()` allowed".to_string(),
CheckKind::DeprecatedLogWarn => {
"`warn` is deprecated in favor of `warning`".to_string()
}
// flake8-unused-arguments
CheckKind::UnusedFunctionArgument(name) => {
format!("Unused function argument: `{name}`")
Expand Down
9 changes: 6 additions & 3 deletions src/checks_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ pub enum CheckCodePrefix {
PGH0,
PGH00,
PGH001,
PGH002,
PLC,
PLC0,
PLC04,
Expand Down Expand Up @@ -1336,10 +1337,11 @@ impl CheckCodePrefix {
CheckCodePrefix::N816 => vec![CheckCode::N816],
CheckCodePrefix::N817 => vec![CheckCode::N817],
CheckCodePrefix::N818 => vec![CheckCode::N818],
CheckCodePrefix::PGH => vec![CheckCode::PGH001],
CheckCodePrefix::PGH0 => vec![CheckCode::PGH001],
CheckCodePrefix::PGH00 => vec![CheckCode::PGH001],
CheckCodePrefix::PGH => vec![CheckCode::PGH001, CheckCode::PGH002],
CheckCodePrefix::PGH0 => vec![CheckCode::PGH001, CheckCode::PGH002],
CheckCodePrefix::PGH00 => vec![CheckCode::PGH001, CheckCode::PGH002],
CheckCodePrefix::PGH001 => vec![CheckCode::PGH001],
CheckCodePrefix::PGH002 => vec![CheckCode::PGH002],
CheckCodePrefix::PLC => {
vec![CheckCode::PLC0414, CheckCode::PLC2201, CheckCode::PLC3002]
}
Expand Down Expand Up @@ -2130,6 +2132,7 @@ impl CheckCodePrefix {
CheckCodePrefix::PGH0 => SuffixLength::One,
CheckCodePrefix::PGH00 => SuffixLength::Two,
CheckCodePrefix::PGH001 => SuffixLength::Three,
CheckCodePrefix::PGH002 => SuffixLength::Three,
CheckCodePrefix::PLC => SuffixLength::Zero,
CheckCodePrefix::PLC0 => SuffixLength::One,
CheckCodePrefix::PLC04 => SuffixLength::Two,
Expand Down
3 changes: 3 additions & 0 deletions src/pygrep_hooks/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod checks;
pub mod plugins;

#[cfg(test)]
mod tests {
Expand All @@ -14,6 +15,8 @@ mod tests {

#[test_case(CheckCode::PGH001, Path::new("PGH001_0.py"); "PGH001_0")]
#[test_case(CheckCode::PGH001, Path::new("PGH001_1.py"); "PGH001_1")]
#[test_case(CheckCode::PGH002, Path::new("PGH002_0.py"); "PGH002_0")]
#[test_case(CheckCode::PGH002, Path::new("PGH002_1.py"); "PGH002_1")]
fn checks(check_code: CheckCode, path: &Path) -> Result<()> {
let snapshot = format!("{}_{}", check_code.as_ref(), path.to_string_lossy());
let mut checks = test_path(
Expand Down
54 changes: 54 additions & 0 deletions src/pygrep_hooks/plugins.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use rustpython_ast::{Expr, ExprKind};

use crate::ast::types::{BindingKind, Range};
use crate::check_ast::Checker;
use crate::checks::{Check, CheckKind};

/// PGH002 - deprecated use of logging.warn
pub fn no_log_warn(checker: &mut Checker, func: &Expr) {
match &func.node {
ExprKind::Attribute { attr, value, .. } => {
let ExprKind::Name { id, .. } = &value.node else { return; };

if attr != "warn" {
return;
}

// `warn.log` is a special case handled by pygrep-hooks
if !(attr == "warn" && id == "log") {
if !checker
.current_scopes()
.find_map(|scope| scope.values.get(id.as_str()))
.map_or(false, |index| matches!(
&checker.bindings[*index].kind,
BindingKind::Importation(name, full_name) if full_name == "logging" && name == id)
)
{
return;
}
}
}
ExprKind::Name { id, .. } => {
if id != "warn" {
return;
}

if !checker
.current_scopes()
.find_map(|scope| scope.values.get(id.as_str()))
.map_or(false, |index| matches!(
&checker.bindings[*index].kind,
BindingKind::FromImportation(name, full_name) if name == "warn" && full_name == "logging.warn")
)
{
return;
}
}
_ => return,
}

checker.add_check(Check::new(
CheckKind::DeprecatedLogWarn,
Range::from_located(func),
));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: src/pygrep_hooks/mod.rs
expression: checks
---
[]

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
source: src/pygrep_hooks/mod.rs
expression: checks
---
- kind: DeprecatedLogWarn
location:
row: 4
column: 0
end_location:
row: 4
column: 12
fix: ~
- kind: DeprecatedLogWarn
location:
row: 5
column: 0
end_location:
row: 5
column: 8
fix: ~
- kind: DeprecatedLogWarn
location:
row: 6
column: 0
end_location:
row: 6
column: 4
fix: ~

0 comments on commit 2015d87

Please sign in to comment.