Thanks to visit codestin.com
Credit goes to github.com

Skip to content

assert_never on exclusive union of tuples (type narrowing issue?) #16835

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
hjwp opened this issue Jan 29, 2024 · 4 comments
Open

assert_never on exclusive union of tuples (type narrowing issue?) #16835

hjwp opened this issue Jan 29, 2024 · 4 comments
Labels
topic-match-statement Python 3.10's match statement topic-reachability Detecting unreachable code

Comments

@hjwp
Copy link

hjwp commented Jan 29, 2024

I feel like this code should work but it doesn't. Am I missing something?

from typing_extensions import assert_never

class Left1:
    pass
class Left2:
    pass

class Right1:
    pass
class Right2:
    pass

# this function should take either left1+right1, or left2+right2

def foo(pair: tuple[Left1, Right1] | tuple[Left2, Right2]) -> str:
    match thing:
        case (Left1(), Right1()):
            return "1"
        case (Left2(), Right2()):
            return "2"
        case _:
            # at this point, we should have exhausted both possibilities in the union.
            assert_never(pair)

gives

src/pairs.py:27: error: Argument 1 to "assert_never" has incompatible type "Union[Tuple[Left1, Right1], Tuple[Left2, Right2]]"; expected "NoReturn"  [arg-type]
@JelleZijlstra
Copy link
Member

This should be reported to your type checker (probably mypy); it's not an issue with typing-extensions which just provides the runtime version of assert_never.

I'll transfer the issue to mypy as I don't recall seeing it before.

@JelleZijlstra JelleZijlstra transferred this issue from python/typing_extensions Jan 29, 2024
@JelleZijlstra JelleZijlstra added topic-match-statement Python 3.10's match statement topic-reachability Detecting unreachable code labels Jan 29, 2024
@hjwp
Copy link
Author

hjwp commented Jan 29, 2024

thanks very much for transferring! I didn't realise you could do that. much appreciated :)

@Hnasar
Copy link
Contributor

Hnasar commented Jan 30, 2024

This seems like a duplicate of #15190

@JelleZijlstra
Copy link
Member

It's similar but unlike #15190 this one doesn't involve Literal.

@hjwp hjwp changed the title assert_never on exclusive union of tuples assert_never on exclusive union of tuples (type narrowing issue?) Aug 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-match-statement Python 3.10's match statement topic-reachability Detecting unreachable code
Projects
None yet
Development

No branches or pull requests

3 participants