Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 8 additions & 0 deletions pylint/checkers/base/name_checker/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,14 @@ def _assigns_typealias(node: nodes.NodeNG | None) -> bool:
inferred = utils.safe_infer(node)
if isinstance(inferred, nodes.ClassDef):
if inferred.qname() == ".Union":
# 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
if (
isinstance(node.parent, nodes.AnnAssign)
and node.parent.value is not None
):
return False
return True
Comment thread
DanielNoord marked this conversation as resolved.
Outdated
elif isinstance(inferred, nodes.FunctionDef):
if inferred.qname() == "typing.TypeAlias":
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