Skip to content

Wrong inference of dataclasses with custom __init__ and varargs. #6399

@mvaled

Description

@mvaled

Small case to reproduce:

from typing import Tuple
from dataclasses import dataclass

@dataclass
class Foo:
    spams: Tuple[int, ...]

    def __init__(self, *spams: int) -> None:
        self.spams = spams

def build_foo(*spams: int) -> Foo:
    return Foo(*spams)

Checking that program with mypy 0.670 (Python 3.6), results in:

bug.py:13: error: Argument 1 to "Foo" has incompatible type "*Tuple[int, ...]"; expected "Tuple[int, ...]"

Without the @dataclass decorator the program passes. It seems that the dataclass is hiding the actual __init__.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongpriority-2-lowtopic-pluginsThe plugin API and ideas for new plugins

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions