Mypy: NamedTuple subclass with default arguments doesn't type check

Created on 12 Jan 2017  路  5Comments  路  Source: python/mypy

from typing import NamedTuple

Unit = str
Factor = str

_Spec = NamedTuple(
        '_Spec',
    [('unit', Unit), ('factor', Factor), ('source', str)],
)


class Spec(_Spec):
    def __new__(cls, unit, factor, source='derived'):
        return super().__new__(cls, unit, factor, source)


def foo() -> Spec:
    return Spec('ham', 'spam')

but this gets:
spec.py: note: In function "foo":
spec.py:18: error: Too few arguments for "Spec"

bug false-positive priority-0-high topic-named-tuple

Most helpful comment

@elazarg still fails in the same way

All 5 comments

I will see if this can be changed. At first glance it seems to be nontrivial.

(Personally I would prefer defining Spec as a namedtuple and use a make_spec function).

@elazarg still fails in the same way

Perhaps we can improve the lookup in checkmember,py#L443 - look up for __new__ first or otherwise mimic the runtime lookup.

(Raising priority to high since this affects some important internal code)

This now works correctly on master.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

glyph picture glyph  路  26Comments

ilevkivskyi picture ilevkivskyi  路  25Comments

snakescott picture snakescott  路  27Comments

timabbott picture timabbott  路  28Comments

gvanrossum picture gvanrossum  路  26Comments