File tree 3 files changed +14
-1
lines changed
pylint/checkers/base/name_checker
3 files changed +14
-1
lines changed Original file line number Diff line number Diff line change
1
+ No longer consider ``Union`` as type annotation as type alias for naming checks.
2
+
3
+ Closes #8487
Original file line number Diff line number Diff line change @@ -597,7 +597,13 @@ def _assigns_typealias(node: nodes.NodeNG | None) -> bool:
597
597
inferred = utils .safe_infer (node )
598
598
if isinstance (inferred , nodes .ClassDef ):
599
599
if inferred .qname () == ".Union" :
600
- return True
600
+ # Union is a special case because it can be used as a type alias
601
+ # or as a type annotation. We only want to check the former.
602
+ assert node is not None
603
+ return not (
604
+ isinstance (node .parent , nodes .AnnAssign )
605
+ and node .parent .value is not None
606
+ )
601
607
elif isinstance (inferred , nodes .FunctionDef ):
602
608
if inferred .qname () == "typing.TypeAlias" :
603
609
return True
Original file line number Diff line number Diff line change 21
21
_BAD_NAME = Union [int , str ] # [invalid-name]
22
22
__BAD_NAME = Union [int , str ] # [invalid-name]
23
23
ANOTHERBADNAME = Union [int , str ] # [invalid-name]
24
+
25
+ # Regression tests
26
+ # This is not a TypeAlias, and thus shouldn't flag the message
27
+ x : Union [str , int ] = 42
You can’t perform that action at this time.
0 commit comments