Skip to content

Commit

Permalink
Also check the typealias naming style for TypeAlias variables defined…
Browse files Browse the repository at this point in the history
… in functions. (#8537)
  • Loading branch information
yilei committed Apr 7, 2023
1 parent b5f2b01 commit b63c8a1
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/8536.false_negative
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
`TypeAlias` variables defined in functions are now checked for `invalid-name` errors.

Closes #8536
7 changes: 6 additions & 1 deletion pylint/checkers/base/name_checker/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,12 @@ def visit_assignname( # pylint: disable=too-many-branches
# 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):
Expand Down
11 changes: 11 additions & 0 deletions tests/functional/t/typealias_naming_style_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,14 @@
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
1 change: 1 addition & 0 deletions tests/functional/t/typealias_naming_style_default.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit b63c8a1

Please sign in to comment.