diff options
author | Daniël van Noord <13665637+DanielNoord@users.noreply.github.com> | 2023-03-23 22:53:01 +0100 |
---|---|---|
committer | Daniël van Noord <13665637+DanielNoord@users.noreply.github.com> | 2023-03-23 23:48:41 +0100 |
commit | ebf1952eb5b4bac46751d91181a62bea4bd6599f (patch) | |
tree | cb0c911b3cdc70cd82610ab716bb12ccdc1f522b | |
parent | b9c1ab3cd5323f5cb305a12c9e105102e4200ead (diff) | |
download | pylint-git-ebf1952eb5b4bac46751d91181a62bea4bd6599f.tar.gz |
Don't consider ``Union`` to always be a type alias (#8489)
Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
(cherry picked from commit 07127ee75f3456920ff5117cdbaf8697744b6cfc)
-rw-r--r-- | doc/whatsnew/fragments/8487.false_positive | 3 | ||||
-rw-r--r-- | pylint/checkers/base/name_checker/checker.py | 8 | ||||
-rw-r--r-- | tests/functional/t/typealias_naming_style_default.py | 4 |
3 files changed, 14 insertions, 1 deletions
diff --git a/doc/whatsnew/fragments/8487.false_positive b/doc/whatsnew/fragments/8487.false_positive new file mode 100644 index 000000000..9ff5e3482 --- /dev/null +++ b/doc/whatsnew/fragments/8487.false_positive @@ -0,0 +1,3 @@ +No longer consider ``Union`` as type annotation as type alias for naming checks. + +Closes #8487 diff --git a/pylint/checkers/base/name_checker/checker.py b/pylint/checkers/base/name_checker/checker.py index 1341edc96..616067d1f 100644 --- a/pylint/checkers/base/name_checker/checker.py +++ b/pylint/checkers/base/name_checker/checker.py @@ -597,7 +597,13 @@ class NameChecker(_BasicChecker): 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 diff --git a/tests/functional/t/typealias_naming_style_default.py b/tests/functional/t/typealias_naming_style_default.py index 45f801521..a2d02c0c6 100644 --- a/tests/functional/t/typealias_naming_style_default.py +++ b/tests/functional/t/typealias_naming_style_default.py @@ -21,3 +21,7 @@ BAD_NAME = Union[int, str] # [invalid-name] _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 |