Skip to content
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

isinstance check does not narrow inferred type correctly on nested Union type #3196

Closed
betaveros opened this issue Apr 19, 2017 · 2 comments · Fixed by #3298
Closed

isinstance check does not narrow inferred type correctly on nested Union type #3196

betaveros opened this issue Apr 19, 2017 · 2 comments · Fixed by #3298
Labels

Comments

@betaveros
Copy link

from typing import Union

class A: pass
class B: pass
class C: pass

def foo(bar: Union[Union[A, B], C]) -> None:
    if isinstance(bar, A):
        reveal_type(bar)
    else:
        reveal_type(bar)

The last line reveals Union[A, B, C] when it should be just Union[B, C]. It works if the type of bar is replaced by Union[A, B, C].

(Of course you'd probably never directly write Union[Union[A, B], C], but it's reasonable to write Union[AB, C] where AB is an alias for Union[A, B]. And the typing module documentation says Unions of Unions are flattened.)

@gvanrossum
Copy link
Member

Is this still failing on master?

@ilevkivskyi
Copy link
Member

Yes, I just checked this. Probably this is because interaction of restrict_subtype_away and is_proper_subtype misses the case of nested unions.

gvanrossum pushed a commit that referenced this issue May 2, 2017
The idea is that we should have a normalized internal representation of unions to simplify reasoning.

This also turns out to fix #3196.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants