-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Fix crash with dataclass_transform
cache
#14734
Conversation
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
This should also be fixed by #14695 fwiw (I took a slightly different approach, but it’s more or less the same fix) |
Out of curiosity, how did you trigger this? Using the master version of MyPy with a library that uses dataclass_transform? |
It's only an issue in the compiled version of mypy. I regularly install the latest one from the mypy wheels repo. In this case I noticed the error while debugging a issues with pydantic models and dataclasses. The recent Tested with pydantic from __future__ import annotations
import dataclasses
import pydantic.dataclasses
from pydantic import BaseModel, Field
from typing_extensions import reveal_type
class Event(BaseModel):
id: int | None = Field(None, alias="id")
Event() # Error
reveal_type(1)
@pydantic.dataclasses.dataclass
class Info:
status: str
def func(info: Info) -> None:
dataclasses.asdict(info) # Error
func(Info(status="Hello"))
# mypy.ini
[mypy]
python_version = 3.10
plugins = pydantic.mypy
[pydantic-mypy]
init_forbid_extra = true
init_typed = true
warn_required_dynamic_aliases = true
warn_untyped_fields = true
|
Yeah, Pydantic will surface a lot of errors now that it didn't before the support for
|
Thanks for taking a look. The As for the other two, pydantic has a custom mypy plugin that used to handle it just fine. mypy/mypy/plugins/dataclasses.py Lines 651 to 657 in c03e979
The Lines 476 to 483 in c03e979
Because of that, the pydantic plugin isn't used which would normally have handled the |
This is very helpful info; thank you for sharing! |
I found a possible solution / workaround for the second issue (with I'm preparing a PR for it at the moment. |
Opened pydantic/pydantic#5077 to fix the pydantic plugin. Remaining Todo's:
|
FYI #14752 should fix |
now that #14695 has landed, can you confirm whether the issue here is fixed for you? |
Took a moment as I needed to wait until the wheels were build. As expected, #14695 does already resolve the issue. I'll go ahead and close this one then. Thanks @wesleywright 👍🏻 |
When serializing a
tuple[...]
field to json, it's automatically converted to[...]
.Thus when reading the cache a list would get assigned to a class field specified as tuple which causes a crash with the compiled version of mypy.
Issue was added in #14667 as it started to parse the
field_specifiers
argument./cc: @wesleywright