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

mypy error "Unexpected keyword argument" on pydantic dataclass InitVar field #5161

Closed
6 of 15 tasks
brianmedigate opened this issue Mar 9, 2023 · 8 comments · Fixed by #5167
Closed
6 of 15 tasks

mypy error "Unexpected keyword argument" on pydantic dataclass InitVar field #5161

brianmedigate opened this issue Mar 9, 2023 · 8 comments · Fixed by #5167
Assignees
Labels
bug V1 Bug related to Pydantic V1.X

Comments

@brianmedigate
Copy link
Contributor

brianmedigate commented Mar 9, 2023

Initial Checks

  • I have searched GitHub for a duplicate issue and I'm sure this is something new
  • I have searched Google & StackOverflow for a solution and couldn't find anything
  • I have read and followed the docs and still think this is a bug
  • I am confident that the issue is with pydantic (not my code, or another library in the ecosystem like FastAPI or mypy)

Description

Since upgrading to pydantic 1.10.6, mypy started complaining about a kwarg being passed to the constructor of a dataclass if the field is an InitVar.

I'm guessing it might be related to #5111

$ mypy test.py
test.py:10: error: Unexpected keyword argument "foo" for "MyDataclass"  [call-arg]
Found 1 error in 1 file (checked 1 source file)

Example Code

from pydantic.dataclasses import dataclass
from dataclasses import InitVar

@dataclass
class MyDataclass:
    foo: InitVar[str]
    bar: str

MyDataclass(foo="foo", bar="bar")

Python, Pydantic & OS Version

pydantic version: 1.10.6
            pydantic compiled: True
                 install path: /home/brian/repos/medigator/venv/lib/python3.8/site-packages/pydantic
               python version: 3.8.10 (default, Nov 14 2022, 12:59:47)  [GCC 9.4.0]
                     platform: Linux-5.15.0-67-generic-x86_64-with-glibc2.29
     optional deps. installed: ['dotenv', 'email-validator', 'typing-extensions']

Affected Components

@brianmedigate brianmedigate added bug V1 Bug related to Pydantic V1.X unconfirmed Bug not yet confirmed as valid/applicable labels Mar 9, 2023
@samuelcolvin
Copy link
Member

This won't be related to #2309, rather it will be related to #5111.

@cdce8p @dmontagu any ideas?

@samuelcolvin samuelcolvin removed the unconfirmed Bug not yet confirmed as valid/applicable label Mar 9, 2023
@brianmedigate
Copy link
Contributor Author

whoops, pasted the wrong link, thanks

@dmontagu
Copy link
Contributor

dmontagu commented Mar 9, 2023

@brianmedigate what is your mypy config? In particular, are you using the pydantic mypy plugin?

@dmontagu dmontagu self-assigned this Mar 9, 2023
@brianmedigate
Copy link
Contributor Author

yes

[mypy]
follow_imports = silent
ignore_missing_imports = True
warn_unused_ignores = True
check_untyped_defs = True
disallow_untyped_defs = True
strict_equality = True
warn_redundant_casts = True
python_version = 3.8
platform = linux
plugins = pydantic.mypy

[pydantic-mypy]
init_forbid_extra = True
init_typed = True
warn_required_dynamic_aliases = True
warn_untyped_fields = True

@dmontagu
Copy link
Contributor

dmontagu commented Mar 9, 2023

Okay, considering you are using the mypy plugin I think it should be readily within our power to fix this (and is probably our fault, to be honest 😅). I will try to take a look at this today or tomorrow.

@brianmedigate
Copy link
Contributor Author

great, thank you!

@cdce8p
Copy link
Contributor

cdce8p commented Mar 9, 2023

Possible fix: #5162
Until now, the plugin manually applied the dataclass transform to pydantic dataclasses. This isn't necessary anymore with mypy 1.1.1 and the @dataclass_transform decorator as it also applies the same transform.

In fact, doing it twice results in the InitVar issue seen here.

@brianmedigate
Copy link
Contributor Author

thank you @cdce8p @dmontagu and @samuelcolvin, you're awesome! 🥇

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

Successfully merging a pull request may close this issue.

4 participants