You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to utilize fastapi + sqlalchemy which uses pydantic. My sqlalchemy column type is BINARY(16) mapping to that exact type in a MariaDB/MySQL database:
...which fails using your current code. You can find the test byte string above in the official python documentation. I've made a modification in validators to fallback to utilizing the bytes argument in UUID, and then the tests pass. I'm hoping you'd be interested in this fix, as its currently preventing us from using pydantic for a large project refactor. Thank you.
The text was updated successfully, but these errors were encountered:
I might create a subclass of UUID following the Custom Data Types doc to be able to move forward with your project refactor. Something like this should work:
In [1]: from uuid importUUID
In [2]: from pydantic import BaseModel
In [3]: classBinaryUUID(UUID):
...: @classmethod
...: def __get_validators__(cls):
...: yield cls.validate
...:
...: @classmethod
...: def validate(cls, v):
...: if not isinstance(v, bytes):
...: raise TypeError('bytes required')
...:
...: return cls(bytes=v)
...:
In [4]: classTestUuid(BaseModel):
...: uuid: UUID
...: buuid: BinaryUUID
...:
In [5]: TestUuid(uuid='12345678123456781234567812345678', buuid=b'\x12\x34\x56\x78'*4)
Out[5]: TestUuid(uuid=UUID('12345678-1234-5678-1234-567812345678'), buuid=BinaryUUID('12345678-1234-5678-1234-567812345678'))
And if you would like to submit a PR with the fix or post the modification you made here for discussion, that would be welcome.
I'm trying to utilize fastapi + sqlalchemy which uses pydantic. My sqlalchemy column type is BINARY(16) mapping to that exact type in a MariaDB/MySQL database:
uuid = Column(BINARY(16), nullable=False, unique=True)
In pydantic I'm defining as:
uuid: UUID
I get an error thrown that the "value is not a valid uuid (type=type_error.uuid)".
I've cloned pydantic and included a new test for the following:
('uuid_check', b'\x12\x34\x56\x78' * 4, UUID('12345678-1234-5678-1234-567812345678')),
...which fails using your current code. You can find the test byte string above in the official python documentation. I've made a modification in validators to fallback to utilizing the bytes argument in UUID, and then the tests pass. I'm hoping you'd be interested in this fix, as its currently preventing us from using pydantic for a large project refactor. Thank you.
The text was updated successfully, but these errors were encountered: