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
Pure-python implementation of dataclasses
module has differences regarding slots
#3791
Comments
Yup this is clearly a bug. Pydantic will fail validation for dataclasses that use import pytest
from dataclasses import dataclass
from unittest import skipIf
from pydantic import BaseModel
@skipIf(sys.version_info < (3, 10), "python version incompatible with 'slots' arg")
def test_pydantic_handling_stdlib_dataclass_slots() -> None:
"""
Test showing failure when using the slots kwarg available the stdlib dataclass decorator in python 3.10+
"""
@dataclass(slots=True)
class UserWithSlots:
username: str
group: str
@dataclass(slots=False)
class UserWithoutSlots:
username: str
group: str
class ModelThatRaises(BaseModel):
user: UserWithSlots
class ModelThatDoesntRaise(BaseModel):
user: UserWithoutSlots
with pytest.raises(ValidationError):
ModelThatRaises(user={"username": "MoisheZuchmir", "group": "admins"})
ModelThatDoesntRaise(user={"username": "MoisheZuchmir", "group": "admins"}) Reported in Starlite here: litestar-org/litestar#359 |
Have you tried on master? Might be fixed by #2557 |
@infohash can you try master? |
The code of @spookylukey works on |
Doesn't feel like it's going to be easy to add since we keep a kind of state when wrapping up the dataclass and a slotted dataclass doesn't allow mutations on those fields. We would need to mess up even more with init, which is something I really don't want to do. |
Agreed, I think we can live without slots for v1.10. |
I'm going to close this as I believe it is addressed in v2, but please comment here if I'm wrong. |
When using pydantic.dataclasses.dataclass,
typing.get_type_hints()
output on a Pydantic model includes__slots__
for the pure-Python version, but not for the Cython version.Test case:
$ pip install pydantic $ python test.py {} $ pip uninstall pydantic $ SKIP_CYTHON=1 pip install --no-binary pydantic pydantic $ python test.py {'__slots__': typing.Tuple[str, ...]}
Found this with 1.8.2 , but the issue is still there with latest version - 1.9.0.
I don't know if this is deliberate, or unavoidable, but it tripped me up, so I thought it was worth reporting.
Impact
I discovered this because we were experimenting with AWS Graviton instances, which are 64-bit ARM CPUs, where, for whatever reason, we are falling back to the pure-Python version.
Checks
The text was updated successfully, but these errors were encountered: