Skip to content

NamedTuple subclass with default arguments doesn't type check #2683

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
graingert opened this issue Jan 12, 2017 · 5 comments
Closed

NamedTuple subclass with default arguments doesn't type check #2683

graingert opened this issue Jan 12, 2017 · 5 comments
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-0-high topic-named-tuple

Comments

@graingert
Copy link
Contributor

graingert commented Jan 12, 2017

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"

@graingert graingert changed the title NamedTuple subclass with default arguments NamedTuple subclass with default arguments doesn't type check Jan 12, 2017
@elazarg
Copy link
Contributor

elazarg commented Jan 12, 2017

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).

@graingert
Copy link
Contributor Author

@elazarg still fails in the same way

@elazarg
Copy link
Contributor

elazarg commented Jan 12, 2017

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

@ilevkivskyi
Copy link
Member

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

@JukkaL JukkaL added the false-positive mypy gave an error on correct code label May 19, 2018
@ilevkivskyi
Copy link
Member

This now works correctly on master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-0-high topic-named-tuple
Projects
None yet
Development

No branches or pull requests

5 participants