Skip to content

Commit 81d5a36

Browse files
committed
inheritance fix
1 parent 97be6a4 commit 81d5a36

File tree

4 files changed

+48
-19
lines changed

4 files changed

+48
-19
lines changed

nvelope/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
__version__ = "0.2.0"
1+
__version__ = "0.3.0"
22

33
from nvelope.nvelope import *

nvelope/nvelope.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -106,18 +106,18 @@ def as_json(self) -> JSON:
106106
def from_json(cls, parsed: JSON) -> "Obj":
107107
assert isinstance(parsed, dict)
108108
kwargs = {}
109-
for name, t in cls.__annotations__.items():
110-
field: Conversion = cls._conversion[name]
109+
for f in fields(cls):
110+
conv: Conversion = cls._conversion[f.name]
111111
if (
112-
hasattr(t, "__origin__")
113-
and inspect.isclass(t.__origin__)
114-
and issubclass(t.__origin__, MaybeMissing)
112+
hasattr(f.type, "__origin__")
113+
and inspect.isclass(f.type.__origin__)
114+
and issubclass(f.type.__origin__, MaybeMissing)
115115
):
116-
kwargs[name] = (
117-
Jst(field.from_json(parsed[name])) if name in parsed else Miss()
116+
kwargs[f.name] = (
117+
Jst(conv.from_json(parsed[f.name])) if f.name in parsed else Miss()
118118
)
119119
else:
120-
kwargs[name] = field.from_json(parsed[name])
120+
kwargs[f.name] = conv.from_json(parsed[f.name])
121121
return cls(**kwargs) # type: ignore
122122

123123

@@ -171,21 +171,21 @@ def as_json(self) -> JSON:
171171
def from_json(cls, parsed: JSON) -> "Obj":
172172
assert isinstance(parsed, dict)
173173
kwargs = {}
174-
for name, t in cls.__annotations__.items():
175-
field: Conversion = cls._conversion[name]
176-
maybe_unescaped_name = cls._maybe_renamed(name)
174+
for field in fields(cls):
175+
conv: Conversion = cls._conversion[field.name]
176+
maybe_unescaped_name = cls._maybe_renamed(field.name)
177177
if (
178-
hasattr(t, "__origin__")
179-
and inspect.isclass(t.__origin__)
180-
and issubclass(t.__origin__, MaybeMissing)
178+
hasattr(field.type, "__origin__")
179+
and inspect.isclass(field.type.__origin__)
180+
and issubclass(field.type.__origin__, MaybeMissing)
181181
):
182-
kwargs[name] = (
183-
Jst(field.from_json(parsed[maybe_unescaped_name]))
182+
kwargs[field.name] = (
183+
Jst(conv.from_json(parsed[maybe_unescaped_name]))
184184
if maybe_unescaped_name in parsed
185185
else Miss()
186186
)
187187
else:
188-
kwargs[name] = field.from_json(parsed[maybe_unescaped_name])
188+
kwargs[field.name] = conv.from_json(parsed[maybe_unescaped_name])
189189
return cls(**kwargs) # type: ignore
190190

191191
@classmethod

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "nvelope"
3-
version = "0.2.0"
3+
version = "0.3.0"
44
description = "Painless JSON marshalling and unmarshalling"
55
authors = ["monomonedula <valh@tuta.io>"]
66
readme = 'README.md'

tests/test_nvelope.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
ListConversion,
1818
MappingConv,
1919
ConversionOf,
20+
JSON,
2021
)
2122

2223

@@ -344,3 +345,31 @@ class Data(Obj):
344345
assert Data({443: "hello there"}).as_json() == {
345346
"mapping_field": {"443": "hello there"}
346347
}
348+
349+
350+
def test_inheritance():
351+
@dataclass
352+
class Human(Obj):
353+
_conversion = {
354+
"name": string_conv,
355+
"age": int_conv,
356+
}
357+
358+
name: str
359+
age: int
360+
361+
@dataclass
362+
class Employee(Human):
363+
_conversion = {
364+
**Human._conversion,
365+
"experience": int_conv,
366+
}
367+
368+
experience: int
369+
370+
@classmethod
371+
def from_json(cls, j: JSON):
372+
return super(Employee, cls).from_json(j)
373+
374+
j = Employee("Boris", 42, 20).as_json()
375+
assert Employee.from_json(j) == Employee("Boris", 42, 20)

0 commit comments

Comments
 (0)