From fbd84c2e9bccf53233850034f92e4b3a2bf371d4 Mon Sep 17 00:00:00 2001 From: Mark Byrne <31762852+mbyrnepr2@users.noreply.github.com> Date: Mon, 27 Feb 2023 12:55:41 +0100 Subject: [PATCH] Prevent emitting ``invalid-name`` on 'global' redefinition (#8337) Closes #8307 Co-authored-by: Pierre Sassoulas --- doc/whatsnew/fragments/8307.bugfix | 3 +++ pylint/checkers/base/name_checker/checker.py | 5 ----- tests/functional/g/globals.py | 9 +++++++++ tests/functional/g/globals.txt | 1 + tests/functional/n/name/name_styles.py | 2 +- tests/functional/n/name/name_styles.txt | 1 - 6 files changed, 14 insertions(+), 7 deletions(-) create mode 100644 doc/whatsnew/fragments/8307.bugfix diff --git a/doc/whatsnew/fragments/8307.bugfix b/doc/whatsnew/fragments/8307.bugfix new file mode 100644 index 0000000000..f86f7b67b8 --- /dev/null +++ b/doc/whatsnew/fragments/8307.bugfix @@ -0,0 +1,3 @@ +Prevent emitting ``invalid-name`` for the line on which a ``global`` statement is declared. + +Closes #8307 diff --git a/pylint/checkers/base/name_checker/checker.py b/pylint/checkers/base/name_checker/checker.py index cdbf4a7b54..c18af89cc9 100644 --- a/pylint/checkers/base/name_checker/checker.py +++ b/pylint/checkers/base/name_checker/checker.py @@ -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", diff --git a/tests/functional/g/globals.py b/tests/functional/g/globals.py index 3960df6548..ce289acf85 100644 --- a/tests/functional/g/globals.py +++ b/tests/functional/g/globals.py @@ -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" diff --git a/tests/functional/g/globals.txt b/tests/functional/g/globals.txt index a378720939..da267fdde2 100644 --- a/tests/functional/g/globals.txt +++ b/tests/functional/g/globals.txt @@ -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 diff --git a/tests/functional/n/name/name_styles.py b/tests/functional/n/name/name_styles.py index 8c71e79371..47ad26e797 100644 --- a/tests/functional/n/name/name_styles.py +++ b/tests/functional/n/name/name_styles.py @@ -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 diff --git a/tests/functional/n/name/name_styles.txt b/tests/functional/n/name/name_styles.txt index ad7dad05fb..720efd3820 100644 --- a/tests/functional/n/name/name_styles.txt +++ b/tests/functional/n/name/name_styles.txt @@ -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