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

"Unexpected keyword argument" for custom dataclasses #6239

Closed
samuelcolvin opened this Issue Jan 22, 2019 · 4 comments

Comments

Projects
None yet
2 participants
@samuelcolvin
Copy link
Contributor

samuelcolvin commented Jan 22, 2019

I'm wonder if I can get pydantic's custom dataclasses to work with mypy, original issue: samuelcolvin/pydantic#280.

The following code passes mpy validation:

from dataclasses import dataclass
# from pydantic.dataclasses import dataclass

@dataclass
class Test:
    test_int: int
    test_str: str

Test(test_int=42, test_str='foobar')

However, if I change the import to use from pydantic.dataclasses import dataclass I get

> mypy --follow-imports=skip test.py
test.py:9: error: Unexpected keyword argument "test_int" for "Test"
test.py:9: error: Unexpected keyword argument "test_str" for "Test

Is this because mypy does somethign special when it seems the standard library dataclasses import, or because of something erroueous in pydantic's dataclass implementation (code here).

Either way, is there anything I can do to fix this in pydantic?

The class returned by pydantic's @dataclass decorator should be (almost) identical to the standard lib decorator as it uses dataclasses._process_class() internally.

@ilevkivskyi

This comment has been minimized.

Copy link
Collaborator

ilevkivskyi commented Jan 22, 2019

This is essentially a duplicate of #5406

A workaround is:

from typing import TYPE_CHECKING
if TYPE_CHECKING:
    from dataclasses import dataclass
else:
    from pydantic.dataclasses import dataclass

(assuming API is the same). You can do the same conditional definition trick directly in pydantic if it supports typing using PEP 561.

@samuelcolvin

This comment has been minimized.

Copy link
Contributor Author

samuelcolvin commented Jan 22, 2019

Thanks, that almost fixes everything.

pydantic's dataclass can also take a config argument. I'm guessing if that argument is used the above workaround wouldn't work. Is there anything we can do to cope with config?

@ilevkivskyi

This comment has been minimized.

Copy link
Collaborator

ilevkivskyi commented Jan 22, 2019

Is there anything we can do to cope with config?

Unfortunately, there is no easy fix. You can however just place couple of # type: ignores on lines where config is used.

@samuelcolvin

This comment has been minimized.

Copy link
Contributor Author

samuelcolvin commented Jan 22, 2019

I guess just have to use @dataclass(config=Config) # type: ignore and everything else will work fine.

@samuelcolvin samuelcolvin referenced this issue Jan 26, 2019

Merged

add type hints #373

4 of 4 tasks complete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment