Skip to content
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

Two issues with subclassing namedtuple-generated class and overriding __new__ #1279

Closed
jstasiak opened this issue Mar 7, 2016 · 7 comments
Closed
Assignees
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-0-high topic-named-tuple

Comments

@jstasiak
Copy link
Contributor

jstasiak commented Mar 7, 2016

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:

@gvanrossum
Copy link
Member

Yeah, mypy should probably define NamedTuple in terms of __new__, not __init__. There are some deficiencies in mypy's __new__ support though that may make this not simple until they are first fixed.

@gvanrossum gvanrossum added the bug mypy got something wrong label Mar 7, 2016
@gvanrossum
Copy link
Member

In particular #1021 looks like a dependency for this. (And we haven't assigned that a milestone yet, so we can't assign a milestone here either.)

@gvanrossum
Copy link
Member

I propose to bump the priority here. This seems a real problem to some of our users.

@ilevkivskyi
Copy link
Member

I propose to bump the priority here. This seems a real problem to some of our users.

Sure, although TBH this will not help much, we are already overloaded with work.

@gvanrossum
Copy link
Member

Indeed. :-(

And aggressively critical users don't motivate me.

msullivan added a commit to python/typeshed that referenced this issue Sep 19, 2018
The __new__ method should suffice, and having both interferes with providing
a __new__ in namedtuples, which we want to do to fix
python/mypy#1279.
@msullivan msullivan self-assigned this Sep 19, 2018
@gvanrossum
Copy link
Member

I don't think python/typeshed#2467 actually fixed this (the auto fix triggered by accident).

@gvanrossum gvanrossum reopened this Sep 19, 2018
@JelleZijlstra
Copy link
Member

Oops, sorry for that and thanks for fixing.

yedpodtrzitko pushed a commit to yedpodtrzitko/typeshed that referenced this issue Jan 23, 2019
The __new__ method should suffice, and having both interferes with providing
a __new__ in namedtuples, which we want to do to fix
python/mypy#1279.
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