Skip to content

Commit

Permalink
Add CoreSchemaType Literal (#389)
Browse files Browse the repository at this point in the history
Add CoreSchemaType Literal
  • Loading branch information
dmontagu authored Feb 9, 2023
1 parent 27a3367 commit 7dcf502
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 9 deletions.
3 changes: 2 additions & 1 deletion pydantic_core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
__version__,
to_json,
)
from .core_schema import CoreConfig, CoreSchema
from .core_schema import CoreConfig, CoreSchema, CoreSchemaType

__all__ = (
'__version__',
'CoreConfig',
'CoreSchema',
'CoreSchemaType',
'SchemaValidator',
'SchemaSerializer',
'Url',
Expand Down
42 changes: 42 additions & 0 deletions pydantic_core/core_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -2587,6 +2587,48 @@ def multi_host_url_schema(
MultiHostUrlSchema,
]

# to update this, call `pytest -k test_core_schema_type_literal` and copy the output
CoreSchemaType = Literal[
'any',
'none',
'bool',
'int',
'float',
'str',
'bytes',
'date',
'time',
'datetime',
'timedelta',
'literal',
'is-instance',
'is-subclass',
'callable',
'list',
'tuple',
'set',
'frozenset',
'generator',
'dict',
'function',
'default',
'nullable',
'union',
'tagged-union',
'chain',
'lax-or-strict',
'typed-dict',
'model',
'arguments',
'call',
'recursive-ref',
'custom-error',
'json',
'url',
'multi-host-url',
]


# used in _pydantic_core.pyi::PydanticKnownError
# to update this, call `pytest -k test_all_errors` and copy the output
ErrorType = Literal[
Expand Down
8 changes: 4 additions & 4 deletions tests/serializers/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,19 +164,19 @@ def append_args(value, info):
)
assert s.to_python(123) == (
"123 info=SerializationInfo(include=None, exclude=None, mode='python', by_alias=True, exclude_unset=False, "
"exclude_defaults=False, exclude_none=False, round_trip=False)"
'exclude_defaults=False, exclude_none=False, round_trip=False)'
)
assert s.to_python(123, mode='other') == (
"123 info=SerializationInfo(include=None, exclude=None, mode='other', by_alias=True, exclude_unset=False, "
"exclude_defaults=False, exclude_none=False, round_trip=False)"
'exclude_defaults=False, exclude_none=False, round_trip=False)'
)
assert s.to_python(123, include={'x'}) == (
"123 info=SerializationInfo(include={'x'}, exclude=None, mode='python', by_alias=True, exclude_unset=False, "
"exclude_defaults=False, exclude_none=False, round_trip=False)"
'exclude_defaults=False, exclude_none=False, round_trip=False)'
)
assert s.to_python(123, mode='json', exclude={1: {2}}) == (
"123 info=SerializationInfo(include=None, exclude={1: {2}}, mode='json', by_alias=True, exclude_unset=False, "
"exclude_defaults=False, exclude_none=False, round_trip=False)"
'exclude_defaults=False, exclude_none=False, round_trip=False)'
)
assert s.to_json(123) == (
b'"123 info=SerializationInfo(include=None, exclude=None, mode=\'json\', by_alias=True, exclude_unset=False, '
Expand Down
18 changes: 17 additions & 1 deletion tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
from pathlib import Path

import pytest
from typing_extensions import get_args

from pydantic_core import core_schema
from pydantic_core import CoreSchema, CoreSchemaType, core_schema
from pydantic_core._pydantic_core import (
SchemaError,
SchemaValidator,
Expand Down Expand Up @@ -178,3 +179,18 @@ def test_all_errors():
literal = ''.join(f'\n {e!r},' for e in error_types)
print(f'python code (end of pydantic_core/core_schema.py):\n\nErrorType = Literal[{literal}\n]')
pytest.fail('core_schema.ErrorType needs to be updated')


def test_core_schema_type_literal():
def get_type_value(schema):
type_ = schema.__annotations__['type']
m = re.search(r"Literal\['(.+?)']", type_.__forward_arg__)
assert m, f'Unknown schema type: {type_}'
return m.group(1)

schema_types = tuple(get_type_value(x) for x in CoreSchema.__args__)
schema_types = tuple(dict.fromkeys(schema_types)) # remove duplicates while preserving order
if get_args(CoreSchemaType) != schema_types:
literal = ''.join(f'\n {e!r},' for e in schema_types)
print(f'python code (near end of pydantic_core/core_schema.py):\n\nCoreSchemaType = Literal[{literal}\n]')
pytest.fail('core_schema.CoreSchemaType needs to be updated')
6 changes: 3 additions & 3 deletions tests/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def f(__input: Any, __info: core_schema.SerializationInfo) -> str:
)
assert s.to_python(123) == (
"SerializationInfo(include=None, exclude=None, mode='python', by_alias=True, exclude_unset=False, "
"exclude_defaults=False, exclude_none=False, round_trip=False)"
'exclude_defaults=False, exclude_none=False, round_trip=False)'
)


Expand All @@ -204,7 +204,7 @@ def f(__input: Any, __serialize: core_schema.SerializeWrapHandler, __info: core_
)
# insert_assert(s.to_python(123, mode='json'))
assert s.to_python(123, mode='json') == (
"SerializationCallable(serializer=str) "
'SerializationCallable(serializer=str) '
"SerializationInfo(include=None, exclude=None, mode='json', by_alias=True, exclude_unset=False, "
"exclude_defaults=False, exclude_none=False, round_trip=False)"
'exclude_defaults=False, exclude_none=False, round_trip=False)'
)

0 comments on commit 7dcf502

Please sign in to comment.