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

Fix type converter handling when init return is annotated #327

Merged
merged 2 commits into from
Jan 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 9 additions & 9 deletions apischema/conversions/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from inspect import Parameter, isclass, signature
from inspect import Parameter, signature
from typing import Any, Callable, Dict, Generic, Optional, Tuple, Type, cast

from apischema.types import AnyType
Expand Down Expand Up @@ -32,7 +32,7 @@ def converter_types(
else:
parameters = list(signature(converter).parameters.values())
except ValueError: # builtin types
if target is None and isclass(converter):
if target is None and is_type(converter):
target = cast(Type[Any], converter)
if source is None:
raise TypeError("Converter source is unknown") from None
Expand All @@ -51,7 +51,7 @@ def converter_types(
if source is not None and target is not None:
return source, target
types = get_type_hints(converter, None, namespace, include_extras=True)
if not types and isclass(converter):
if not types and is_type(converter):
types = get_type_hints(
converter.__new__, None, namespace, include_extras=True
) or get_type_hints(
Expand All @@ -63,12 +63,12 @@ def converter_types(
except KeyError:
raise TypeError("converter source is unknown") from None
if target is None:
try:
target = types.pop("return")
except KeyError:
if isclass(converter):
target = cast(Type, converter)
else:
if is_type(converter):
target = cast(Type, converter)
else:
try:
target = types.pop("return")
except KeyError:
raise TypeError("converter target is unknown") from None
return source, target

Expand Down
6 changes: 3 additions & 3 deletions apischema/serialization/serialized_methods.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from collections import defaultdict
from dataclasses import dataclass
from functools import wraps
from inspect import Parameter, isclass, signature
from inspect import Parameter, signature
from typing import (
Any,
Callable,
Expand All @@ -24,7 +24,7 @@
from apischema.ordering import Ordering
from apischema.schemas import Schema
from apischema.types import AnyType, Undefined, UndefinedType
from apischema.typing import generic_mro, get_type_hints
from apischema.typing import generic_mro, get_type_hints, is_type
from apischema.utils import (
deprecate_kwargs,
get_args2,
Expand Down Expand Up @@ -61,7 +61,7 @@ def return_type(self, return_type: AnyType) -> AnyType:
def types(self, owner: AnyType = None) -> Mapping[str, AnyType]:
types = get_type_hints(self.func, include_extras=True)
if "return" not in types:
if isclass(self.func):
if is_type(self.func):
types["return"] = self.func
else:
raise TypeError("Function must be typed")
Expand Down
12 changes: 12 additions & 0 deletions tests/integration/test_type_converter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from apischema import deserialize, deserializer


@deserializer
class Foo:
def __init__(self, bar: int) -> None:
self.bar = bar


def test_type_converter():
foo = deserialize(Foo, 42)
assert isinstance(foo, Foo) and foo.bar == 42