Skip to content

Commit

Permalink
fix repr for single literals (#300)
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelcolvin authored Oct 18, 2022
1 parent d1feaa3 commit 72caae4
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/validators/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,5 +332,10 @@ fn expected_repr_name(mut repr_args: Vec<String>) -> (String, String) {
let name = format!("literal[{}]", repr_args.join(","));
// unwrap is okay since we check the length in build at the top of this file
let last_repr = repr_args.pop().unwrap();
(format!("{} or {}", repr_args.join(", "), last_repr), name)
let repr = if repr_args.is_empty() {
last_repr
} else {
format!("{} or {}", repr_args.join(", "), last_repr)
};
(repr, name)
}
45 changes: 44 additions & 1 deletion tests/validators/test_literal.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
from enum import Enum

import pytest

Expand Down Expand Up @@ -106,6 +107,7 @@ def test_literal_py_and_json(py_and_json: PyAndJson, kwarg_expected, input_value
with pytest.raises(ValidationError, match=re.escape(expected.message)) as exc_info:
v.validate_test(input_value)
if expected.errors is not None:
# debug(exc_info.value.errors())
assert exc_info.value.errors() == expected.errors
else:
assert v.validate_test(input_value) == expected
Expand All @@ -123,13 +125,34 @@ def test_literal_py_and_json(py_and_json: PyAndJson, kwarg_expected, input_value
Err("Input should be 1 or b'whatever' [kind=literal_error, input_value=3, input_type=int]"),
id='wrong-general',
),
([b'bite'], b'bite', b'bite'),
pytest.param(
[b'bite'],
'spoon',
Err(
"Input should be b'bite' [kind=literal_error, input_value='spoon', input_type=str]",
[
{
'kind': 'literal_error',
'loc': [],
'message': "Input should be 1 or '1'",
'input_value': '2',
'context': {'expected': "1 or '1'"},
}
],
),
id='single-byte',
),
],
)
def test_literal_not_json(kwarg_expected, input_value, expected):
v = SchemaValidator({'type': 'literal', 'expected': kwarg_expected})
if isinstance(expected, Err):
with pytest.raises(ValidationError, match=re.escape(expected.message)):
with pytest.raises(ValidationError, match=re.escape(expected.message)) as exc_info:
v.validate_python(input_value)
if expected.errors is not None:
# debug(exc_info.value.errors())
assert exc_info.value.errors() == expected.errors
else:
assert v.validate_python(input_value) == expected

Expand Down Expand Up @@ -170,3 +193,23 @@ def test_union():
'input_value': 'c',
},
]


def test_enum():
class FooEnum(Enum):
foo = 'foo_value'

v = SchemaValidator(core_schema.literal_schema(FooEnum.foo))
assert v.validate_python(FooEnum.foo) == FooEnum.foo
with pytest.raises(ValidationError) as exc_info:
v.validate_python('foo_value')
# insert_assert(exc_info.value.errors())
assert exc_info.value.errors() == [
{
'kind': 'literal_error',
'loc': [],
'message': "Input should be <FooEnum.foo: 'foo_value'>",
'input_value': 'foo_value',
'context': {'expected': "<FooEnum.foo: 'foo_value'>"},
}
]

0 comments on commit 72caae4

Please sign in to comment.