Skip to content

Commit

Permalink
follow on from #501, fix "list_all_errors" (#506)
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelcolvin authored Mar 31, 2023
1 parent ecc8df4 commit 8b2be89
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 14 deletions.
10 changes: 6 additions & 4 deletions pydantic_core/_pydantic_core.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ else:
from typing import TypedDict

if sys.version_info < (3, 11):
from typing_extensions import Literal, TypeAlias
from typing_extensions import Literal, NotRequired, TypeAlias
else:
from typing import Literal, TypeAlias
from typing import Literal, NotRequired, TypeAlias

__all__ = (
'__version__',
Expand Down Expand Up @@ -210,8 +210,10 @@ class PydanticSerializationUnexpectedValue(ValueError):

class ErrorTypeInfo(TypedDict):
type: ErrorType
message_template: str
example_message: str
message_template_python: str
example_message_python: str
message_template_json: NotRequired[str]
example_message_json: NotRequired[str]
example_context: 'dict[str, str | int | float] | None'

class ArgsKwargs:
Expand Down
13 changes: 11 additions & 2 deletions src/errors/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,17 @@ pub fn list_all_errors(py: Python) -> PyResult<&PyList> {
if !matches!(error_type, ErrorType::CustomError { .. }) {
let d = PyDict::new(py);
d.set_item("type", error_type.to_string())?;
d.set_item("message_template", error_type.message_template_python())?;
d.set_item("example_message", error_type.render_message(py, &ErrorMode::Python)?)?;
let message_template_python = error_type.message_template_python();
d.set_item("message_template_python", message_template_python)?;
d.set_item(
"example_message_python",
error_type.render_message(py, &ErrorMode::Python)?,
)?;
let message_template_json = error_type.message_template_json();
if message_template_python != message_template_json {
d.set_item("message_template_json", message_template_json)?;
d.set_item("example_message_json", error_type.render_message(py, &ErrorMode::Json)?)?;
}
d.set_item("example_context", error_type.py_dict(py)?)?;
errors.push(d);
}
Expand Down
28 changes: 20 additions & 8 deletions tests/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,29 +364,41 @@ def test_all_errors():
assert errors[:4] == [
{
'type': 'no_such_attribute',
'message_template': "Object has no attribute '{attribute}'",
'example_message': "Object has no attribute ''",
'message_template_python': "Object has no attribute '{attribute}'",
'example_message_python': "Object has no attribute ''",
'example_context': {'attribute': ''},
},
{
'type': 'json_invalid',
'message_template': 'Invalid JSON: {error}',
'example_message': 'Invalid JSON: ',
'message_template_python': 'Invalid JSON: {error}',
'example_message_python': 'Invalid JSON: ',
'example_context': {'error': ''},
},
{
'type': 'json_type',
'message_template': 'JSON input should be string, bytes or bytearray',
'example_message': 'JSON input should be string, bytes or bytearray',
'message_template_python': 'JSON input should be string, bytes or bytearray',
'example_message_python': 'JSON input should be string, bytes or bytearray',
'example_context': None,
},
{
'type': 'recursion_loop',
'message_template': 'Recursion error - cyclic reference detected',
'example_message': 'Recursion error - cyclic reference detected',
'message_template_python': 'Recursion error - cyclic reference detected',
'example_message_python': 'Recursion error - cyclic reference detected',
'example_context': None,
},
]

none_required = next(e for e in errors if e['type'] == 'none_required')
# insert_assert(none_required)
assert none_required == {
'type': 'none_required',
'message_template_python': 'Input should be None',
'example_message_python': 'Input should be None',
'message_template_json': 'Input should be null',
'example_message_json': 'Input should be null',
'example_context': None,
}

error_types = [e['type'] for e in errors]
if error_types != list(core_schema.ErrorType.__args__):
literal = ''.join(f'\n {e!r},' for e in error_types)
Expand Down

0 comments on commit 8b2be89

Please sign in to comment.