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
UUID with less than 8000 fails with NoneType #7355
Comments
I think impl BuildValidator for UuidValidator {
const EXPECTED_TYPE: &'static str = "uuid";
fn build(
schema: &PyDict,
config: Option<&PyDict>,
_definitions: &mut DefinitionsBuilder<CombinedValidator>,
) -> PyResult<CombinedValidator> {
let py = schema.py();
let version = schema.get_as::<u8>(intern!(py, "version"))?.map(Version::from); which only returns a value if the variant is RFC 4122 (returns @property
def version(self):
# The version bits are only meaningful for RFC 4122 UUIDs.
if self.variant == RFC_4122:
return int((self.int >> 76) & 0xf) So maybe depending on the uuid value, |
Thanks for reporting, sorry for the slow reply, I'll look at this today. |
Just to clarify, your model requires Indeed, the >>> from uuid import UUID
>>> uid = UUID('00000000-7fff-4000-7fff-000000000000')
>>> repr(uid.version)
'None' Modifying your example to ignore UUID version in the type check passes your tests. from uuid import UUID
from pydantic import BaseModel
class DemoModel(BaseModel):
uuid_field: UUID
def __str__(self):
return f'DemoModel(uuid_field = {self.uuid_field!r})'
def test(id: str | UUID):
print(f'{id=} type is {type(id)}')
object = DemoModel(uuid_field=id)
print(object)
test('00000000-8000-4000-8000-000000000000')
test('00000000-7fff-4000-7fff-000000000000')
test(UUID('00000000-8000-4000-8000-000000000000'))
test(UUID('00000000-7fff-4000-7fff-000000000000'))
However, this doesn't mean that there is no bug in Pydantic. Pydantic should provide meaningful error message that reflects the version check correctly. |
Initial Checks
Description
We had been using some 'dummy' UUID values, which are valid UUID4, as per https://www.freecodeformat.com/validate-uuid-guid.php for example, but pydantic does not seem to like them, we get a NoneType error.
I've attached an example script which passes 2 UUIDs to a test, first as str, which works, then as UUID, and this one fails:
00000000-7fff-4000-7fff-000000000000
When we run in our application, we get them from the DB layer and they come up as UUID() type, which was then failing.
It seems if the 2nd and 4th sections are less than 8000, the parsing fails, so 7fff fails but 8000 works.
The output we get is like this:
Example Code
Python, Pydantic & OS Version
The text was updated successfully, but these errors were encountered: