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

mypy not recognizing valid arguments to namedtuple constructor #2852

Closed
sjaensch opened this issue Feb 12, 2017 · 9 comments
Closed

mypy not recognizing valid arguments to namedtuple constructor #2852

sjaensch opened this issue Feb 12, 2017 · 9 comments
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal topic-named-tuple

Comments

@sjaensch
Copy link

from collections import namedtuple

class Test(namedtuple('BaseNamedTuple', ('field1', 'field2'))):
    def __new__(cls, field1, field2, new_arg):
        print('new_arg = ', new_arg)
        return super().__new__(cls, field1, field2)


Test(field1=1, field2=2, new_arg=False)

mypy error: Unexpected keyword argument "new_arg" for "Test"

@JelleZijlstra
Copy link
Member

This is very closely related to #2683.

@gvanrossum gvanrossum added the bug mypy got something wrong label Feb 13, 2017
@JukkaL JukkaL added the false-positive mypy gave an error on correct code label May 19, 2018
@inoryy
Copy link

inoryy commented Jul 16, 2018

Same with Python 3.7 Data class:

@dataclass
class A:
    a: int
    b: float = 0.0

a = A(1, b=2.0)

mypy output:

error: Too many arguments for "A"
error: Unexpected keyword argument "b" for "A"

@JelleZijlstra
Copy link
Member

Dataclasses should work correctly with the most recent release. Did you upgrade?

@inoryy
Copy link

inoryy commented Jul 16, 2018

@JelleZijlstra thanks! Can confirm it is fixed for dataclasses in mypy 0.620.

@bbarker
Copy link

bbarker commented Jul 23, 2018

I'm also experiencing this with dataclasses, much like in @inoryy 's example, though when I came across it, 'b' was defined in a superclass of 'A' (also a dataclass in my case).

@JelleZijlstra
Copy link
Member

@bbarker could you be more specific, and perhaps open a new issue? Relevant information includes your mypy version and the exact code you're checking and error you are getting.

@bbarker
Copy link

bbarker commented Jul 23, 2018

@JelleZijlstra sure, I will paste here for now.

Mypy version is 0.620

Code where the error occurs:

        # see https://github.com/python/mypy/issues/2852 for type ignores:
        self.primary_archive = Archive(   # type: ignore
            id=taxonomy.CATEGORIES[self.primary_category.id]['in_archive'])
        self.primary_group = Group(  # type: ignore
            id=taxonomy.ARCHIVES[self.primary_archive.id]['in_group'])

dataclass defs:

@dataclass
class Category():
    """Represents an arXiv category."""

    id: str = field(default_factory=str)
    """The category identifier (e.g. cs.DL)."""
    name: str = field(init=False)
    """The name of the category (e.g. Digital Libraries)."""

    def __post_init__(self) -> None:
        """Get the full category name."""
        if self.id in taxonomy.CATEGORIES:
            self.name = taxonomy.CATEGORIES[self.id]['name']


@dataclass
class Archive(Category):
    """Represents an arXiv archive."""

    def __post_init__(self) -> None:
        """Get the full archive name."""
        if self.id in taxonomy.ARCHIVES:
            self.name = taxonomy.ARCHIVES[self.id]['name']

@JelleZijlstra
Copy link
Member

Thanks! That sounds like we're not treating inheritance between dataclasses right, which is a very separate issue from the original one here. Could you open a new issue?

(I will rename this issue to clarify that it's about namedtuple.)

@JelleZijlstra JelleZijlstra changed the title mypy not recognizing valid arguments to object constructor mypy not recognizing valid arguments to namedtuple constructor Jul 23, 2018
@JelleZijlstra
Copy link
Member

This is a duplicate of #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-1-normal topic-named-tuple
Projects
None yet
Development

No branches or pull requests

7 participants