Skip to content

typing_extensions assert_never's Never not supported #7565

@henryiii

Description

@henryiii

Bug description

Python 3.11 added Never as an alias of NoReturn. The signature of assert_never is assert_never(__x: Never) -> Never (personally, I wish had been assert_never(__x: Never) -> NoReturn, but that's what it is). This was backported to typing_extensions 4.1.0, as well.

I tried the following, but it seems typing_extensions.assert_never is uninferable.

diff --git a/pylint/checkers/refactoring/refactoring_checker.py b/pylint/checkers/refactoring/refactoring_checker.py
index ed1b7a938..21b9b2b07 100644
--- a/pylint/checkers/refactoring/refactoring_checker.py
+++ b/pylint/checkers/refactoring/refactoring_checker.py
@@ -1907,9 +1907,9 @@ class RefactoringChecker(checkers.BaseTokenChecker):
         if isinstance(node, nodes.FunctionDef) and node.returns:
             return (
                 isinstance(node.returns, nodes.Attribute)
-                and node.returns.attrname == "NoReturn"
+                and node.returns.attrname in {"NoReturn", "Never"}
                 or isinstance(node.returns, nodes.Name)
-                and node.returns.name == "NoReturn"
+                and node.returns.name in {"NoReturn", "Never"}
             )
         try:
             return node.qname() in self._never_returning_functions

If it's defined locally, however:

def assert_never(value: Never) -> Never:
    assert False, f"Unhandled value: {value} ({type(value).__name__})"

Then the above patch fixes it, so there's that, at least. Maybe that would at least fix it on Python 3.11? Maybe typing_extensions.assert_never could be added to the list of no return functions if it can't be inferred? However, when I stick an print statement in here, it seems the node is "Uninferrable", and adding "typing_extensions.assert_never" to refactoring.never-returning-functions does not work.

This is using cibuildwheel's source, see pypa/cibuildwheel#1291.

It looks like this was partially addressed in #7311, but not for all places NoReturn shows up.

Configuration

No response

Command used

pylint src

Pylint output

cibuildwheel/architecture.py:117:4: R1710: Either all return statements in a function should return an expression, or none of them should. (inconsistent-return-statements)

Expected behavior

Your code has been rated at 10.00/10 (previous run: 10.00/10, +0.00)

Pylint version

pylint 2.15.3
astroid 2.12.10
Python 3.10.7 (main, Sep 15 2022, 01:51:29) [Clang 14.0.0 (clang-1400.0.29.102)]

OS / Environment

No response

Additional dependencies

typing_extensions==4.3.0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions