Skip to content

Commit

Permalink
Run Black over fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Feb 26, 2023
1 parent 37b98ae commit 7c0593b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# # Adapted from: https://github.com/PyCQA/pylint/blob/b70d2abd7fabe9bfd735a30b593b9cd5eaa36194/tests/functional/g/globals.py
# Adapted from:
# https://github.com/PyCQA/pylint/blob/b70d2abd7fabe9bfd735a30b593b9cd5eaa36194/tests/functional/g/globals.py

CONSTANT = 1


def FUNC():
pass

Expand Down Expand Up @@ -54,7 +57,7 @@ def CONSTANT():

def override_func():
"""Overriding a function should only throw a global statement error"""
global FUNC # [global-statement]
global FUNC # [global-statement]

def FUNC():
pass
Expand All @@ -66,7 +69,7 @@ def override_class():
"""Overriding a class should only throw a global statement error"""
global CLASS # [global-statement]

class CLASS():
class CLASS:
pass

CLASS()
32 changes: 13 additions & 19 deletions crates/ruff/src/rules/pylint/rules/global_statement.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
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.
/// Checks for the use of `global` statements to update identifiers.
///
/// ## 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.
/// Pylint discourages the use of `global` variables as global mutable
/// state is a common source of bugs and confusing behavior.
///
/// ## Example
/// ```python
/// var = 1
///
///
/// def foo():
/// global var # [global-statement]
/// var = 10
/// print(var)
///
///
/// foo()
/// print(var)
/// ```
Expand All @@ -33,12 +30,10 @@ define_violation!(
/// ```python
/// var = 1
///
///
/// def foo():
/// print(var)
/// return 10
///
///
/// var = foo()
/// print(var)
/// ```
Expand All @@ -54,27 +49,26 @@ impl Violation for GlobalStatement {
}
}

// PLW0603
/// PLW0603
pub fn global_statement(checker: &mut Checker, name: &str) {
let scope = &checker.scopes[*checker.scope_stack.last().expect("No current scope found")];

if let Some(&bidx) = scope.bindings.get(name) {
let binding = &checker.bindings[bidx];
if BindingKind::is_global(&binding.kind) {
let diag = Diagnostic::new(
let scope = checker.current_scope();
if let Some(index) = scope.bindings.get(name) {
let binding = &checker.bindings[*index];
if binding.kind.is_global() {
let diagnostic = Diagnostic::new(
GlobalStatement {
name: name.to_string(),
},
// Match Pylint's behavior by reporting on the `global` statement`, rather
// than the variable usage.
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`"),
.expect("`global` bindings should always have a `source`"),
),
);
checker.diagnostics.push(diag);
checker.diagnostics.push(diagnostic);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,87 +6,87 @@ expression: diagnostics
GlobalStatement:
name: CONSTANT
location:
row: 14
row: 17
column: 4
end_location:
row: 14
row: 17
column: 19
fix: ~
parent: ~
- kind:
GlobalStatement:
name: sys
location:
row: 21
row: 24
column: 4
end_location:
row: 21
row: 24
column: 14
fix: ~
parent: ~
- kind:
GlobalStatement:
name: namedtuple
location:
row: 27
row: 30
column: 4
end_location:
row: 27
row: 30
column: 21
fix: ~
parent: ~
- kind:
GlobalStatement:
name: CONSTANT
location:
row: 33
row: 36
column: 4
end_location:
row: 33
row: 36
column: 19
fix: ~
parent: ~
- kind:
GlobalStatement:
name: CONSTANT
location:
row: 40
row: 43
column: 4
end_location:
row: 40
row: 43
column: 19
fix: ~
parent: ~
- kind:
GlobalStatement:
name: CONSTANT
location:
row: 47
row: 50
column: 4
end_location:
row: 47
row: 50
column: 19
fix: ~
parent: ~
- kind:
GlobalStatement:
name: FUNC
location:
row: 57
row: 60
column: 4
end_location:
row: 57
row: 60
column: 15
fix: ~
parent: ~
- kind:
GlobalStatement:
name: CLASS
location:
row: 67
row: 70
column: 4
end_location:
row: 67
row: 70
column: 16
fix: ~
parent: ~
Expand Down

0 comments on commit 7c0593b

Please sign in to comment.