Skip to content

isinstance(x, (T1, T2)) won't narrow the type of x when NamedTuple is involved. #1914

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
kennytm opened this issue Jul 20, 2016 · 1 comment

Comments

@kennytm
Copy link

kennytm commented Jul 20, 2016

Test case:

from typing import NamedTuple, Union

A = NamedTuple('A', [('y', int)])
B = NamedTuple('B', [('x', int)])
C = NamedTuple('C', [('x', int)])

def foo(a: Union[A, B, C]):
    if isinstance(a, (B, C)):
        a.x
        b = a # type: Union[B, C]

This fails with:

1.py: note: In function "foo":
1.py:9: error: Some element of union has no attribute "x"
1.py:10: error: Incompatible types in assignment (expression has type "Union[A, B, C]", variable has type "Union[B, C]")

The error happens only when NamedTuple is involved. Otherwise the type of a is correctly narrowed to Union[B, C] inside the branch.

cc #1328

$ mypy -V
mypy 0.4.3
$ python3 -V
Python 3.5.2
@ddfisher
Copy link
Collaborator

This might be related to #1691.

@gvanrossum gvanrossum added the bug mypy got something wrong label Jul 21, 2016
@gvanrossum gvanrossum added this to the 0.5 milestone Jul 21, 2016
@JukkaL JukkaL self-assigned this Mar 14, 2017
JukkaL added a commit that referenced this issue Mar 19, 2017
JukkaL added a commit that referenced this issue Mar 21, 2017
JukkaL added a commit that referenced this issue Mar 29, 2017
The main change is that unions containing Any are no longer simplified
to just Any. Also union simplification now has a deterministic result unlike
previously, when result could depend on the order of items in a union
(this is true modulo remaining bugs).

This required changes in various other places to keep the existing
semantics, and resulted in some fixes to existing test cases. I also
had to fix some tangentially related minor bugs that were triggered
by the other changes.

We generally don't have fully constructed TypeInfos so
we can't do proper union simplification during semantic
analysis. Just implement simple-minded simplification that 
deals with the cases we care about.

Fixes #2978. Fixes #1914.
JukkaL added a commit that referenced this issue Mar 30, 2017
The main change is that unions containing Any are no longer simplified
to just Any. Also union simplification now has a deterministic result unlike
previously, when result could depend on the order of items in a union
(this is true modulo remaining bugs).

This required changes in various other places to keep the existing
semantics, and resulted in some fixes to existing test cases. I also
had to fix some tangentially related minor bugs that were triggered
by the other changes.

We generally don't have fully constructed TypeInfos so
we can't do proper union simplification during semantic
analysis. Just implement simple-minded simplification that 
deals with the cases we care about.

Fixes #2978. Fixes #1914.
ilevkivskyi pushed a commit that referenced this issue Apr 4, 2017
* Fixes to union simplification, isinstance and more (#3025)

The main change is that unions containing Any are no longer simplified
to just Any. Also union simplification now has a deterministic result unlike
previously, when result could depend on the order of items in a union
(this is true modulo remaining bugs).

This required changes in various other places to keep the existing
semantics, and resulted in some fixes to existing test cases. I also
had to fix some tangentially related minor bugs that were triggered
by the other changes.

We generally don't have fully constructed TypeInfos so
we can't do proper union simplification during semantic
analysis. Just implement simple-minded simplification that 
deals with the cases we care about.

Fixes #2978. Fixes #1914.

* Fix strict optional and partial type special case and fix test

* Fix lint

* Make is_subtype and is_proper_subtype do promotion the same way

Fixes #1850.

* Drop reference to ErrorType (recently expunged)

* Fix merge mistake

* Fix another merge mistake

* Update based on review

* Add test case

* Fix redundant TODO due to copy paste
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants