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: apply global config globally #524

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
49 changes: 34 additions & 15 deletions dataclasses_json/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,17 @@ def _decode_dataclass(cls, kvs, infer_missing):
return cls(**init_kwargs)


def _decode_type(type_, value, infer_missing):
if _has_decoder_in_global_config(type_):
return _get_decoder_in_global_config(type_)(value)
if is_dataclass(type_) or is_dataclass(type_):
Copy link

@rtsscy rtsscy May 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is a refactor from _decode_generic, would this line need to be
if is_dataclass(type_) or is_dataclass(value):

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes you are correct. Let me create a new PR to fix this.

return _decode_dataclass(type_, value, infer_missing)
if _is_supported_generic(type_):
return _decode_generic(type_, value, infer_missing)
else:
return _support_extended_types(type_, value)
PJCampi marked this conversation as resolved.
Show resolved Hide resolved


def _support_extended_types(field_type, field_value):
if _issubclass_safe(field_type, datetime):
# FIXME this is a hack to deal with mm already decoding
Expand Down Expand Up @@ -305,12 +316,7 @@ def _decode_generic(type_, value, infer_missing):
res = value
elif _is_optional(type_) and len(_args) == 2: # Optional
type_arg = _get_type_arg_param(type_, 0)
if is_dataclass(type_arg) or is_dataclass(value):
res = _decode_dataclass(type_arg, value, infer_missing)
elif _is_supported_generic(type_arg):
res = _decode_generic(type_arg, value, infer_missing)
else:
res = _support_extended_types(type_arg, value)
res = _decode_type(type_arg, value, infer_missing)
else: # Union (already decoded or try to decode a dataclass)
type_options = _get_type_args(type_)
res = value # assume already decoded
Expand Down Expand Up @@ -367,13 +373,6 @@ def _decode_items(type_args, xs, infer_missing):
type_arg is a typevar we need to extract the reified type information
hence the check of `is_dataclass(vs)`
"""
def _decode_item(type_arg, x):
if is_dataclass(type_arg) or is_dataclass(xs):
return _decode_dataclass(type_arg, x, infer_missing)
if _is_supported_generic(type_arg):
return _decode_generic(type_arg, x, infer_missing)
return x

def handle_pep0673(pre_0673_hint: str) -> Union[Type, str]:
for module in sys.modules:
maybe_resolved = getattr(sys.modules[module], type_args, None)
Expand All @@ -390,13 +389,13 @@ def handle_pep0673(pre_0673_hint: str) -> Union[Type, str]:

if _isinstance_safe(type_args, Collection) and not _issubclass_safe(type_args, Enum):
if len(type_args) == len(xs):
return list(_decode_item(type_arg, x) for type_arg, x in zip(type_args, xs))
return list(_decode_type(type_arg, x, infer_missing) for type_arg, x in zip(type_args, xs))
else:
raise TypeError(f"Number of types specified in the collection type {str(type_args)} "
f"does not match number of elements in the collection. In case you are working with tuples"
f"take a look at this document "
f"docs.python.org/3/library/typing.html#annotating-tuples.")
return list(_decode_item(type_args, x) for x in xs)
return list(_decode_type(type_args, x, infer_missing) for x in xs)


def _asdict(obj, encode_json=False):
Expand Down Expand Up @@ -428,5 +427,25 @@ def _asdict(obj, encode_json=False):
# enum.IntFlag and enum.Flag are regarded as collections in Python 3.11, thus a check against Enum is needed
elif isinstance(obj, Collection) and not isinstance(obj, (str, bytes, Enum)):
return list(_asdict(v, encode_json=encode_json) for v in obj)
# encoding of generics primarily relies on concrete types while decoding relies on type annotations. This makes
# applying encoders/decoders from global configuration inconsistent.
elif _has_encoder_in_global_config(type(obj)):
return _get_encoder_in_global_config(type(obj))(obj)
else:
return copy.deepcopy(obj)


def _has_decoder_in_global_config(type_):
return type_ in cfg.global_config.decoders


def _get_decoder_in_global_config(type_):
return cfg.global_config.decoders[type_]


def _has_encoder_in_global_config(type_):
return type_ in cfg.global_config.encoders


def _get_encoder_in_global_config(type_):
return cfg.global_config.encoders[type_]
33 changes: 33 additions & 0 deletions tests/test_global_config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from dataclasses import dataclass
from typing import List, Optional

from dataclasses_json import dataclass_json
from datetime import date
Expand All @@ -19,6 +20,18 @@ class PersonWithBirthday:
birthday: date


@dataclass_json
@dataclass
class HistoricalEvents:
dates: List[date]


@dataclass_json
@dataclass
class PackageDelivery:
date: Optional[date]


class TestGlobalConfig:
def test_encoder_override(self):
dataclasses_json.cfg.global_config.encoders[str] = lambda s: s[::-1]
Expand All @@ -30,3 +43,23 @@ def test_encoder_extension(self):
assert PersonWithBirthday("Kobe Bryant", date(1978, 8, 23)).to_json() \
== '{"name": "Kobe Bryant", "birthday": "1978-08-23"}'
dataclasses_json.cfg.global_config.encoders = {}

def test_encoder_and_decoder_extension_in_collections(self):
dataclasses_json.cfg.global_config.encoders[date] = date.isoformat
dataclasses_json.cfg.global_config.decoders[date] = date.fromisoformat
historical_events = HistoricalEvents([date(1918, 11, 11), date(1945, 5, 8)])
expected_json = '{"dates": ["1918-11-11", "1945-05-08"]}'
assert historical_events.to_json() == expected_json
assert HistoricalEvents.from_json(expected_json) == historical_events
dataclasses_json.cfg.global_config.encoders = {}
dataclasses_json.cfg.global_config.decoders = {}

def test_encoder_and_decoder_extension_in_union(self):
dataclasses_json.cfg.global_config.encoders[date] = date.isoformat
dataclasses_json.cfg.global_config.decoders[date] = date.fromisoformat
package_delivery = PackageDelivery(date(2023, 1, 1))
expected_json = '{"date": "2023-01-01"}'
assert package_delivery.to_json() == expected_json
assert PackageDelivery.from_json(expected_json) == package_delivery
dataclasses_json.cfg.global_config.encoders = {}
dataclasses_json.cfg.global_config.decoders = {}
8 changes: 8 additions & 0 deletions tests/test_union.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,3 +276,11 @@ def test_deserialize_with_mismatched_field_types():
obj = s.loads(json)
assert obj.event is not None
assert obj.event.data == "Hello world!"


def test_deserialize_with_mismatched_field_types():
json = '{"event": {"data": "Hello world!"} }'
s = C16.schema()
obj = s.loads(json)
assert obj.event is not None
assert obj.event.data == "Hello world!"
Loading