8000 add type hints for function-wrap · pydantic/pydantic-core@03c8e09 · GitHub
[go: up one dir, main page]

Skip to content

Commit 03c8e09

Browse files
committed
add type hints for function-wrap
1 parent ea60243 commit 03c8e09

File tree

4 files changed

+37
-7
lines changed

4 files changed

+37
-7
lines changed

pydantic_core/core_schema.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,16 +178,26 @@ def function_plain_ser_schema(
178178
)
179179

180180

181+
class SerializeWrapHandler(Protocol): # pragma: no cover
182+
def __call__(self, __input_value: Any, __index_key: int | str | None = None) -> Any:
183+
...
184+
185+
186+
class SerializeWrapFunction(Protocol): # pragma: no cover
187+
def __call__(self, __input_value: Any, __serializer: SerializeWrapHandler, __info: SerializationInfo) -> Any:
188+
...
189+
190+
181191
class FunctionWrapSerSchema(TypedDict, total=False):
182192
type: Required[Literal['function-wrap']]
183-
function: Required[SerializePlainFunction]
193+
function: Required[SerializeWrapFunction]
184194
schema: Required[CoreSchema]
185195
json_return_type: JsonReturnTypes
186196
when_used: WhenUsed # default: 'always'
187197

188198

189199
def function_wrap_ser_schema(
190-
function: SerializePlainFunction,
200+
function: SerializeWrapFunction,
191201
schema: CoreSchema,
192202
*,
193203
json_return_type: JsonReturnTypes | None = None,

src/serializers/type_serializers/function.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,10 @@ impl SerializationCallable {
289289
}
290290

291291
fn __repr__(&self) -> PyResult<String> {
292-
Ok(format!("SerializationCallable(serializer={:?})", self.serializer))
292+
Ok(format!(
293+
"SerializationCallable(serializer={})",
294+
self.serializer.get_name()
295+
))
293296
}
294297

295298
fn __str__(&self) -> PyResult<String> {

tests/serializers/test_functions.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,9 @@ def f(value, serializer, _info):
267267
s = SchemaSerializer(
268268
core_schema.any_schema(serialization=core_schema.function_wrap_ser_schema(f, core_schema.int_schema()))
269269
)
270-
assert s.to_python('foo') == 'result=3 repr=SerializationCallable(serializer=Int(IntSerializer))'
271-
assert s.to_python('foo', mode='json') == 'result=3 repr=SerializationCallable(serializer=Int(IntSerializer))'
272-
assert s.to_json('foo') == b'"result=3 repr=SerializationCallable(serializer=Int(IntSerializer))"'
270+
assert s.to_python('foo') == 'result=3 repr=SerializationCallable(serializer=int)'
271+
assert s.to_python('foo', mode='json') == 'result=3 repr=SerializationCallable(serializer=int)'
272+
assert s.to_json('foo') == b'"result=3 repr=SerializationCallable(serializer=int)"'
273273

274274

275275
def test_deque():

tests/test_typing.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ def test_type_error():
180180
assert isinstance(e, PydanticKnownError)
181181

182182

183-
def test_ser_function():
183+
def test_ser_function_plain():
184184
def f(__input: Any, __info: core_schema.SerializationInfo) -> str:
185185
return str(__info)
186186

@@ -191,3 +191,20 @@ def f(__input: Any, __info: core_schema.SerializationInfo) -> str:
191191
"SerializationInfo(include=None, exclude=None, mode='python', by_alias=True, exclude_unset=False, "
192192
"exclude_defaults=False, exclude_none=False, round_trip=False)"
193193
)
194+
195+
196+
def test_ser_function_wrap():
197+
def f(__input: Any, __serialize: core_schema.SerializeWrapHandler, __info: core_schema.SerializationInfo) -> str:
198+
return f'{__serialize} {__info}'
199+
200+
s = SchemaSerializer(
201+
core_schema.any_schema(
202+
serialization=core_schema.function_wrap_ser_schema(f, core_schema.str_schema(), when_used='json')
203+
)
204+
)
205+
# insert_assert(s.to_python(123, mode='json'))
206+
assert s.to_python(123, mode='json') == (
207+
"SerializationCallable(serializer=str) "
208+
"SerializationInfo(include=None, exclude=None, mode='json', by_alias=True, exclude_unset=False, "
209+
"exclude_defaults=False, exclude_none=False, round_trip=False)"
210+
)

0 commit comments

Comments
 (0)
0