summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYilei "Dolee" Yang <yileiyang@google.com>2023-04-07 13:17:19 -0700
committerGitHub <noreply@github.com>2023-04-07 22:17:19 +0200
commitb63c8a1c148e85e1ad4e645b7fffd8c8772d6201 (patch)
treea5ce101c4d90149d1b4ee4a110d16a208a59ce5d
parentb5f2b01635edd23fecc1546f3fdb2a41e6a51995 (diff)
downloadpylint-git-b63c8a1c148e85e1ad4e645b7fffd8c8772d6201.tar.gz
Also check the typealias naming style for TypeAlias variables defined in functions. (#8537)
-rw-r--r--doc/whatsnew/fragments/8536.false_negative3
-rw-r--r--pylint/checkers/base/name_checker/checker.py7
-rw-r--r--tests/functional/t/typealias_naming_style_default.py11
-rw-r--r--tests/functional/t/typealias_naming_style_default.txt1
4 files changed, 21 insertions, 1 deletions
diff --git a/doc/whatsnew/fragments/8536.false_negative b/doc/whatsnew/fragments/8536.false_negative
new file mode 100644
index 000000000..8b24bc96f
--- /dev/null
+++ b/doc/whatsnew/fragments/8536.false_negative
@@ -0,0 +1,3 @@
+`TypeAlias` variables defined in functions are now checked for `invalid-name` errors.
+
+Closes #8536
diff --git a/pylint/checkers/base/name_checker/checker.py b/pylint/checkers/base/name_checker/checker.py
index 9203d7acf..56ae925fb 100644
--- a/pylint/checkers/base/name_checker/checker.py
+++ b/pylint/checkers/base/name_checker/checker.py
@@ -476,7 +476,12 @@ class NameChecker(_BasicChecker):
# global introduced variable aren't in the function locals
if node.name in frame and node.name not in frame.argnames():
if not _redefines_import(node):
- self._check_name("variable", node.name, node)
+ if isinstance(
+ assign_type, nodes.AnnAssign
+ ) and self._assigns_typealias(assign_type.annotation):
+ self._check_name("typealias", node.name, node)
+ else:
+ self._check_name("variable", node.name, node)
# Check names defined in class scopes
elif isinstance(frame, nodes.ClassDef):
diff --git a/tests/functional/t/typealias_naming_style_default.py b/tests/functional/t/typealias_naming_style_default.py
index 8baabb49c..6a2c81569 100644
--- a/tests/functional/t/typealias_naming_style_default.py
+++ b/tests/functional/t/typealias_naming_style_default.py
@@ -31,3 +31,14 @@ x: Union[str, int] = 42
y: Union[str, int]
# But the following, using a good TypeAlias name, is:
GoodTypeAliasToUnion: TypeAlias = Union[str, int]
+
+
+def my_function():
+ """My doc."""
+ LocalGoodName: TypeAlias = int
+ local_bad_name: TypeAlias = int # [invalid-name]
+ local_declaration: Union[str, int]
+ LocalTypeAliasToUnion: TypeAlias = Union[str, int]
+ local_declaration = 1
+ del local_declaration
+ del LocalGoodName, local_bad_name, LocalTypeAliasToUnion
diff --git a/tests/functional/t/typealias_naming_style_default.txt b/tests/functional/t/typealias_naming_style_default.txt
index 455cbc213..24090841f 100644
--- a/tests/functional/t/typealias_naming_style_default.txt
+++ b/tests/functional/t/typealias_naming_style_default.txt
@@ -9,3 +9,4 @@ invalid-name:23:0:23:9::"Type alias name ""_BAD_NAME"" doesn't conform to predef
invalid-name:24:0:24:10::"Type alias name ""__BAD_NAME"" doesn't conform to predefined naming style":HIGH
invalid-name:25:0:25:9::"Type alias name ""_1BadName"" doesn't conform to predefined naming style":HIGH
invalid-name:26:0:26:14::"Type alias name ""ANOTHERBADNAME"" doesn't conform to predefined naming style":HIGH
+invalid-name:39:4:39:18:my_function:"Type alias name ""local_bad_name"" doesn't conform to predefined naming style":HIGH