Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/8487.false_positive
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
No longer consider ``Union`` as type annotation as type alias for naming checks.

Closes #8487
8 changes: 7 additions & 1 deletion pylint/checkers/base/name_checker/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,13 @@ def _assigns_typealias(node: nodes.NodeNG | None) -> bool:
inferred = utils.safe_infer(node)
if isinstance(inferred, nodes.ClassDef):
if inferred.qname() == ".Union":
return True
# 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
)
elif isinstance(inferred, nodes.FunctionDef):
if inferred.qname() == "typing.TypeAlias":
return True
Expand Down
4 changes: 4 additions & 0 deletions tests/functional/t/typealias_naming_style_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@
_BAD_NAME = Union[int, str] # [invalid-name]
__BAD_NAME = Union[int, str] # [invalid-name]
ANOTHERBADNAME = Union[int, str] # [invalid-name]

# Regression tests
# This is not a TypeAlias, and thus shouldn't flag the message
x: Union[str, int] = 42