Skip to content

Support assertion style type guards in exhaustive branch analysis #11238

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

Closed
KotlinIsland opened this issue Oct 1, 2021 · 2 comments
Closed
Labels
feature topic-join-v-union Using join vs. using unions topic-type-narrowing Conditional type narrowing / binder

Comments

@KotlinIsland
Copy link
Contributor

def func(a: object, b: bool) -> None:
    if b:
        assert isinstance(a, int)
    else:
        assert isinstance(a, str)

    reveal_type(a)  # note: Revealed type is "builtins.object"

a should be narrowed to int | str but it is instead not narrowed at all.

@hi-ogawa
Copy link
Contributor

Considering the behavior of the example below, "narrowing" is happening in some sense, but possibly mypy's join type is producing join(int, str) = object.

# test1.py

class P:
    pass


class C1(P):
    pass


class C2(P):
    pass


def f(a: object, b: bool) -> None:
    if b:
        assert isinstance(a, C1)
    else:
        assert isinstance(a, C2)

    reveal_type(a)  # note: Revealed type is "test1.P"

@JelleZijlstra JelleZijlstra added the topic-join-v-union Using join vs. using unions label Mar 19, 2022
@AlexWaygood AlexWaygood added the topic-type-narrowing Conditional type narrowing / binder label Mar 28, 2022
@brianschubert
Copy link
Collaborator

This was implemented in #18538, which should release with v1.16.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature topic-join-v-union Using join vs. using unions topic-type-narrowing Conditional type narrowing / binder
Projects
None yet
Development

No branches or pull requests

5 participants