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

Incorrect number of arguments expected when dataclass is applied using __init_subclass__ #11056

Open
flyte opened this issue Sep 4, 2021 · 0 comments

Comments

@flyte
Copy link

flyte commented Sep 4, 2021

Bug Report

A bit obscure and perhaps not a supported pattern, but using __init_subclass__ to turn subclasses into dataclasses in order to remove the need for library users to use the @dataclass decorator makes mypy confused about how many args the subclass' constructor expects.

To Reproduce

https://mypy-play.net/?mypy=latest&python=3.10&gist=d5900693474f425fc04dc5d36bd0eb2d

from dataclasses import dataclass


class Event:
    def __init_subclass__(cls):
        dataclass(cls)


class BaseEvent(Event):
    base_field: str


class SubEvent(BaseEvent):
    sub_field: str
    
    
SubEvent("base", "sub")

Expected Behavior

No errors from mypy.

Actual Behavior

In [1]: from dataclasses import dataclass
    ..: 
    ..: 
    ..: class Event:
    ..:     def __init_subclass__(cls):
    ..:         dataclass(cls)
    ..: 
    ..: 
    ..: class BaseEvent(Event):
    ..:     base_field: str
    ..: 
    ..: 
    ..: class SubEvent(BaseEvent):
    ..:     sub_field: str
    ..: 
    ..: 
    ..: SubEvent("base", "sub")
    ..: 
Out[1]: SubEvent(base_field='base', sub_field='sub')
main.py:17: error: Too many arguments for "SubEvent"
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 0.910
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.10
  • Operating system and version: n/a (playground)
@flyte flyte added the bug mypy got something wrong label Sep 4, 2021
@AlexWaygood AlexWaygood added feature topic-dataclasses priority-2-low and removed bug mypy got something wrong labels Mar 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants