Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement PGH002 - deprecated use of logging.warn #1250

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,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
8 changes: 8 additions & 0 deletions resources/test/fixtures/pygrep-hooks/PGH002_0.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
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")
15 changes: 15 additions & 0 deletions resources/test/fixtures/pygrep-hooks/PGH002_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import logging
from logging import warn

logging.warn("this is not ok")
log.warn("this is also not ok")
warn("not ok")


def foo():
from logging import warn

def warn():
pass

warn("has been redefined, but we will still report it")
3 changes: 3 additions & 0 deletions src/checkers/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1959,6 +1959,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::deprecated_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 @@ -316,6 +316,7 @@ pub enum CheckCode {
RUF100,
// pygrep-hooks
PGH001,
PGH002,
// pandas-vet
PDV002,
PDV003,
Expand Down Expand Up @@ -887,6 +888,7 @@ pub enum CheckKind {
BooleanPositionalValueInFunctionCall,
// pygrep-hooks
NoEval,
DeprecatedLogWarn,
// flake8-unused-arguments
UnusedFunctionArgument(String),
UnusedMethodArgument(String),
Expand Down Expand Up @@ -1260,6 +1262,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 @@ -1504,6 +1507,7 @@ impl CheckCode {
CheckCode::PDV015 => CheckCategory::PandasVet,
CheckCode::PDV901 => CheckCategory::PandasVet,
CheckCode::PGH001 => CheckCategory::PygrepHooks,
CheckCode::PGH002 => CheckCategory::PygrepHooks,
CheckCode::PLC0414 => CheckCategory::Pylint,
CheckCode::PLC2201 => CheckCategory::Pylint,
CheckCode::PLC3002 => CheckCategory::Pylint,
Expand Down Expand Up @@ -1847,6 +1851,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 @@ -2684,6 +2689,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 @@ -325,6 +325,7 @@ pub enum CheckCodePrefix {
PGH0,
PGH00,
PGH001,
PGH002,
PLC,
PLC0,
PLC04,
Expand Down Expand Up @@ -1424,10 +1425,11 @@ impl CheckCodePrefix {
CheckCodePrefix::PDV9 => vec![CheckCode::PDV901],
CheckCodePrefix::PDV90 => vec![CheckCode::PDV901],
CheckCodePrefix::PDV901 => vec![CheckCode::PDV901],
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 @@ -2258,6 +2260,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
19 changes: 19 additions & 0 deletions src/pygrep_hooks/plugins.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use rustpython_ast::Expr;

use crate::ast::helpers::{collect_call_paths, dealias_call_path, match_call_path};
use crate::ast::types::Range;
use crate::checkers::ast::Checker;
use crate::checks::{Check, CheckKind};

/// PGH002 - deprecated use of logging.warn
pub fn deprecated_log_warn(checker: &mut Checker, func: &Expr) {
let call_path = dealias_call_path(collect_call_paths(func), &checker.import_aliases);
if call_path == ["log", "warn"]
|| match_call_path(&call_path, "logging", "warn", &checker.from_imports)
{
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,37 @@
---
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: ~
- kind: DeprecatedLogWarn
location:
row: 15
column: 4
end_location:
row: 15
column: 8
fix: ~