Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CoreSchemaType Literal #389

Merged
merged 2 commits into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)'
Copy link
Collaborator Author

@dmontagu dmontagu Feb 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needed to change this to make make pass, here and elsewhere that quotes changed in test files

)
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
@@ -1,10 +1,11 @@
import re
import sys
from pathlib import Path
from typing import get_args

import pytest

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)'
)