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

[Backport maintenance/2.17.x] Fix typelias invalid-name false positives for Union variables without assignment. #8548

Merged
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
4 changes: 4 additions & 0 deletions doc/whatsnew/fragments/8540.false_positive
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
`Union` typed variables without assignment are no longer treated as
`TypeAlias`.

Closes #8540
5 changes: 1 addition & 4 deletions pylint/checkers/base/name_checker/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,10 +602,7 @@ def _assigns_typealias(node: nodes.NodeNG | None) -> bool:
# Union is a special case because it can be used as a type alias
# or as a type annotation. We only want to check the former.
assert node is not None
return not (
isinstance(node.parent, nodes.AnnAssign)
and node.parent.value is not None
)
return not isinstance(node.parent, nodes.AnnAssign)
elif isinstance(inferred, nodes.FunctionDef):
if inferred.qname() == "typing.TypeAlias":
return True
Expand Down
5 changes: 4 additions & 1 deletion tests/functional/t/typealias_naming_style_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,8 @@
ANOTHERBADNAME = Union[int, str] # [invalid-name]

# Regression tests
# This is not a TypeAlias, and thus shouldn't flag the message
# They are not TypeAlias, and thus shouldn't flag the message
x: Union[str, int] = 42
y: Union[str, int]
# But the following, using a good TypeAlias name, is:
GoodTypeAliasToUnion: TypeAlias = Union[str, int]
4 changes: 2 additions & 2 deletions tests/functional/t/typealias_naming_style_rgx.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

# Valid
TypeAliasShouldBeLikeThis: TypeAlias = int
_TypeAliasShouldBeLikeThis: Union[str, int]
_TypeAliasShouldBeLikeThis = Union[str, int]

# Invalid
TypeAliasShouldntBeLikeThis: TypeAlias = int # [invalid-name]
_TypeAliasShouldntBeLikeThis: Union[str, int] # [invalid-name]
_TypeAliasShouldntBeLikeThis = Union[str, int] # [invalid-name]