Closed
Description
Python 3.5.1, mypy 2f89c79.
Test code:
from typing import NamedTuple
Base = NamedTuple('Base', [('param', int)])
class Child(Base):
def __new__(cls, param: int = 1) -> 'Child':
return Base.__new__(cls, param)
print(Child())
Python result:
% python3 test2.py
Child(param=1)
mypy output:
test2.py: note: In member "__new__" of class "Child":
test2.py:9: error: Too many arguments for "__new__" of "object"
test2.py: note: At top level:
test2.py:12: error: Too few arguments for "Child"
I poked around in the code a bit and I believe I managed to narrow this down to the following difference:
- The code above depends on the namedtuple-generated class to have
__new__
defined (CPython: https://github.com/python/cpython/blob/9967d6c1053f186f3b3d118ac336a03752efec32/Lib/collections/__init__.py#L316) - mypy treats namedtuple definitions as if they were overriding
__init__
instead (Line 1444 in 2f89c79