summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPierre Sassoulas <pierre.sassoulas@gmail.com>2023-02-27 15:27:54 +0100
committerGitHub <noreply@github.com>2023-02-27 15:27:54 +0100
commit71cdb25cd32d05e07d4b8d59ec6ba8ace501b423 (patch)
tree654ba45d0146f9bdda0d2f5d8b105b257920794e
parentff8cdd2b4096c44854731aba556f8a948ff9b3c4 (diff)
downloadpylint-git-71cdb25cd32d05e07d4b8d59ec6ba8ace501b423.tar.gz
Prevent emitting ``invalid-name`` on 'global' redefinition (#8337) (#8354)
Closes #8307 Co-authored-by: Mark Byrne <31762852+mbyrnepr2@users.noreply.github.com>
-rw-r--r--doc/whatsnew/fragments/8307.bugfix3
-rw-r--r--pylint/checkers/base/name_checker/checker.py5
-rw-r--r--tests/functional/g/globals.py9
-rw-r--r--tests/functional/g/globals.txt1
-rw-r--r--tests/functional/n/name/name_styles.py2
-rw-r--r--tests/functional/n/name/name_styles.txt1
6 files changed, 14 insertions, 7 deletions
diff --git a/doc/whatsnew/fragments/8307.bugfix b/doc/whatsnew/fragments/8307.bugfix
new file mode 100644
index 000000000..f86f7b67b
--- /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 cdbf4a7b5..c18af89cc 100644
--- a/pylint/checkers/base/name_checker/checker.py
+++ b/pylint/checkers/base/name_checker/checker.py
@@ -388,11 +388,6 @@ class NameChecker(_BasicChecker):
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 3960df654..ce289acf8 100644
--- a/tests/functional/g/globals.py
+++ b/tests/functional/g/globals.py
@@ -90,3 +90,12 @@ def override_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 a37872093..da267fdde 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 8c71e7937..47ad26e79 100644
--- a/tests/functional/n/name/name_styles.py
+++ b/tests/functional/n/name/name_styles.py
@@ -94,7 +94,7 @@ NOT_CORRECT = CorrectClassName # [invalid-name]
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 ad7dad05f..720efd382 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