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) (#8548)

(cherry picked from commit cb255ea)

Co-authored-by: Yilei "Dolee" Yang <[email protected]>
  • Loading branch information
github-actions[bot] and yilei committed Apr 7, 2023
1 parent 011c6ac commit ed67cc8
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 ed67cc8

Please sign in to comment.