dataclass-extensions 0.2.1__py3-none-any.whl → 0.2.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -26,7 +26,11 @@ class Decoder:
26
26
  Decode a dataset from a JSON-safe dictionary. The inverse of :func:`encode()`.
27
27
  """
28
28
  type_hints = typing.get_type_hints(config_class)
29
- kwargs = {k: _coerce(v, type_hints[k], self.custom_handlers, k) for k, v in data.items()}
29
+ kwargs: dict[str, Any] = {}
30
+ for k, v in data.items():
31
+ if k not in type_hints:
32
+ raise AttributeError(f"class '{config_class.__qualname__}' has no attribute '{k}'")
33
+ kwargs[k] = _coerce(v, type_hints[k], self.custom_handlers, k)
30
34
  return config_class(**kwargs)
31
35
 
32
36
 
@@ -96,6 +100,9 @@ def _coerce(
96
100
  except TypeError:
97
101
  pass
98
102
 
103
+ if allowed_type is float and _safe_isinstance(value, (float, int)):
104
+ return float(value)
105
+
99
106
  origin = getattr(allowed_type, "__origin__", None)
100
107
  args = getattr(allowed_type, "__args__", None)
101
108
  if (origin is list or origin is collections.abc.MutableSequence) and _safe_isinstance(
@@ -168,7 +175,14 @@ def _coerce(
168
175
  type_name = value.get("type", allowed_type._default_type)
169
176
  if type_name is not None:
170
177
  allowed_type = allowed_type.get_registered_class(type_name)
171
- type_hints = typing.get_type_hints(allowed_type)
178
+
179
+ try:
180
+ type_hints = typing.get_type_hints(allowed_type)
181
+ except NameError as e:
182
+ raise NameError(
183
+ f"{str(e)}. If you're using 'from __future__ import annotations' you may need to import this type."
184
+ ) from e
185
+
172
186
  kwargs = {}
173
187
  for k, v in value.items():
174
188
  try:
@@ -3,6 +3,7 @@ from __future__ import annotations
3
3
  import dataclasses
4
4
  import pathlib
5
5
  from datetime import datetime
6
+ from enum import Enum
6
7
  from typing import Any, Callable, ClassVar, Generator, Type, TypeVar
7
8
 
8
9
  from .registrable import Registrable
@@ -70,6 +71,8 @@ class Encoder:
70
71
  return d.timestamp()
71
72
  elif isinstance(d, pathlib.Path):
72
73
  return str(d)
74
+ elif isinstance(d, Enum):
75
+ return d.value
73
76
  elif d is None or isinstance(d, (float, int, bool, str)):
74
77
  return d
75
78
 
@@ -1 +1 @@
1
- VERSION = "0.2.1"
1
+ VERSION = "0.2.3"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dataclass-extensions
3
- Version: 0.2.1
3
+ Version: 0.2.3
4
4
  Summary: Additional functionality for Python dataclasses
5
5
  Author-email: Pete Walsh <epwalsh10@gmail.com>
6
6
  License: Apache License
@@ -0,0 +1,13 @@
1
+ dataclass_extensions/__init__.py,sha256=jpgRh5jjj9jM1tkhTVvB-aDThI2v4tAlq_WpR0-NOSI,255
2
+ dataclass_extensions/decode.py,sha256=d-EzqMywX3eDwPgvPfgKryfakGFh2pU8MnHn7xqhwZI,7150
3
+ dataclass_extensions/encode.py,sha256=7mLHmZrHThQMbC0wY5voHx3mOHgsRbq59kw_JdwgOOg,3324
4
+ dataclass_extensions/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
+ dataclass_extensions/registrable.py,sha256=TWdJ0vLkOTsw5Zm8uOk7Ueh6ZODn-q_2jht1uWRkpzE,4608
6
+ dataclass_extensions/types.py,sha256=UwSV0MKjFDaK4dlgm72vJHHr6-8QNe8jMhbTZGOMIJM,247
7
+ dataclass_extensions/utils.py,sha256=8cigzY9JDEWexWXlcMxXyf27S50-1ZPMNmn4zUSrc6k,935
8
+ dataclass_extensions/version.py,sha256=b-Z-6_UwMK_FEiFRkdRlWa4UpfELs391bZgkMRRJkm0,18
9
+ dataclass_extensions-0.2.3.dist-info/licenses/LICENSE,sha256=YvuKOpYh3COIF0yqq-nCMXtpS7mh1GyYvPVlW2j1G-M,11359
10
+ dataclass_extensions-0.2.3.dist-info/METADATA,sha256=_qOamBsevpRJ7gM3-SBn4lLg9tW2Ot6KrdEPhAWkRR0,15215
11
+ dataclass_extensions-0.2.3.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
12
+ dataclass_extensions-0.2.3.dist-info/top_level.txt,sha256=njfdeqLtTThNGwqnoKu4JV8J1287xGAeEwyESpoU-k8,21
13
+ dataclass_extensions-0.2.3.dist-info/RECORD,,
@@ -1,13 +0,0 @@
1
- dataclass_extensions/__init__.py,sha256=jpgRh5jjj9jM1tkhTVvB-aDThI2v4tAlq_WpR0-NOSI,255
2
- dataclass_extensions/decode.py,sha256=baPXLrWznJRA-JxPpSDFLB0y4OCvpnu1qNY13gIjnSo,6622
3
- dataclass_extensions/encode.py,sha256=rctJHAwlSnjOw0rTGbHRWigb2BT4DI3FYGiDzAjDmEQ,3233
4
- dataclass_extensions/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
- dataclass_extensions/registrable.py,sha256=TWdJ0vLkOTsw5Zm8uOk7Ueh6ZODn-q_2jht1uWRkpzE,4608
6
- dataclass_extensions/types.py,sha256=UwSV0MKjFDaK4dlgm72vJHHr6-8QNe8jMhbTZGOMIJM,247
7
- dataclass_extensions/utils.py,sha256=8cigzY9JDEWexWXlcMxXyf27S50-1ZPMNmn4zUSrc6k,935
8
- dataclass_extensions/version.py,sha256=Vdi6OffDRorPQeWjvXo2MPbidl7CNworxvziT78bjl0,18
9
- dataclass_extensions-0.2.1.dist-info/licenses/LICENSE,sha256=YvuKOpYh3COIF0yqq-nCMXtpS7mh1GyYvPVlW2j1G-M,11359
10
- dataclass_extensions-0.2.1.dist-info/METADATA,sha256=HfEEj5f6qnWVrx7LBzosDJ2dz7fkiQBQ_ktO_UeY81w,15215
11
- dataclass_extensions-0.2.1.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
12
- dataclass_extensions-0.2.1.dist-info/top_level.txt,sha256=njfdeqLtTThNGwqnoKu4JV8J1287xGAeEwyESpoU-k8,21
13
- dataclass_extensions-0.2.1.dist-info/RECORD,,