Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Also check the typealias naming style for TypeAlias variables defined in functions. #8537

Merged
merged 3 commits into from
Apr 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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