Skip to content

Commit b63c8a1

Browse files
authored
Also check the typealias naming style for TypeAlias variables defined in functions. (#8537)
1 parent b5f2b01 commit b63c8a1

File tree

4 files changed

+21
-1
lines changed

4 files changed

+21
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
`TypeAlias` variables defined in functions are now checked for `invalid-name` errors.
2+
3+
Closes #8536

pylint/checkers/base/name_checker/checker.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,12 @@ def visit_assignname( # pylint: disable=too-many-branches
476476
# global introduced variable aren't in the function locals
477477
if node.name in frame and node.name not in frame.argnames():
478478
if not _redefines_import(node):
479-
self._check_name("variable", node.name, node)
479+
if isinstance(
480+
assign_type, nodes.AnnAssign
481+
) and self._assigns_typealias(assign_type.annotation):
482+
self._check_name("typealias", node.name, node)
483+
else:
484+
self._check_name("variable", node.name, node)
480485

481486
# Check names defined in class scopes
482487
elif isinstance(frame, nodes.ClassDef):

tests/functional/t/typealias_naming_style_default.py

+11
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,14 @@
3131
y: Union[str, int]
3232
# But the following, using a good TypeAlias name, is:
3333
GoodTypeAliasToUnion: TypeAlias = Union[str, int]
34+
35+
36+
def my_function():
37+
"""My doc."""
38+
LocalGoodName: TypeAlias = int
39+
local_bad_name: TypeAlias = int # [invalid-name]
40+
local_declaration: Union[str, int]
41+
LocalTypeAliasToUnion: TypeAlias = Union[str, int]
42+
local_declaration = 1
43+
del local_declaration
44+
del LocalGoodName, local_bad_name, LocalTypeAliasToUnion

tests/functional/t/typealias_naming_style_default.txt

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ invalid-name:23:0:23:9::"Type alias name ""_BAD_NAME"" doesn't conform to predef
99
invalid-name:24:0:24:10::"Type alias name ""__BAD_NAME"" doesn't conform to predefined naming style":HIGH
1010
invalid-name:25:0:25:9::"Type alias name ""_1BadName"" doesn't conform to predefined naming style":HIGH
1111
invalid-name:26:0:26:14::"Type alias name ""ANOTHERBADNAME"" doesn't conform to predefined naming style":HIGH
12+
invalid-name:39:4:39:18:my_function:"Type alias name ""local_bad_name"" doesn't conform to predefined naming style":HIGH

0 commit comments

Comments
 (0)