Skip to content

Commit

Permalink
Prevent emitting invalid-name on 'global' redefinition (#8337) (#…
Browse files Browse the repository at this point in the history
…8354)

Closes #8307

Co-authored-by: Mark Byrne <[email protected]>
  • Loading branch information
Pierre-Sassoulas and mbyrnepr2 authored Feb 27, 2023
1 parent ff8cdd2 commit 71cdb25
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 @@ -388,11 +388,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 @@ -90,3 +90,12 @@ class CLASS():
pass

CLASS()


# 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 @@ -12,3 +12,4 @@ global-statement:60:4:60:19:global_operator_assign:Using the global statement:UN
global-statement:67:4:67:19:global_function_assign:Using the global statement:UNDEFINED
global-statement:77:4:77:15:override_func:Using the global statement:UNDEFINED
global-statement:87:4:87:16:override_class:Using the global statement:UNDEFINED
global-statement:100:4:100:15:setup_shared_foo:Using the global statement:UNDEFINED
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 71cdb25

Please sign in to comment.