Skip to content

Commit

Permalink
Moar Updates
Browse files Browse the repository at this point in the history
* Add support for `Sequence`, `Collection`, and `MutableSequence`
* Add support for container class with `Meta` config (#163)
* Fix for `NoneType`
  • Loading branch information
rnag committed Dec 8, 2024
1 parent 2eade44 commit 989416b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
14 changes: 14 additions & 0 deletions dataclass_wizard/class_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,24 @@ def call_meta_initializer_if_needed(cls):
"""
Calls the Meta initializer when the inner :class:`Meta` is sub-classed.
"""
# TODO add tests

cls_module = cls.__module__

# skip classes provided by this library
if cls_module.startswith('dataclass_wizard.'):
return

cls_name = get_class_name(cls)

if cls_name in META_INITIALIZER:
META_INITIALIZER[cls_name](cls)
else:
for base in cls.__bases__:
base_cls_name = get_class_name(base)

if base_cls_name in META_INITIALIZER:
META_INITIALIZER[base_cls_name](cls)


def get_meta(cls, base_cls=AbstractMeta):
Expand Down
11 changes: 9 additions & 2 deletions dataclass_wizard/type_def.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
from datetime import date, time, datetime
from enum import Enum
from os import PathLike
from types import NoneType
from typing import (
Any, Type, TypeVar, Sequence, Mapping, List, Dict, DefaultDict, FrozenSet,
Union, NamedTuple, Callable, AnyStr, TextIO, BinaryIO,
Expand All @@ -56,7 +55,15 @@
)
from uuid import UUID

from .constants import PY311_OR_ABOVE, PY313_OR_ABOVE
from .constants import PY310_OR_ABOVE, PY311_OR_ABOVE, PY313_OR_ABOVE


# The class of the `None` singleton, cached for re-usability
if PY310_OR_ABOVE:
# https://docs.python.org/3/library/types.html#types.NoneType
from types import NoneType
else:
NoneType = type(None)

# Type check for numeric types - needed because `bool` is technically
# a Number.
Expand Down
14 changes: 12 additions & 2 deletions dataclass_wizard/v1/loaders.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# TODO cleanup imports

import types
from base64 import decodebytes
from collections import defaultdict, deque, namedtuple
import collections.abc as abc
Expand Down Expand Up @@ -37,6 +36,7 @@
from ..models import Extras, PatternedDT, TypeInfo
from ..parsers import *
from ..type_def import (
NoneType,
ExplicitNull, FrozenKeys, DefFactory, NoneType, JSONObject,
PyRequired, PyNotRequired, PyLiteralString,
M, N, T, E, U, DD, LSQ, NT
Expand All @@ -60,7 +60,7 @@
# returns the same object. We can provide a fast-path for these types in asdict and astuple.
_SIMPLE_TYPES = (
# Common JSON Serializable types
types.NoneType,
NoneType,
bool,
int,
float,
Expand Down Expand Up @@ -718,6 +718,16 @@ def get_string_for_annotation(cls,
# for the `cls` (base_type)
load_hook = cls.load_to_dataclass

elif origin in (abc.Sequence, abc.MutableSequence, abc.Collection):
load_hook = cls.load_to_iterable
# desired (non-generic) origin type
origin = tuple if origin is abc.Sequence else list
# Get type arguments, e.g. `Sequence[int]` -> `int`
try:
args = get_args(type_ann)
except ValueError:
args = Any,

else:

# TODO everything should use `get_origin_v2`
Expand Down

0 comments on commit 989416b

Please sign in to comment.