mypy not recognizing valid arguments to namedtuple constructor

Created on 12 Feb 2017  路  9Comments  路  Source: python/mypy

````python
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"

bug false-positive priority-1-normal topic-named-tuple

Most helpful comment

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"

All 9 comments

This is very closely related to https://github.com/python/mypy/issues/2683.

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"

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

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

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

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

@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']

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

This is a duplicate of #1279.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mr-rodgers picture mr-rodgers  路  29Comments

tjltjl picture tjltjl  路  35Comments

JukkaL picture JukkaL  路  30Comments

ilevkivskyi picture ilevkivskyi  路  25Comments

MarkCBell picture MarkCBell  路  25Comments