Skip to content

Commit

Permalink
Fix typelias invalid-name false positives for Union variables witho…
Browse files Browse the repository at this point in the history
…ut assignment. (#8541)
  • Loading branch information
yilei authored Apr 7, 2023
1 parent eeddd66 commit cb255ea
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 7 deletions.
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]

0 comments on commit cb255ea

Please sign in to comment.