Skip to content

Commit

Permalink
Don't consider Union to always be a type alias (#8489)
Browse files Browse the repository at this point in the history
Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
(cherry picked from commit 07127ee)
  • Loading branch information
DanielNoord committed Mar 23, 2023
1 parent b9c1ab3 commit ebf1952
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
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

0 comments on commit ebf1952

Please sign in to comment.