-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement PGH002 - deprecated use of logging.warn
Refs #980
- Loading branch information
Showing
10 changed files
with
130 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
)); | ||
} |
6 changes: 6 additions & 0 deletions
6
src/pygrep_hooks/snapshots/ruff__pygrep_hooks__tests__PGH002_PGH002_0.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
source: src/pygrep_hooks/mod.rs | ||
expression: checks | ||
--- | ||
[] | ||
|
29 changes: 29 additions & 0 deletions
29
src/pygrep_hooks/snapshots/ruff__pygrep_hooks__tests__PGH002_PGH002_1.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: ~ | ||
|