Skip to content

Commit 98f4a4a

Browse files
committed
refactor: error message uuid version
1 parent ff17506 commit 98f4a4a

File tree

8 files changed

+35
-66
lines changed

8 files changed

+35
-66
lines changed

python/pydantic_core/core_schema.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3906,7 +3906,7 @@ def definition_reference_schema(
39063906
'url_scheme',
39073907
'uuid_type',
39083908
'uuid_parsing',
3909-
'uuid_version_mismatch',
3909+
'uuid_version',
39103910
]
39113911

39123912

src/errors/types.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,8 @@ pub enum ErrorType {
313313
UuidParsing {
314314
error: String,
315315
},
316-
UuidVersionMismatch {
317-
version: usize,
318-
schema_version: usize,
316+
UuidVersion {
317+
expected_version: usize,
319318
},
320319
}
321320

@@ -461,8 +460,8 @@ impl ErrorType {
461460
Self::UrlTooLong { .. } => extract_context!(UrlTooLong, ctx, max_length: usize),
462461
Self::UrlScheme { .. } => extract_context!(UrlScheme, ctx, expected_schemes: String),
463462
Self::UuidParsing { .. } => extract_context!(UuidParsing, ctx, error: String),
464-
Self::UuidVersionMismatch { .. } => {
465-
extract_context!(UuidVersionMismatch, ctx, version: usize, schema_version: usize)
463+
Self::UuidVersion { .. } => {
464+
extract_context!(UuidVersion, ctx, expected_version: usize)
466465
}
467466
_ => {
468467
if ctx.is_some() {
@@ -570,7 +569,7 @@ impl ErrorType {
570569
Self::UrlScheme {..} => "URL scheme should be {expected_schemes}",
571570
Self::UuidType => "UUID input should be a string, bytes or UUID object",
572571
Self::UuidParsing { .. } => "Input should be a valid UUID, {error}",
573-
Self::UuidVersionMismatch { .. } => "UUID version {version} does not match expected version: {schema_version}"
572+
Self::UuidVersion { .. } => "UUID version {expected_version} expected"
574573

575574
}
576575
}
@@ -692,10 +691,7 @@ impl ErrorType {
692691
Self::UrlTooLong { max_length } => to_string_render!(tmpl, max_length),
693692
Self::UrlScheme { expected_schemes } => render!(tmpl, expected_schemes),
694693
Self::UuidParsing { error } => render!(tmpl, error),
695-
Self::UuidVersionMismatch {
696-
version,
697-
schema_version,
698-
} => to_string_render!(tmpl, version, schema_version),
694+
Self::UuidVersion { expected_version } => to_string_render!(tmpl, expected_version),
699695
_ => Ok(tmpl.to_string()),
700696
}
701697
}
@@ -758,10 +754,7 @@ impl ErrorType {
758754
Self::UrlScheme { expected_schemes } => py_dict!(py, expected_schemes),
759755

760756
Self::UuidParsing { error } => py_dict!(py, error),
761-
Self::UuidVersionMismatch {
762-
version,
763-
schema_version,
764-
} => py_dict!(py, version, schema_version),
757+
Self::UuidVersion { expected_version } => py_dict!(py, expected_version),
765758
_ => Ok(None),
766759
}
767760
}

src/input/input_python.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ impl<'a> Input<'a> for PyAny {
351351
Err(ValError::new(ErrorType::FloatType, self))
352352
}
353353
}
354+
354355
fn strict_dict(&'a self) -> ValResult<GenericMapping<'a>> {
355356
if let Ok(dict) = self.downcast::<PyDict>() {
356357
Ok(dict.into())

src/input/return_enums.rs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -853,27 +853,6 @@ impl<'a> EitherInt<'a> {
853853
}
854854
}
855855

856-
pub fn into_u128(self, py: Python<'a>) -> ValResult<'a, u128> {
857-
match self {
858-
EitherInt::I64(i) => match u128::try_from(i) {
859-
Ok(u) => Ok(u),
860-
Err(_) => Err(ValError::new(ErrorType::IntParsingSize, i.into_py(py).into_ref(py))),
861-
},
862-
EitherInt::U64(u) => match u128::try_from(u) {
863-
Ok(u) => Ok(u),
864-
Err(_) => Err(ValError::new(ErrorType::IntParsingSize, u.into_py(py).into_ref(py))),
865-
},
866-
EitherInt::BigInt(u) => match u128::try_from(u) {
867-
Ok(u) => Ok(u),
868-
Err(e) => Err(ValError::new(
869-
ErrorType::IntParsingSize,
870-
e.into_original().into_py(py).into_ref(py),
871-
)),
872-
},
873-
EitherInt::Py(i) => i.extract().map_err(|_| ValError::new(ErrorType::IntParsingSize, i)),
874-
}
875-
}
876-
877856
pub fn as_int(&self) -> ValResult<'a, Int> {
878857
match self {
879858
EitherInt::I64(i) => Ok(Int::I64(*i)),

src/validators/uuid.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,7 @@ impl Validator for UuidValidator {
9797
let py_input_version: usize = py_input.getattr(intern!(py, "version"))?.extract()?;
9898
let expected_version = usize::from(expected_version);
9999
if expected_version != py_input_version {
100-
return Err(ValError::new(
101-
ErrorType::UuidVersionMismatch {
102-
version: py_input_version,
103-
schema_version: expected_version,
104-
},
105-
input,
106-
));
100+
return Err(ValError::new(ErrorType::UuidVersion { expected_version }, input));
107101
}
108102
}
109103
Ok(py_input.to_object(py))
@@ -161,13 +155,7 @@ impl UuidValidator {
161155
let v1 = uuid.get_version_num();
162156
let expected_version = usize::from(expected_version);
163157
if v1 != expected_version {
164-
return Err(ValError::new(
165-
ErrorType::UuidVersionMismatch {
166-
version: v1,
167-
schema_version: expected_version,
168-
},
169-
input,
170-
));
158+
return Err(ValError::new(ErrorType::UuidVersion { expected_version }, input));
171159
}
172160
};
173161
Ok(uuid)

tests/benchmarks/test_micro_benchmarks.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -694,10 +694,22 @@ def validator(self):
694694
@pytest.fixture(scope='class')
695695
def pydantic_validator(self):
696696
def to_UUID(v: Any) -> UUID:
697+
if isinstance(v, UUID):
698+
return v
697699
try:
698-
return UUID(v)
699-
except Exception as e:
700-
raise PydanticCustomError('uuid_parsing', 'Input should be a valid uuid') from e
700+
if isinstance(v, str):
701+
return UUID(v)
702+
else:
703+
try:
704+
return UUID(v.decode())
705+
except ValueError:
706+
# 16 bytes in big-endian order as the bytes argument fail
707+
# the above check
708+
return UUID(bytes=v)
709+
except ValueError:
710+
raise PydanticCustomError(
711+
'uuid_parsing', 'Input should be a valid UUID, unable to parse string as an UUID'
712+
)
701713

702714
json_schema = core_schema.no_info_after_validator_function(
703715
to_UUID, core_schema.str_schema(strict=True, strip_whitespace=True)

tests/test_errors.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -284,11 +284,7 @@ def f(input_value, info):
284284
('url_scheme', 'URL scheme should be "foo", "bar" or "spam"', {'expected_schemes': '"foo", "bar" or "spam"'}),
285285
('uuid_type', 'UUID input should be a string, bytes or UUID object', None),
286286
('uuid_parsing', 'Input should be a valid UUID, Foobar', {'error': 'Foobar'}),
287-
(
288-
'uuid_version_mismatch',
289-
'UUID version 42 does not match expected version: 41',
290-
{'version': 42, 'schema_version': 41},
291-
),
287+
('uuid_version', 'UUID version 42 expected', {'expected_version': 42}),
292288
]
293289

294290

tests/validators/test_uuid.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,14 @@ def test_uuid_strict(input_value, expected):
118118
('0e7ac198-9acd-4c0c-b4b4-761974bf71d7', 4, UUID('0e7ac198-9acd-4c0c-b4b4-761974bf71d7')),
119119
(UUID('0e7ac198-9acd-4c0c-b4b4-761974bf71d7'), 4, UUID('0e7ac198-9acd-4c0c-b4b4-761974bf71d7')),
120120
# Invalid UUIDs
121-
('a6cc5730-2261-11ee-9c43-2eb5a363657c', 5, Err('UUID version 1 does not match expected version: 5')),
122-
(UUID('a6cc5730-2261-11ee-9c43-2eb5a363657c'), 5, Err('UUID version 1 does not match expected version: 5')),
123-
('04e4aeb3-8f20-30d0-8852-d295e1265eed', 4, Err('UUID version 3 does not match expected version: 4')),
124-
(UUID('04e4aeb3-8f20-30d0-8852-d295e1265eed'), 4, Err('UUID version 3 does not match expected version: 4')),
125-
('0e7ac198-9acd-4c0c-b4b4-761974bf71d7', 3, Err('UUID version 4 does not match expected version: 3')),
126-
(UUID('0e7ac198-9acd-4c0c-b4b4-761974bf71d7'), 3, Err('UUID version 4 does not match expected version: 3')),
127-
('08ed0736-fb95-5cc5-85ed-37e4f3df9b29', 1, Err('UUID version 5 does not match expected version: 1')),
128-
(UUID('08ed0736-fb95-5cc5-85ed-37e4f3df9b29'), 1, Err('UUID version 5 does not match expected version: 1')),
121+
('a6cc5730-2261-11ee-9c43-2eb5a363657c', 5, Err('UUID version 5 expected')),
122+
(UUID('a6cc5730-2261-11ee-9c43-2eb5a363657c'), 5, Err('UUID version 5 expected')),
123+
('04e4aeb3-8f20-30d0-8852-d295e1265eed', 4, Err('UUID version 4 expected')),
124+
(UUID('04e4aeb3-8f20-30d0-8852-d295e1265eed'), 4, Err('UUID version 4 expected')),
125+
('0e7ac198-9acd-4c0c-b4b4-761974bf71d7', 3, Err('UUID version 3 expected')),
126+
(UUID('0e7ac198-9acd-4c0c-b4b4-761974bf71d7'), 3, Err('UUID version 3 expected')),
127+
('08ed0736-fb95-5cc5-85ed-37e4f3df9b29', 1, Err('UUID version 1 expected')),
128+
(UUID('08ed0736-fb95-5cc5-85ed-37e4f3df9b29'), 1, Err('UUID version 1 expected')),
129129
],
130130
)
131131
def test_uuid_version(input_value, version, expected):

0 commit comments

Comments
 (0)