Skip to content

Commit 72caae4

Browse files
authored
fix repr for single literals (#300)
1 parent d1feaa3 commit 72caae4

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

src/validators/literal.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,5 +332,10 @@ fn expected_repr_name(mut repr_args: Vec<String>) -> (String, String) {
332332
let name = format!("literal[{}]", repr_args.join(","));
333333
// unwrap is okay since we check the length in build at the top of this file
334334
let last_repr = repr_args.pop().unwrap();
335-
(format!("{} or {}", repr_args.join(", "), last_repr), name)
335+
let repr = if repr_args.is_empty() {
336+
last_repr
337+
} else {
338+
format!("{} or {}", repr_args.join(", "), last_repr)
339+
};
340+
(repr, name)
336341
}

tests/validators/test_literal.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import re
2+
from enum import Enum
23

34
import pytest
45

@@ -106,6 +107,7 @@ def test_literal_py_and_json(py_and_json: PyAndJson, kwarg_expected, input_value
106107
with pytest.raises(ValidationError, match=re.escape(expected.message)) as exc_info:
107108
v.validate_test(input_value)
108109
if expected.errors is not None:
110+
# debug(exc_info.value.errors())
109111
assert exc_info.value.errors() == expected.errors
110112
else:
111113
assert v.validate_test(input_value) == expected
@@ -123,13 +125,34 @@ def test_literal_py_and_json(py_and_json: PyAndJson, kwarg_expected, input_value
123125
Err("Input should be 1 or b'whatever' [kind=literal_error, input_value=3, input_type=int]"),
124126
id='wrong-general',
125127
),
128+
([b'bite'], b'bite', b'bite'),
129+
pytest.param(
130+
[b'bite'],
131+
'spoon',
132+
Err(
133+
"Input should be b'bite' [kind=literal_error, input_value='spoon', input_type=str]",
134+
[
135+
{
136+
'kind': 'literal_error',
137+
'loc': [],
138+
'message': "Input should be 1 or '1'",
139+
'input_value': '2',
140+
'context': {'expected': "1 or '1'"},
141+
}
142+
],
143+
),
144+
id='single-byte',
145+
),
126146
],
127147
)
128148
def test_literal_not_json(kwarg_expected, input_value, expected):
129149
v = SchemaValidator({'type': 'literal', 'expected': kwarg_expected})
130150
if isinstance(expected, Err):
131-
with pytest.raises(ValidationError, match=re.escape(expected.message)):
151+
with pytest.raises(ValidationError, match=re.escape(expected.message)) as exc_info:
132152
v.validate_python(input_value)
153+
if expected.errors is not None:
154+
# debug(exc_info.value.errors())
155+
assert exc_info.value.errors() == expected.errors
133156
else:
134157
assert v.validate_python(input_value) == expected
135158

@@ -170,3 +193,23 @@ def test_union():
170193
'input_value': 'c',
171194
},
172195
]
196+
197+
198+
def test_enum():
199+
class FooEnum(Enum):
200+
foo = 'foo_value'
201+
202+
v = SchemaValidator(core_schema.literal_schema(FooEnum.foo))
203+
assert v.validate_python(FooEnum.foo) == FooEnum.foo
204+
with pytest.raises(ValidationError) as exc_info:
205+
v.validate_python('foo_value')
206+
# insert_assert(exc_info.value.errors())
207+
assert exc_info.value.errors() == [
208+
{
209+
'kind': 'literal_error',
210+
'loc': [],
211+
'message': "Input should be <FooEnum.foo: 'foo_value'>",
212+
'input_value': 'foo_value',
213+
'context': {'expected': "<FooEnum.foo: 'foo_value'>"},
214+
}
215+
]

0 commit comments

Comments
 (0)