Skip to content

Commit

Permalink
Use PEP570 syntax (#1216)
Browse files Browse the repository at this point in the history
  • Loading branch information
Viicos authored Mar 5, 2024
1 parent a98b8b0 commit f669db9
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 27 deletions.
2 changes: 1 addition & 1 deletion python/pydantic_core/_pydantic_core.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class Some(Generic[_T]):
Returns the value wrapped by `Some`.
"""
@classmethod
def __class_getitem__(cls, __item: Any) -> Type[Self]: ...
def __class_getitem__(cls, item: Any, /) -> Type[Self]: ...

@final
class SchemaValidator:
Expand Down
30 changes: 15 additions & 15 deletions python/pydantic_core/core_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,13 @@ def simple_ser_schema(type: ExpectedSerializationTypes) -> SimpleSerSchema:
return SimpleSerSchema(type=type)


# (__input_value: Any) -> Any
# (input_value: Any, /) -> Any
GeneralPlainNoInfoSerializerFunction = Callable[[Any], Any]
# (__input_value: Any, __info: FieldSerializationInfo) -> Any
# (input_value: Any, info: FieldSerializationInfo, /) -> Any
GeneralPlainInfoSerializerFunction = Callable[[Any, SerializationInfo], Any]
# (__model: Any, __input_value: Any) -> Any
# (model: Any, input_value: Any, /) -> Any
FieldPlainNoInfoSerializerFunction = Callable[[Any, Any], Any]
# (__model: Any, __input_value: Any, __info: FieldSerializationInfo) -> Any
# (model: Any, input_value: Any, info: FieldSerializationInfo, /) -> Any
FieldPlainInfoSerializerFunction = Callable[[Any, Any, FieldSerializationInfo], Any]
SerializerFunction = Union[
GeneralPlainNoInfoSerializerFunction,
Expand Down Expand Up @@ -275,7 +275,7 @@ def plain_serializer_function_ser_schema(
function: The function to use for serialization
is_field_serializer: Whether the serializer is for a field, e.g. takes `model` as the first argument,
and `info` includes `field_name`
info_arg: Whether the function takes an `__info` argument
info_arg: Whether the function takes an `info` argument
return_schema: Schema to use for serializing return value
when_used: When the function should be called
"""
Expand All @@ -293,16 +293,16 @@ def plain_serializer_function_ser_schema(


class SerializerFunctionWrapHandler(Protocol): # pragma: no cover
def __call__(self, __input_value: Any, __index_key: int | str | None = None) -> Any: ...
def __call__(self, input_value: Any, index_key: int | str | None = None, /) -> Any: ...


# (__input_value: Any, __serializer: SerializerFunctionWrapHandler) -> Any
# (input_value: Any, serializer: SerializerFunctionWrapHandler, /) -> Any
GeneralWrapNoInfoSerializerFunction = Callable[[Any, SerializerFunctionWrapHandler], Any]
# (__input_value: Any, __serializer: SerializerFunctionWrapHandler, __info: SerializationInfo) -> Any
# (input_value: Any, serializer: SerializerFunctionWrapHandler, info: SerializationInfo, /) -> Any
GeneralWrapInfoSerializerFunction = Callable[[Any, SerializerFunctionWrapHandler, SerializationInfo], Any]
# (__model: Any, __input_value: Any, __serializer: SerializerFunctionWrapHandler) -> Any
# (model: Any, input_value: Any, serializer: SerializerFunctionWrapHandler, /) -> Any
FieldWrapNoInfoSerializerFunction = Callable[[Any, Any, SerializerFunctionWrapHandler], Any]
# (__model: Any, __input_value: Any, __serializer: SerializerFunctionWrapHandler, __info: FieldSerializationInfo) -> Any
# (model: Any, input_value: Any, serializer: SerializerFunctionWrapHandler, info: FieldSerializationInfo, /) -> Any
FieldWrapInfoSerializerFunction = Callable[[Any, Any, SerializerFunctionWrapHandler, FieldSerializationInfo], Any]
WrapSerializerFunction = Union[
GeneralWrapNoInfoSerializerFunction,
Expand Down Expand Up @@ -338,7 +338,7 @@ def wrap_serializer_function_ser_schema(
function: The function to use for serialization
is_field_serializer: Whether the serializer is for a field, e.g. takes `model` as the first argument,
and `info` includes `field_name`
info_arg: Whether the function takes an `__info` argument
info_arg: Whether the function takes an `info` argument
schema: The schema to use for the inner serialization
return_schema: Schema to use for serializing return value
when_used: When the function should be called
Expand Down Expand Up @@ -1767,7 +1767,7 @@ def dict_schema(
)


# (__input_value: Any) -> Any
# (input_value: Any, /) -> Any
NoInfoValidatorFunction = Callable[[Any], Any]


Expand All @@ -1776,7 +1776,7 @@ class NoInfoValidatorFunctionSchema(TypedDict):
function: NoInfoValidatorFunction


# (__input_value: Any, __info: ValidationInfo) -> Any
# (input_value: Any, info: ValidationInfo, /) -> Any
WithInfoValidatorFunction = Callable[[Any, ValidationInfo], Any]


Expand Down Expand Up @@ -1990,7 +1990,7 @@ def __call__(self, input_value: Any, outer_location: str | int | None = None) ->
...


# (__input_value: Any, __validator: ValidatorFunctionWrapHandler) -> Any
# (input_value: Any, validator: ValidatorFunctionWrapHandler, /) -> Any
NoInfoWrapValidatorFunction = Callable[[Any, ValidatorFunctionWrapHandler], Any]


Expand All @@ -1999,7 +1999,7 @@ class NoInfoWrapValidatorFunctionSchema(TypedDict):
function: NoInfoWrapValidatorFunction


# (__input_value: Any, __validator: ValidatorFunctionWrapHandler, __info: ValidationInfo) -> Any
# (input_value: Any, validator: ValidatorFunctionWrapHandler, info: ValidationInfo, /) -> Any
WithInfoWrapValidatorFunction = Callable[[Any, ValidatorFunctionWrapHandler, ValidationInfo], Any]


Expand Down
8 changes: 4 additions & 4 deletions tests/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,8 @@ def test_type_error():


def test_ser_function_plain():
def f(__input: Any, __info: core_schema.SerializationInfo) -> str:
return str(__info)
def f(input: Any, info: core_schema.SerializationInfo, /) -> str:
return str(info)

s = SchemaSerializer(
core_schema.any_schema(
Expand All @@ -239,9 +239,9 @@ def f(__input: Any, __info: core_schema.SerializationInfo) -> str:

def test_ser_function_wrap():
def f(
__input: Any, __serialize: core_schema.SerializerFunctionWrapHandler, __info: core_schema.SerializationInfo
input: Any, serialize: core_schema.SerializerFunctionWrapHandler, info: core_schema.SerializationInfo, /
) -> str:
return f'{__serialize} {__info}'
return f'{serialize} {info}'

s = SchemaSerializer(
core_schema.any_schema(
Expand Down
10 changes: 5 additions & 5 deletions tests/validators/test_model_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ def __iter__(self):
def __len__(self) -> int:
return len(self._d)

def __getitem__(self, __k):
return self._d[__k]
def __getitem__(self, k, /):
return self._d[k]

def __repr__(self):
return 'Map({})'.format(', '.join(f'{k}={v!r}' for k, v in self._d.items()))
Expand Down Expand Up @@ -1188,9 +1188,9 @@ class Source:
a = 1
b = 2

def __getattribute__(self, __name: str) -> Any:
accessed.append(__name)
return super().__getattribute__(__name)
def __getattribute__(self, name: str, /) -> Any:
accessed.append(name)
return super().__getattribute__(name)

assert v.validate_python(Source()) == ({'a': 1}, None, {'a'})
assert 'a' in accessed and 'b' not in accessed
Expand Down
4 changes: 2 additions & 2 deletions tests/validators/test_typed_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ def __iter__(self):
def __len__(self) -> int:
return len(self._d)

def __getitem__(self, __k):
return self._d[__k]
def __getitem__(self, k, /):
return self._d[k]

def __repr__(self):
return 'Map({})'.format(', '.join(f'{k}={v!r}' for k, v in self._d.items()))
Expand Down

0 comments on commit f669db9

Please sign in to comment.