Skip to content

Commit

Permalink
Add Meta option to include non init-ed fields
Browse files Browse the repository at this point in the history
Updates #60
  • Loading branch information
huwcbjones committed Sep 15, 2023
1 parent cbad82b commit 7b8ed2b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
22 changes: 21 additions & 1 deletion marshmallow_dataclass/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,9 @@ def class_schema(
>>> person
Person(name='Anonymous', friends=[Person(name='Roger Boucher', friends=[])])
Marking dataclass fields as non-initialized (``init=False``), by default, will result in those
fields from being exluded in the schema. To override this behaviour, set the ``Meta`` option
``include_non_init=True``.
>>> @dataclasses.dataclass()
... class C:
... important: int = dataclasses.field(init=True, default=0)
Expand All @@ -310,6 +313,20 @@ def class_schema(
>>> c
C(important=9, unimportant=0)
>>> @dataclasses.dataclass()
... class C:
... class Meta:
... include_non_init = True
... important: int = dataclasses.field(init=True, default=0)
... unimportant: int = dataclasses.field(init=False, default=0)
...
>>> c = class_schema(C)().load({
... "important": 9, # This field will be imported
... "unimportant": 9 # This field will be imported
... }, unknown=marshmallow.EXCLUDE)
>>> c
C(important=9, unimportant=9)
>>> @dataclasses.dataclass
... class Website:
... url:str = dataclasses.field(metadata = {
Expand Down Expand Up @@ -408,6 +425,9 @@ def _internal_class_schema(
if hasattr(v, "__marshmallow_hook__") or k in MEMBERS_WHITELIST
}

# Determine whether we should include non-init fields
include_non_init = getattr(getattr(clazz, "Meta", None), "include_non_init", False)

# Update the schema members to contain marshmallow fields instead of dataclass fields
type_hints = get_type_hints(
clazz, localns=clazz_frame.f_locals if clazz_frame else None
Expand All @@ -424,7 +444,7 @@ def _internal_class_schema(
),
)
for field in fields
if field.init
if field.init or include_non_init
)

schema_class = type(clazz.__name__, (_base_schema(clazz, base_schema),), attributes)
Expand Down
23 changes: 23 additions & 0 deletions tests/test_class_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,29 @@ class Second:
{"first": {"second": {"first": None}}},
)

def test_init_fields(self):
@dataclasses.dataclass
class NoMeta:
no_init: str = dataclasses.field(init=False)

@dataclasses.dataclass
class NoInit:
class Meta:
pass

no_init: str = dataclasses.field(init=False)

@dataclasses.dataclass
class Init:
class Meta:
include_non_init = True

no_init: str = dataclasses.field(init=False)

self.assertNotIn("no_init", class_schema(NoMeta)().fields)
self.assertNotIn("no_init", class_schema(NoInit)().fields)
self.assertIn("no_init", class_schema(Init)().fields)


if __name__ == "__main__":
unittest.main()

0 comments on commit 7b8ed2b

Please sign in to comment.