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

NamedTuple subclasses do not pick up __new__'s signature #5194

Closed
toolforger opened this issue Jun 11, 2018 · 1 comment
Closed

NamedTuple subclasses do not pick up __new__'s signature #5194

toolforger opened this issue Jun 11, 2018 · 1 comment

Comments

@toolforger
Copy link

toolforger commented Jun 11, 2018

mypy inspects the field names instead of looking at the signature of __new__.
(I didn't try __init__, that wasn't my use case.)

Mypy 0.610, Python 3.6.3.
Not tried Mypy from git master (haven't checked how to do that).

Code:

#! /usr/bin/env python3

from typing import List, NamedTuple

class Command(NamedTuple):
    subcommands: List[str]

class HelpCommand(Command):
    def __new__(cls):
        return Command.__new__(cls, subcommands=[])

HelpCommand() # fails in mypy, works in Python
HelpCommand(subcommands=[]) # works in mypy, fails in Python

Command: python3 -m mypy typetest.py

Output:

typetest.py:12: error: Too few arguments for "HelpCommand"

Python has a different idea about what should work and what shouldn't:

$ ./typetest.py 
Traceback (most recent call last):
  File "./typetest.py", line 12, in <module>
    HelpCommand(subcommands=[]) # works
TypeError: __new__() got an unexpected keyword argument 'subcommands'
@ethanhs
Copy link
Collaborator

ethanhs commented Jun 11, 2018

Closing as a duplicate of #1279.

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