Skip to content

Commit 07127ee

Browse files
Don't consider Union to always be a type alias (#8489)
Co-authored-by: Pierre Sassoulas <[email protected]>
1 parent 355f4fe commit 07127ee

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
No longer consider ``Union`` as type annotation as type alias for naming checks.
2+
3+
Closes #8487

pylint/checkers/base/name_checker/checker.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,13 @@ def _assigns_typealias(node: nodes.NodeNG | None) -> bool:
597597
inferred = utils.safe_infer(node)
598598
if isinstance(inferred, nodes.ClassDef):
599599
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+
)
601607
elif isinstance(inferred, nodes.FunctionDef):
602608
if inferred.qname() == "typing.TypeAlias":
603609
return True

tests/functional/t/typealias_naming_style_default.py

+4
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,7 @@
2121
_BAD_NAME = Union[int, str] # [invalid-name]
2222
__BAD_NAME = Union[int, str] # [invalid-name]
2323
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

0 commit comments

Comments
 (0)