From ed67cc806606668bec945c9b62f006d7aad02664 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Fri, 7 Apr 2023 11:53:06 +0000 Subject: [PATCH] Fix typelias `invalid-name` false positives for Union variables without assignment. (#8541) (#8548) (cherry picked from commit cb255eaaed8bba6bec1f7bf5d4cde15821c1dd46) Co-authored-by: Yilei "Dolee" Yang --- doc/whatsnew/fragments/8540.false_positive | 4 ++++ pylint/checkers/base/name_checker/checker.py | 5 +---- tests/functional/t/typealias_naming_style_default.py | 5 ++++- tests/functional/t/typealias_naming_style_rgx.py | 4 ++-- 4 files changed, 11 insertions(+), 7 deletions(-) create mode 100644 doc/whatsnew/fragments/8540.false_positive diff --git a/doc/whatsnew/fragments/8540.false_positive b/doc/whatsnew/fragments/8540.false_positive new file mode 100644 index 0000000000..543913637d --- /dev/null +++ b/doc/whatsnew/fragments/8540.false_positive @@ -0,0 +1,4 @@ +`Union` typed variables without assignment are no longer treated as +`TypeAlias`. + +Closes #8540 diff --git a/pylint/checkers/base/name_checker/checker.py b/pylint/checkers/base/name_checker/checker.py index c2b615a481..58f7198ef0 100644 --- a/pylint/checkers/base/name_checker/checker.py +++ b/pylint/checkers/base/name_checker/checker.py @@ -602,10 +602,7 @@ def _assigns_typealias(node: nodes.NodeNG | None) -> bool: # 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 - ) + return not isinstance(node.parent, nodes.AnnAssign) 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 6b27c14a01..8baabb49cd 100644 --- a/tests/functional/t/typealias_naming_style_default.py +++ b/tests/functional/t/typealias_naming_style_default.py @@ -26,5 +26,8 @@ ANOTHERBADNAME = Union[int, str] # [invalid-name] # Regression tests -# This is not a TypeAlias, and thus shouldn't flag the message +# They are not TypeAlias, and thus shouldn't flag the message x: Union[str, int] = 42 +y: Union[str, int] +# But the following, using a good TypeAlias name, is: +GoodTypeAliasToUnion: TypeAlias = Union[str, int] diff --git a/tests/functional/t/typealias_naming_style_rgx.py b/tests/functional/t/typealias_naming_style_rgx.py index 0910644dab..bc43b930d5 100644 --- a/tests/functional/t/typealias_naming_style_rgx.py +++ b/tests/functional/t/typealias_naming_style_rgx.py @@ -3,8 +3,8 @@ # Valid TypeAliasShouldBeLikeThis: TypeAlias = int -_TypeAliasShouldBeLikeThis: Union[str, int] +_TypeAliasShouldBeLikeThis = Union[str, int] # Invalid TypeAliasShouldntBeLikeThis: TypeAlias = int # [invalid-name] -_TypeAliasShouldntBeLikeThis: Union[str, int] # [invalid-name] +_TypeAliasShouldntBeLikeThis = Union[str, int] # [invalid-name]