8000 Add CoreSchemaType Literal (#389) · pydantic/pydantic-core@7dcf502 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7dcf502

Browse files
authored
Add CoreSchemaType Literal (#389)
Add CoreSchemaType Literal
1 parent 27a3367 commit 7dcf502

File tree

5 files changed

+68
-9
lines changed

5 files changed

+68
-9
lines changed

pydantic_core/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@
1313
__version__,
1414
to_json,
1515
)
16-
from .core_schema import CoreConfig, CoreSchema
16+
from .core_schema import CoreConfig, CoreSchema, CoreSchemaType
1717

1818
__all__ = (
1919
'__version__',
2020
'CoreConfig',
2121
'CoreSchema',
22+
'CoreSchemaType',
2223
'SchemaValidator',
2324
'SchemaSerializer',
2425
'Url',

pydantic_core/core_schema.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2587,6 +2587,48 @@ def multi_host_url_schema(
25872587
MultiHostUrlSchema,
25882588
]
25892589

2590+
# to update this, call `pytest -k test_core_schema_type_literal` and copy the output
2591+
CoreSchemaType = Literal[
2592+
'any',
2593+
'none',
2594+
'bool',
2595+
'int',
2596+
'float',
2597+
'str',
2598+
'bytes',
2599+
'date',
2600+
'time',
2601+
'datetime&# 8000 39;,
2602+
'timedelta',
2603+
'literal',
2604+
'is-instance',
2605+
'is-subclass',
2606+
'callable',
2607+
'list',
2608+
'tuple',
2609+
'set',
2610+
'frozenset',
2611+
'generator',
2612+
'dict',
2613+
'function',
2614+
'default',
2615+
'nullable',
2616+
'union',
2617+
'tagged-union',
2618+
'chain',
2619+
'lax-or-strict',
2620+
'typed-dict',
2621+
'model',
2622+
'arguments',
2623+
'call',
2624+
'recursive-ref',
2625+
'custom-error',
2626+
'json',
2627+
'url',
2628+
'multi-host-url',
2629+
]
2630+
2631+
25902632
# used in _pydantic_core.pyi::PydanticKnownError
25912633
# to update this, call `pytest -k test_all_errors` and copy the output
25922634
ErrorType = Literal[

tests/serializers/test_functions.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,19 +164,19 @@ def append_args(value, info):
164164
)
165165
assert s.to_python(123) == (
166166
"123 info=SerializationInfo(include=None, exclude=None, mode='python', by_alias=True, exclude_unset=False, "
167-
"exclude_defaults=False, exclude_none=False, round_trip=False)"
167+
'exclude_defaults=False, exclude_none=False, round_trip=False)'
168168
)
169169
assert s.to_python(123, mode='other') == (
170170
"123 info=SerializationInfo(include=None, exclude=None, mode='other', by_alias=True, exclude_unset=False, "
171-
"exclude_defaults=False, exclude_none=False, round_trip=False)"
171+
'exclude_defaults=False, exclude_none=False, round_trip=False)'
172172
)
173173
assert s.to_python(123, include={'x'}) == (
174174
"123 info=SerializationInfo(include={'x'}, exclude=None, mode='python', by_alias=True, exclude_unset=False, "
175-
"exclude_defaults=False, exclude_none=False, round_trip=False)"
175+
'exclude_defaults=False, exclude_none=False, round_trip=False)'
176176
)
177177
assert s.to_python(123, mode='json', exclude={1: {2}}) == (
178178
"123 info=SerializationInfo(include=None, exclude={1: {2}}, mode='json', by_alias=True, exclude_unset=False, "
179-
"exclude_defaults=False, exclude_none=False, round_trip=False)"
179+
'exclude_defaults=False, exclude_none=False, round_trip=False)'
180180
)
181181
assert s.to_json(123) == (
182182
b'"123 info=SerializationInfo(include=None, exclude=None, mode=\'json\', by_alias=True, exclude_unset=False, '

tests/test_misc.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
from pathlib import Path
44

55
import pytest
6+
from typing_extensions import get_args
67

7-
from pydantic_core import core_schema
8+
from pydantic_core import CoreSchema, CoreSchemaType, core_schema
89
from pydantic_core._pydantic_core import (
910
SchemaError,
1011
SchemaValidator,
@@ -178,3 +179,18 @@ def test_all_errors():
178179
literal = ''.join(f'\n {e!r},' for e in error_types)
179180
print(f'python code (end of pydantic_core/core_schema.py):\n\nErrorType = Literal[{literal}\n]')
180181
pytest.fail('core_schema.ErrorType needs to be updated')
182+
183+
184+
def test_core_schema_type_literal():
185+
def get_type_value(schema):
186+
type_ = schema.__annotations__['type']
187+
m = re.search(r"Literal\['(.+?)']", type_.__forward_arg__)
188+
assert m, f'Unknown schema type: {type_}'
189+
return m.group(1)
190+
191+
schema_types = tuple(get_type_value(x) for x in CoreSchema.__args__)
192+
schema_types = tuple(dict.fromkeys(schema_types)) # remove duplicates while preserving order
193+
if get_args(CoreSchemaType) != schema_types:
194+
literal = ''.join(f'\n {e!r},' for e in schema_types)
195+
print(f'python code (near end of pydantic_core/core_schema.py):\n\nCoreSchemaType = Literal[{literal}\n]')
196+
pytest.fail('core_schema.CoreSchemaType needs to be updated')
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ def f(__input: Any, __info: core_schema.SerializationInfo) -> str:
189189
)
190190
assert s.to_python(123) == (
191191
"SerializationInfo(include=None, exclude=None, mode='python', by_alias=True, exclude_unset=False, "
192-
"exclude_defaults=False, exclude_none=False, round_trip=False)"
192+
'exclude_defaults=False, exclude_none=False, round_trip=False)'
193193
)
194194

195195

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