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

dataclass decorator: unexpected keyword argument using mypy #3647

Closed
BayerSe opened this issue Jan 10, 2022 · 3 comments
Closed

dataclass decorator: unexpected keyword argument using mypy #3647

BayerSe opened this issue Jan 10, 2022 · 3 comments
Labels
bug V1 Bug related to Pydantic V1.X

Comments

@BayerSe
Copy link

BayerSe commented Jan 10, 2022

Bug

Output of python -c "import pydantic.utils; print(pydantic.utils.version_info())":

             pydantic version: 1.9.0
            pydantic compiled: True
                 install path: /home/bay2rng/PycharmProjects/semisched/packages/scheduler/.venv/lib/python3.9/site-packages/pydantic
               python version: 3.9.9 (main, Nov 16 2021, 03:08:02)  [GCC 9.3.0]
                     platform: Linux-5.4.0-90-generic-x86_64-with-glibc2.31
     optional deps. installed: ['typing-extensions']

While translating some models implemented using Python's standard dataclass decorator into Pydantic dataclasses, mypy 0.931 started complaining about unexpected keyword arguments

from dataclasses import dataclass
from pydantic.dataclasses import dataclass as pd_dataclass


@dataclass
class ModelA:
    parameter: int


@pd_dataclass
class ModelB:
    parameter: int


a = ModelA(parameter=1)
b = ModelB(parameter=1)

Output of mypy test.py

test.py:16: error: Unexpected keyword argument "parameter" for "ModelB"
@BayerSe BayerSe added the bug V1 Bug related to Pydantic V1.X label Jan 10, 2022
@pecigonzalo
Copy link

It seems the return type has some problem, and likely affecting VSCode auto-completion as well which I believe was meant to be fixed in #2721.

If you hover "b" in this example, you will see its type is detected as (variable) b: Dataclass.

@ajkaijanaho
Copy link

The dataclass decorator is annotated as returning a Dataclass object, which defines the Pydantic dataclass additions, losing any information about the original class that is being decorated. The dataclass transformation defined in #2721 is only applied to ModelMetaclass, which is not the metaclass of dataclasses.

I know of no way to annotate correctly a decorator that modifies a class's methods and fields. You either do what Pydantic does or you annotate like your decorator does not change the class.

@adriangb
Copy link
Member

This works on both v1.10.X and v2 without issues even with mypy --strict, so it seems like it's been resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V1 Bug related to Pydantic V1.X
Projects
None yet
Development

No branches or pull requests

4 participants