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 schema deserialization 'type' value output #386

Merged
merged 6 commits into from
Dec 12, 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
4 changes: 4 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ updates:
directory: "benchmark"
schedule:
interval: "daily"
- package-ecosystem: "pip"
directory: "scripts"
schedule:
interval: "daily"
10 changes: 5 additions & 5 deletions apischema/conversions/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
TYPE_CHECKING,
Any,
Callable,
List,
MutableMapping,
Optional,
Tuple,
Type,
TypeVar,
Union,
Expand Down Expand Up @@ -37,8 +37,8 @@
pass


_deserializers: MutableMapping[AnyType, List[ConvOrFunc]] = CacheAwareDict(
defaultdict(list)
_deserializers: MutableMapping[AnyType, Tuple[ConvOrFunc, ...]] = CacheAwareDict(
defaultdict(tuple)
)
_serializers: MutableMapping[AnyType, ConvOrFunc] = CacheAwareDict({})
Deserializer = TypeVar(
Expand All @@ -54,7 +54,7 @@ def default_deserialization(tp):
return _deserializers.get(tp)

else:
default_deserialization = _deserializers.get # type: ignore
default_deserialization = _deserializers.get


def default_serialization(tp: Type) -> Optional[AnyConversion]:
Expand Down Expand Up @@ -83,7 +83,7 @@ def check_converter_type(tp: AnyType) -> AnyType:
def _add_deserializer(conversion: ConvOrFunc, target: AnyType):
target = check_converter_type(target)
if conversion not in _deserializers[target]:
_deserializers[target].append(conversion)
_deserializers[target] = *_deserializers[target], conversion


class DeserializerDescriptor(MethodWrapper[staticmethod]):
Expand Down
50 changes: 25 additions & 25 deletions apischema/deserialization/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
OptionalMethod,
PatternField,
RawConstructor,
RawConstructorCopy,
RecMethod,
SetMethod,
SimpleObjectMethod,
Expand Down Expand Up @@ -154,6 +155,26 @@ def check_only(method: DeserializationMethod) -> bool:
)


def is_raw_dataclass(cls: type) -> bool:
return (
dataclasses.is_dataclass(cls)
and type(cls) == type # no metaclass
and "__slots__" not in cls.__dict__
and not hasattr(cls, "__post_init__")
and all(f.init for f in dataclasses.fields(cls))
and cls.__new__ is object.__new__
and (
cls.__setattr__ is object.__setattr__
or getattr(cls, dataclasses._PARAMS).frozen # type: ignore
)
and (
list(inspect.signature(cls.__init__, follow_wrapped=False).parameters) # type: ignore
== ["__dataclass_self__" if "self" in dataclasses.fields(cls) else "self"]
+ [f.name for f in dataclasses.fields(cls)]
)
)


@dataclasses.dataclass(frozen=True)
class DeserializationMethodFactory:
factory: Factory
Expand Down Expand Up @@ -481,31 +502,12 @@ def factory(
)
object_constraints = constraints_validators(constraints)[dict]
all_alliases = set(alias_by_name.values())
constructor: Constructor
constructor: Optional[Constructor] = None
if is_typed_dict(cls):
constructor = NoConstructor(cls)
elif (
settings.deserialization.override_dataclass_constructors
and dataclasses.is_dataclass(cls)
and "__slots__" not in cls.__dict__
and not hasattr(cls, "__post_init__")
and all(f.init for f in dataclasses.fields(cls))
and cls.__new__ is object.__new__
and (
cls.__setattr__ is object.__setattr__
or getattr(cls, dataclasses._PARAMS).frozen # type: ignore
)
and (
list(
inspect.signature(cls.__init__, follow_wrapped=False).parameters
)
== [
"__dataclass_self__"
if "self" in dataclasses.fields(cls)
else "self"
]
+ [f.name for f in dataclasses.fields(cls)]
)
and is_raw_dataclass(cls)
):
constructor = FieldsConstructor(
cls,
Expand All @@ -521,8 +523,6 @@ def factory(
if f.default_factory is not dataclasses.MISSING
),
)
else:
constructor = RawConstructor(cls)
if (
not object_constraints
and not flattened_fields
Expand All @@ -545,15 +545,15 @@ def factory(
)
):
return SimpleObjectMethod(
constructor,
constructor or RawConstructorCopy(cls),
tuple(normal_fields),
all_alliases,
is_typed_dict(cls),
settings.errors.missing_property,
settings.errors.unexpected_property,
)
return ObjectMethod(
constructor,
constructor or RawConstructor(cls),
object_constraints,
tuple(normal_fields),
tuple(flattened_fields),
Expand Down
Loading