marshmallow-code / marshmallow-code/marshmallow
Add fields.Iterable as a base class for collection field types
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 7.2k
- Forks
- 738
- Avg merge
- 1d 23h
- Merged PRs (30d)
- 7
Description
I'd like to deserialize into some types that aren't json serializable, for example a frozenset of integers, `frozenset({1,2,3})`.
I can think of a few options in the current API:
- define a custom `FrozensetField` type and define its `_serialize` and `_deserialize` methods,
- use `fields.Function(deserialize=frozenset, serialize=list)`,
- define a custom replacement for the json module.
Is `fields.Function` the recommended strategy here? I'm unsure because
- it doesn't take a `cls_or_instance` parameter like `fields.List` does, so it's not obvious how to make a `set` *of* some other objects. For example, how do I define a frozenset of `fields.Nested(OtherSchema)`?
- it makes `Function` the main focus of attention, instead of the data.
Alternatively, an API that comes to mind is something like
```python
class SomeSchema(marshmallow.Schema):
x = fields.Integer()
y = fields.String()
class MoreSchema(marshmallow.Schema):
values = fields.List(fields.Nested(SomeSchema), deserialize=frozenset)
data = fields.Mapping(
keys=fields.String(),
values=fields.Integer(),
deserialize=collections.OrderedDict
)
```
Like `fields.Function`, these might also take a `serialize` argument, defaulting to `list` and `dict`, respectively.
This would generalize the existing idea of [`Mapping.mapping_type`](https://github.com/marshmallow-code/marshmallow/blob/dev/src/marshmallow/fields.py#L1272) , and would make it an instance attribute rather than a class attribute.
What is the recommended approach for deserializing to `Frozenset[int]` or `Frozenset[MyObject]`? Do you think an API like the idea above would be useful?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with src/marshmallow/fields.py, especially fields.List, fields.Mapping, and Mapping.mapping_type. Compare the proposed collection-type API with fields.Function and determine the expected behavior for frozenset values and nested objects. The issue does not define a settled API, implementation location, or tests, so the desired interface and acceptance criteria need clarification first.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend-api-design
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100