Skip to content

Commit 71cdb25

Browse files
Prevent emitting invalid-name on 'global' redefinition (#8337) (#8354)
Closes #8307 Co-authored-by: Mark Byrne <[email protected]>
1 parent ff8cdd2 commit 71cdb25

File tree

6 files changed

+14
-7
lines changed

6 files changed

+14
-7
lines changed

doc/whatsnew/fragments/8307.bugfix

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Prevent emitting ``invalid-name`` for the line on which a ``global`` statement is declared.
2+
3+
Closes #8307

pylint/checkers/base/name_checker/checker.py

-5
Original file line numberDiff line numberDiff line change
@@ -388,11 +388,6 @@ def visit_functiondef(self, node: nodes.FunctionDef) -> None:
388388

389389
visit_asyncfunctiondef = visit_functiondef
390390

391-
@utils.only_required_for_messages("disallowed-name", "invalid-name")
392-
def visit_global(self, node: nodes.Global) -> None:
393-
for name in node.names:
394-
self._check_name("const", name, node)
395-
396391
@utils.only_required_for_messages(
397392
"disallowed-name",
398393
"invalid-name",

tests/functional/g/globals.py

+9
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,12 @@ class CLASS():
9090
pass
9191

9292
CLASS()
93+
94+
95+
# Prevent emitting `invalid-name` for the line on which `global` is declared
96+
# https://github.com/PyCQA/pylint/issues/8307
97+
98+
_foo: str = "tomato"
99+
def setup_shared_foo():
100+
global _foo # [global-statement]
101+
_foo = "potato"

tests/functional/g/globals.txt

+1
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ global-statement:60:4:60:19:global_operator_assign:Using the global statement:UN
1212
global-statement:67:4:67:19:global_function_assign:Using the global statement:UNDEFINED
1313
global-statement:77:4:77:15:override_func:Using the global statement:UNDEFINED
1414
global-statement:87:4:87:16:override_class:Using the global statement:UNDEFINED
15+
global-statement:100:4:100:15:setup_shared_foo:Using the global statement:UNDEFINED

tests/functional/n/name/name_styles.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class EmbeddedClass:
9494
def test_globals():
9595
"""Names in global statements are also checked."""
9696
global NOT_CORRECT
97-
global AlsoCorrect # [invalid-name]
97+
global AlsoCorrect
9898
NOT_CORRECT = 1
9999
AlsoCorrect = 2
100100

tests/functional/n/name/name_styles.txt

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ invalid-name:53:4:53:38:CorrectClassName.__DunDER_IS_not_free_for_all__:"Method
1010
invalid-name:83:0:83:18::"Class name ""BAD_NAME_FOR_CLASS"" doesn't conform to PascalCase naming style":HIGH
1111
invalid-name:84:0:84:23::"Class name ""NEXT_BAD_NAME_FOR_CLASS"" doesn't conform to PascalCase naming style":HIGH
1212
invalid-name:91:0:91:11::"Class name ""NOT_CORRECT"" doesn't conform to PascalCase naming style":HIGH
13-
invalid-name:97:4:97:22:test_globals:"Constant name ""AlsoCorrect"" doesn't conform to UPPER_CASE naming style":HIGH
1413
invalid-name:110:4:110:21:FooClass.PROPERTY_NAME:"Attribute name ""PROPERTY_NAME"" doesn't conform to snake_case naming style":INFERENCE
1514
invalid-name:116:4:116:30:FooClass.ABSTRACT_PROPERTY_NAME:"Attribute name ""ABSTRACT_PROPERTY_NAME"" doesn't conform to snake_case naming style":INFERENCE
1615
invalid-name:121:4:121:28:FooClass.PROPERTY_NAME_SETTER:"Attribute name ""PROPERTY_NAME_SETTER"" doesn't conform to snake_case naming style":INFERENCE

0 commit comments

Comments
 (0)