Skip to content

Commit 4424e31

Browse files
authored
Fix type converter handling when init return is annotated (#327)
1 parent 7cb07c0 commit 4424e31

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

apischema/conversions/utils.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from inspect import Parameter, isclass, signature
1+
from inspect import Parameter, signature
22
from typing import Any, Callable, Dict, Generic, Optional, Tuple, Type, cast
33

44
from apischema.types import AnyType
@@ -32,7 +32,7 @@ def converter_types(
3232
else:
3333
parameters = list(signature(converter).parameters.values())
3434
except ValueError: # builtin types
35-
if target is None and isclass(converter):
35+
if target is None and is_type(converter):
3636
target = cast(Type[Any], converter)
3737
if source is None:
3838
raise TypeError("Converter source is unknown") from None
@@ -51,7 +51,7 @@ def converter_types(
5151
if source is not None and target is not None:
5252
return source, target
5353
types = get_type_hints(converter, None, namespace, include_extras=True)
54-
if not types and isclass(converter):
54+
if not types and is_type(converter):
5555
types = get_type_hints(
5656
converter.__new__, None, namespace, include_extras=True
5757
) or get_type_hints(
@@ -63,12 +63,12 @@ def converter_types(
6363
except KeyError:
6464
raise TypeError("converter source is unknown") from None
6565
if target is None:
66-
try:
67-
target = types.pop("return")
68-
except KeyError:
69-
if isclass(converter):
70-
target = cast(Type, converter)
71-
else:
66+
if is_type(converter):
67+
target = cast(Type, converter)
68+
else:
69+
try:
70+
target = types.pop("return")
71+
except KeyError:
7272
raise TypeError("converter target is unknown") from None
7373
return source, target
7474

apischema/serialization/serialized_methods.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from collections import defaultdict
22
from dataclasses import dataclass
33
from functools import wraps
4-
from inspect import Parameter, isclass, signature
4+
from inspect import Parameter, signature
55
from typing import (
66
Any,
77
Callable,
@@ -24,7 +24,7 @@
2424
from apischema.ordering import Ordering
2525
from apischema.schemas import Schema
2626
from apischema.types import AnyType, Undefined, UndefinedType
27-
from apischema.typing import generic_mro, get_type_hints
27+
from apischema.typing import generic_mro, get_type_hints, is_type
2828
from apischema.utils import (
2929
deprecate_kwargs,
3030
get_args2,
@@ -61,7 +61,7 @@ def return_type(self, return_type: AnyType) -> AnyType:
6161
def types(self, owner: AnyType = None) -> Mapping[str, AnyType]:
6262
types = get_type_hints(self.func, include_extras=True)
6363
if "return" not in types:
64-
if isclass(self.func):
64+
if is_type(self.func):
6565
types["return"] = self.func
6666
else:
6767
raise TypeError("Function must be typed")
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from apischema import deserialize, deserializer
2+
3+
4+
@deserializer
5+
class Foo:
6+
def __init__(self, bar: int) -> None:
7+
self.bar = bar
8+
9+
10+
def test_type_converter():
11+
foo = deserialize(Foo, 42)
12+
assert isinstance(foo, Foo) and foo.bar == 42

0 commit comments

Comments
 (0)