Skip to content

pow NoReturn overload is used by Pyright and Pyre when argument types are not known #8566

Closed

Description

@debonte

The first overload of pow currently looks like this:

@overload
def pow(base: int, exp: int, mod: Literal[0]) -> NoReturn: ...

When the argument types are not known, Pyright and Pyre use the first overload -- the NoReturn overload.

Here's an example from microsoft/pylance-release#3214:

def RSAcompute(message, exponent, pubkey):
    data = pow(message, exponent, pubkey)
    # The rest of the function is marked as unreachable
    # because Pyright/Pylance see pow's return type as NoReturn
    toto = 42
    return data

res = RSAcompute(12, 34, 56)
print("Similarly this line is considered unreachable")

@AlexWaygood, it looks like you added the NoReturn overload here. Does it actually help users? I'm wondering if it could be removed, or at least moved down in the overload ordering so Pyright and Pyre would behave better in this situation.

Activity

  1. JelleZijlstra commented on Aug 19, 2022

    @JelleZijlstra
    Member

    I think we should remove it. Returning NoReturn isn't a good fit for cases where the code throws an error the user probably isn't expecting, because it can easily lead to type checkers silently skipping the code. Ideally we'd be able to use something like TypingError from python/typing#1043. Until we have that, I think it's better to accept that we can't handle cases like this in the stub.

  2. AlexWaygood commented on Aug 19, 2022

    @AlexWaygood
    Member

    Sure, let's get rid of it and add a TODO comment. Thanks for raising the issue @debonte.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions