Skip to content

mypy gets confused by dataclasses with partial type hints #7750

@wadetregaskis-linkedin

Description

@wadetregaskis-linkedin

This code:

from dataclasses import dataclass

@dataclass
class MypyBug:
    foo: str
    troz = 5

MypyBug(foo='abc', troz=11)

…yields the error message:

test.py:8: error: Unexpected keyword argument "troz" for "MypyBug"

That's simply wrong. troz is an argument to the autogenerated __init__. The code executes perfectly at runtime.

If you remove all type hints from MypyBug, mypy stops complaining. If you add type hints for all members of MypyBug, it also stops complaining.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions