Skip to content

Commit

Permalink
Prevent emitting invalid-name on 'global' redefinition (#8337)
Browse files Browse the repository at this point in the history
Closes #8307
  • Loading branch information
mbyrnepr2 authored Feb 27, 2023
1 parent 641f526 commit d30c45b
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 7 deletions.
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/8307.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 1,3 @@
Prevent emitting ``invalid-name`` for the line on which a ``global`` statement is declared.

Closes #8307
5 changes: 0 additions & 5 deletions pylint/checkers/base/name_checker/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,11 389,6 @@ def visit_functiondef(self, node: nodes.FunctionDef) -> None:

visit_asyncfunctiondef = visit_functiondef

@utils.only_required_for_messages("disallowed-name", "invalid-name")
def visit_global(self, node: nodes.Global) -> None:
for name in node.names:
self._check_name("const", name, node)

@utils.only_required_for_messages(
"disallowed-name",
"invalid-name",
Expand Down
9 changes: 9 additions & 0 deletions tests/functional/g/globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 102,12 @@ def init_connection_state(alias):
global RAN_DB_DICT # [global-variable-not-assigned]
RAN_DB_SET.add(alias)
return RAN_DB_DICT.setdefault("color", "Palomino")


# Prevent emitting `invalid-name` for the line on which `global` is declared
# https://github.com/PyCQA/pylint/issues/8307

_foo: str = "tomato"
def setup_shared_foo():
global _foo # [global-statement]
_foo = "potato"
1 change: 1 addition & 0 deletions tests/functional/g/globals.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 14,4 @@ global-statement:81:4:81:15:override_func:Using the global statement:HIGH
global-statement:91:4:91:16:override_class:Using the global statement:HIGH
global-variable-not-assigned:101:4:101:21:init_connection_state:Using global for 'RAN_DB_SET' but no assignment is done:HIGH
global-variable-not-assigned:102:4:102:22:init_connection_state:Using global for 'RAN_DB_DICT' but no assignment is done:HIGH
global-statement:112:4:112:15:setup_shared_foo:Using the global statement:HIGH
2 changes: 1 addition & 1 deletion tests/functional/n/name/name_styles.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 94,7 @@ class EmbeddedClass:
def test_globals():
"""Names in global statements are also checked."""
global NOT_CORRECT
global AlsoCorrect # [invalid-name]
global AlsoCorrect
NOT_CORRECT = 1
AlsoCorrect = 2

Expand Down
1 change: 0 additions & 1 deletion tests/functional/n/name/name_styles.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 10,6 @@ invalid-name:53:4:53:38:CorrectClassName.__DunDER_IS_not_free_for_all__:"Method
invalid-name:83:0:83:18::"Class name ""BAD_NAME_FOR_CLASS"" doesn't conform to PascalCase naming style":HIGH
invalid-name:84:0:84:23::"Class name ""NEXT_BAD_NAME_FOR_CLASS"" doesn't conform to PascalCase naming style":HIGH
invalid-name:91:0:91:11::"Class name ""NOT_CORRECT"" doesn't conform to PascalCase naming style":HIGH
invalid-name:97:4:97:22:test_globals:"Constant name ""AlsoCorrect"" doesn't conform to UPPER_CASE naming style":HIGH
invalid-name:110:4:110:21:FooClass.PROPERTY_NAME:"Attribute name ""PROPERTY_NAME"" doesn't conform to snake_case naming style":INFERENCE
invalid-name:116:4:116:30:FooClass.ABSTRACT_PROPERTY_NAME:"Attribute name ""ABSTRACT_PROPERTY_NAME"" doesn't conform to snake_case naming style":INFERENCE
invalid-name:121:4:121:28:FooClass.PROPERTY_NAME_SETTER:"Attribute name ""PROPERTY_NAME_SETTER"" doesn't conform to snake_case naming style":INFERENCE
Expand Down

0 comments on commit d30c45b

Please sign in to comment.