-
Notifications
You must be signed in to change notification settings - Fork 944
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
304 additions
and
1 deletion.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
crates/ruff/resources/test/fixtures/pylint/global_statement.py
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,72 @@ | ||
# # Adapted from: https://github.com/PyCQA/pylint/blob/b70d2abd7fabe9bfd735a30b593b9cd5eaa36194/tests/functional/g/globals.py | ||
CONSTANT = 1 | ||
|
||
def FUNC(): | ||
pass | ||
|
||
|
||
class CLASS: | ||
pass | ||
|
||
|
||
def fix_contant(value): | ||
"""all this is ok, but try not using global ;)""" | ||
global CONSTANT # [global-statement] | ||
print(CONSTANT) | ||
CONSTANT = value | ||
|
||
|
||
def global_with_import(): | ||
"""should only warn for global-statement when using `Import` node""" | ||
global sys # [global-statement] | ||
import sys | ||
|
||
|
||
def global_with_import_from(): | ||
"""should only warn for global-statement when using `ImportFrom` node""" | ||
global namedtuple # [global-statement] | ||
from collections import namedtuple | ||
|
||
|
||
def global_del(): | ||
"""Deleting the global name prevents `global-variable-not-assigned`""" | ||
global CONSTANT # [global-statement] | ||
print(CONSTANT) | ||
del CONSTANT | ||
|
||
|
||
def global_operator_assign(): | ||
"""Operator assigns should only throw a global statement error""" | ||
global CONSTANT # [global-statement] | ||
print(CONSTANT) | ||
CONSTANT += 1 | ||
|
||
|
||
def global_function_assign(): | ||
"""Function assigns should only throw a global statement error""" | ||
global CONSTANT # [global-statement] | ||
|
||
def CONSTANT(): | ||
pass | ||
|
||
CONSTANT() | ||
|
||
|
||
def override_func(): | ||
"""Overriding a function should only throw a global statement error""" | ||
global FUNC # [global-statement] | ||
|
||
def FUNC(): | ||
pass | ||
|
||
FUNC() | ||
|
||
|
||
def override_class(): | ||
"""Overriding a class should only throw a global statement error""" | ||
global CLASS # [global-statement] | ||
|
||
class CLASS(): | ||
pass | ||
|
||
CLASS() |
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,82 @@ | ||
use ruff_macros::{define_violation, derive_message_formats}; | ||
|
||
use crate::ast::types::BindingKind; | ||
use crate::checkers::ast::Checker; | ||
use crate::registry::Diagnostic; | ||
use crate::violation::Violation; | ||
use crate::Range; | ||
|
||
define_violation!( | ||
/// ## What it does | ||
/// Checks for usage of the `global` statement to update a global identifier. | ||
/// | ||
/// ## Why is this bad? | ||
/// Global variables should be avoided unless very necessary because of these non-exhaustive | ||
/// list of reasons: it breaks encapsulation and makes tracing program flow very difficult. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// var = 1 | ||
/// | ||
/// | ||
/// def foo(): | ||
/// global var # [global-statement] | ||
/// var = 10 | ||
/// print(var) | ||
/// | ||
/// | ||
/// foo() | ||
/// print(var) | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// var = 1 | ||
/// | ||
/// | ||
/// def foo(): | ||
/// print(var) | ||
/// return 10 | ||
/// | ||
/// | ||
/// var = foo() | ||
/// print(var) | ||
/// ``` | ||
pub struct GlobalStatement { | ||
pub name: String, | ||
} | ||
); | ||
impl Violation for GlobalStatement { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
let GlobalStatement { name } = self; | ||
format!("Using the global statement to update `{name}` is discouraged") | ||
} | ||
} | ||
|
||
// PLW0603 | ||
pub fn global_statement(checker: &mut Checker, name: &str) { | ||
let scope = &checker.scopes[*checker.scope_stack.last().expect("No current scope found")]; | ||
|
||
// println!("{:#?}", name); | ||
if let Some(&bidx) = scope.bindings.get(name) { | ||
let binding = &checker.bindings[bidx]; | ||
// println!("{:#?}", binding); | ||
if BindingKind::is_global(&binding.kind) { | ||
let diag = Diagnostic::new( | ||
GlobalStatement { | ||
name: name.to_string(), | ||
}, | ||
Range::from_located( | ||
// NOTE: This could've been `binding.range` except pylint wants to report the | ||
// location of the `global` keyword instead of the identifier. | ||
binding | ||
.source | ||
.as_ref() | ||
.expect("Global statements should always have `source`"), | ||
), | ||
); | ||
checker.diagnostics.push(diag); | ||
} | ||
} | ||
} |
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
93 changes: 93 additions & 0 deletions
93
...f/src/rules/pylint/snapshots/ruff__rules__pylint__tests__PLW0603_global_statement.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,93 @@ | ||
--- | ||
source: crates/ruff/src/rules/pylint/mod.rs | ||
expression: diagnostics | ||
--- | ||
- kind: | ||
GlobalStatement: | ||
name: CONSTANT | ||
location: | ||
row: 14 | ||
column: 4 | ||
end_location: | ||
row: 14 | ||
column: 19 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
GlobalStatement: | ||
name: sys | ||
location: | ||
row: 21 | ||
column: 4 | ||
end_location: | ||
row: 21 | ||
column: 14 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
GlobalStatement: | ||
name: namedtuple | ||
location: | ||
row: 27 | ||
column: 4 | ||
end_location: | ||
row: 27 | ||
column: 21 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
GlobalStatement: | ||
name: CONSTANT | ||
location: | ||
row: 33 | ||
column: 4 | ||
end_location: | ||
row: 33 | ||
column: 19 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
GlobalStatement: | ||
name: CONSTANT | ||
location: | ||
row: 40 | ||
column: 4 | ||
end_location: | ||
row: 40 | ||
column: 19 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
GlobalStatement: | ||
name: CONSTANT | ||
location: | ||
row: 47 | ||
column: 4 | ||
end_location: | ||
row: 47 | ||
column: 19 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
GlobalStatement: | ||
name: FUNC | ||
location: | ||
row: 57 | ||
column: 4 | ||
end_location: | ||
row: 57 | ||
column: 15 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
GlobalStatement: | ||
name: CLASS | ||
location: | ||
row: 67 | ||
column: 4 | ||
end_location: | ||
row: 67 | ||
column: 16 | ||
fix: ~ | ||
parent: ~ | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.