Skip to content

Incomplete and incorrect stub generation of a dataclass. #16811

Closed
@nknavasto

Description

@nknavasto

Bug Report
Stubs for a dataclass are incomplete and depending on the use case also incorrect. Consider the following dataclass:

# module.py
import dataclasses


@dataclasses.dataclass
class A:
    foo: str
    bar: str = "default"
    baz: str = dataclasses.field(default="hidden default", init=False)

    def method(self, a: int) -> float:
        return 0.0

The resulting generated stubs are

import dataclasses

@dataclasses.dataclass
class A:
    foo: str
    bar: str = ...
    baz: str = ...
    def method(self, a: int) -> float: ...
    def __init__(self, foo, bar) -> None: ...

As you can see, the method method received the correct type hints. However, the __init__ method did not. It correctly removed the baz parameter, as this has the init=False flag, but no type hints are present. Also, it does not reflect the fact, that bar is only an optional parameter. This leads to incorrect type hints when using this dataclass.

To Reproduce
Run stubgen module.py.

Expected Behavior

The expected behavior could look something like this:

import dataclasses

@dataclasses.dataclass
class A:
    foo: str
    bar: str = ...
    baz: str = ...
    def method(self, a: int) -> float: ...
    def __init__(self, foo: str, bar: str = "default") -> None: ...

Your Environment

  • Mypy version used: 1.8.0
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.8

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions