Skip to content

Commit ed67cc8

Browse files
Fix typelias invalid-name false positives for Union variables without assignment. (#8541) (#8548)
(cherry picked from commit cb255ea) Co-authored-by: Yilei "Dolee" Yang <[email protected]>
1 parent 011c6ac commit ed67cc8

File tree

4 files changed

+11
-7
lines changed

4 files changed

+11
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
`Union` typed variables without assignment are no longer treated as
2+
`TypeAlias`.
3+
4+
Closes #8540

pylint/checkers/base/name_checker/checker.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -602,10 +602,7 @@ def _assigns_typealias(node: nodes.NodeNG | None) -> bool:
602602
# Union is a special case because it can be used as a type alias
603603
# or as a type annotation. We only want to check the former.
604604
assert node is not None
605-
return not (
606-
isinstance(node.parent, nodes.AnnAssign)
607-
and node.parent.value is not None
608-
)
605+
return not isinstance(node.parent, nodes.AnnAssign)
609606
elif isinstance(inferred, nodes.FunctionDef):
610607
if inferred.qname() == "typing.TypeAlias":
611608
return True

tests/functional/t/typealias_naming_style_default.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,8 @@
2626
ANOTHERBADNAME = Union[int, str] # [invalid-name]
2727

2828
# Regression tests
29-
# This is not a TypeAlias, and thus shouldn't flag the message
29+
# They are not TypeAlias, and thus shouldn't flag the message
3030
x: Union[str, int] = 42
31+
y: Union[str, int]
32+
# But the following, using a good TypeAlias name, is:
33+
GoodTypeAliasToUnion: TypeAlias = Union[str, int]

tests/functional/t/typealias_naming_style_rgx.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
# Valid
55
TypeAliasShouldBeLikeThis: TypeAlias = int
6-
_TypeAliasShouldBeLikeThis: Union[str, int]
6+
_TypeAliasShouldBeLikeThis = Union[str, int]
77

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

0 commit comments

Comments
 (0)