From bd4c8f1a9b6b420ba3e3ad5afe265fb7bb6b2d15 Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Fri, 12 Jul 2024 08:38:08 -0400 Subject: [PATCH] Handle assert_never() when imported from typing_extensions (#9782) (#9790) (cherry picked from commit a48cd4c6a872b6565bc58030b74585812f327f36) --- doc/whatsnew/fragments/9780.false_positive | 3 +++ pylint/checkers/variables.py | 5 ++--- tests/functional/u/used/used_before_assignment_py311.py | 8 +++++++- tests/functional/u/used/used_before_assignment_py311.rc | 2 -- 4 files changed, 12 insertions(+), 6 deletions(-) create mode 100644 doc/whatsnew/fragments/9780.false_positive delete mode 100644 tests/functional/u/used/used_before_assignment_py311.rc diff --git a/doc/whatsnew/fragments/9780.false_positive b/doc/whatsnew/fragments/9780.false_positive new file mode 100644 index 0000000000..219a4b500e --- /dev/null +++ b/doc/whatsnew/fragments/9780.false_positive @@ -0,0 +1,3 @@ +Treat `assert_never()` the same way when imported from `typing_extensions`. + +Closes #9780 diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py index bfc80697c4..495051f315 100644 --- a/pylint/checkers/variables.py +++ b/pylint/checkers/variables.py @@ -32,7 +32,7 @@ is_sys_guard, overridden_method, ) -from pylint.constants import PY39_PLUS, PY311_PLUS, TYPING_NEVER, TYPING_NORETURN +from pylint.constants import PY39_PLUS, TYPING_NEVER, TYPING_NORETURN from pylint.interfaces import CONTROL_FLOW, HIGH, INFERENCE, INFERENCE_FAILURE from pylint.typing import MessageDefinitionTuple @@ -944,8 +944,7 @@ def _defines_name_raises_or_returns(name: str, node: nodes.NodeNG) -> bool: if utils.is_terminating_func(node.value): return True if ( - PY311_PLUS - and isinstance(node.value.func, nodes.Name) + isinstance(node.value.func, nodes.Name) and node.value.func.name == "assert_never" ): return True diff --git a/tests/functional/u/used/used_before_assignment_py311.py b/tests/functional/u/used/used_before_assignment_py311.py index 2e46ff5fd6..5c69e1067d 100644 --- a/tests/functional/u/used/used_before_assignment_py311.py +++ b/tests/functional/u/used/used_before_assignment_py311.py @@ -1,6 +1,12 @@ """assert_never() introduced in 3.11""" from enum import Enum -from typing import assert_never + +from pylint.constants import PY311_PLUS + +if PY311_PLUS: + from typing import assert_never # pylint: disable=no-name-in-module +else: + from typing_extensions import assert_never class MyEnum(Enum): diff --git a/tests/functional/u/used/used_before_assignment_py311.rc b/tests/functional/u/used/used_before_assignment_py311.rc deleted file mode 100644 index 56e6770585..0000000000 --- a/tests/functional/u/used/used_before_assignment_py311.rc +++ /dev/null @@ -1,2 +0,0 @@ -[testoptions] -min_pyver=3.11