Skip to content

Error in built-in list type model #6100

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
kevinjacobs-progenity opened this issue Dec 22, 2018 · 1 comment
Closed

Error in built-in list type model #6100

kevinjacobs-progenity opened this issue Dec 22, 2018 · 1 comment

Comments

@kevinjacobs-progenity
Copy link

  • problem: list.__iadd__ type model appears to be wrong
  • Python version: CPython 3.6.6 and 3.7.1
  • mypy version: 0.650

Examples:

from typing import List

class MyList1(list): pass
l1 = MyList1([1, 2, 3])
l1 += [4, 5, 6]
l1.extend([7, 8, 9])
print(type(l1), isinstance(l1, MyList1), l1)

class MyList2(List[int]): pass
l2 = MyList2([1, 2, 3])
l2 += [4, 5, 6]
l2.extend([7, 8, 9])
print(type(l2), isinstance(l2, MyList2), l2)

Note that both MyList1 and MyList2 are list subclasses, and all I'm attempting to do is initialize instances and use += (.__iadd__) and .extend to extend them.

Running the above code results in the following mostly unsurprising output in Python 3.6.6:

<class '__main__.MyList1'> True [1, 2, 3, 4, 5, 6, 7, 8, 9]
__main__.MyList2 True [1, 2, 3, 4, 5, 6, 7, 8, 9]

and in Python 3.7.1:

<class '__main__.MyList1'> True [1, 2, 3, 4, 5, 6, 7, 8, 9]
<class '__main__.MyList2'> True [1, 2, 3, 4, 5, 6, 7, 8, 9]

Let's ignore the disparate type representations in Python 3.6.6 -- my concern is that mypy complains that the in-place extensions using __iadd__ result in invalid result types in both cases and both versions:

test.py:5: error: Result type of + incompatible in assignment
test.py:11: error: Result type of + incompatible in assignment

My first reaction was that I am doing something wrong. However, after scouring the internet, this problem doesn't appear to be an instance of one of the common typing pitfalls. The mypy result simply does not match the actual runtime types produced by CPython, which suggests to me that the mypy type model for list.__iadd__ is incorrect and resulting in a spurious error.

I'm reporting this potential bug here, rather than in the typeshed repository, since this deals with a built-in type, assumptions about the core type model, and not the standard library. Apologies in advance if I'm wrong about any of the above.

@ilevkivskyi
Copy link
Member

Thanks for reporting!

This is actually a typeshed issue, mypy uses typeshed even for builtin types. You can try to propose a more precise signature for __iadd__() using self-types (currently it returns List). But be careful with #2354.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants