Skip to content

Commit 63d989c

Browse files
author
python-desert
authored
Support additional metadata in ib()/field()
2 parents 6d28b9d + dd9def7 commit 63d989c

File tree

4 files changed

+51
-3
lines changed

4 files changed

+51
-3
lines changed

CHANGELOG.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
2020.01.06 (2020-01-06)
2+
-----------------------
3+
4+
5+
Changes
6+
^^^^^^^
7+
8+
- Additional metadata are supported in ib() and fields().
9+
`#28 <https://github.com/python-desert/desert/issues/28>`_
10+
11+
12+
----
13+
14+
115
2020.01.05 (2020-01-05)
216
-----------------------
317

src/desert/__init__.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ def field(marshmallow_field: marshmallow.fields.Field, **kw) -> dataclasses.Fiel
6161
class A:
6262
x: int = desert.field(marshmallow.fields.Int())
6363
"""
64-
return dataclasses.field(**kw, metadata=metadata(marshmallow_field))
64+
meta = metadata(marshmallow_field)
65+
meta.update(kw.pop("metadata", {}))
66+
return dataclasses.field(**kw, metadata=meta)
6567

6668

6769
def ib(marshmallow_field: marshmallow.fields.Field, **kw) -> attr._make._CountingAttr:
@@ -73,7 +75,9 @@ def ib(marshmallow_field: marshmallow.fields.Field, **kw) -> attr._make._Countin
7375
class A:
7476
x: int = desert.ib(marshmallow.fields.Int())
7577
"""
76-
return attr.ib(**kw, metadata=metadata(marshmallow_field))
78+
meta = metadata(marshmallow_field)
79+
meta.update(kw.pop("metadata", {}))
80+
return attr.ib(**kw, metadata=meta)
7781

7882

7983
__version__ = desert._version.__version__

src/desert/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "2020.01.05"
1+
__version__ = "2020.01.06"

tests/test_make.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,36 @@ class A:
191191
assert schema.load({"x": timestring}) == A(x=dt)
192192

193193

194+
def test_concise_field_metadata():
195+
"""Concisely create a dataclasses.Field with metadata."""
196+
197+
@dataclasses.dataclass
198+
class A:
199+
x: str = desert.field(marshmallow.fields.NaiveDateTime(), metadata={"foo": 1})
200+
201+
timestring = "2019-10-21T10:25:00"
202+
dt = datetime.datetime(year=2019, month=10, day=21, hour=10, minute=25, second=00)
203+
schema = desert.schema(A)
204+
205+
assert schema.load({"x": timestring}) == A(x=dt)
206+
assert dataclasses.fields(A)[0].metadata["foo"] == 1
207+
208+
209+
def test_concise_attrib_metadata():
210+
"""Concisely create an attr.ib() with metadata."""
211+
212+
@attr.dataclass
213+
class A:
214+
x: str = desert.ib(marshmallow.fields.NaiveDateTime(), metadata={"foo": 1})
215+
216+
timestring = "2019-10-21T10:25:00"
217+
dt = datetime.datetime(year=2019, month=10, day=21, hour=10, minute=25, second=00)
218+
schema = desert.schema(A)
219+
220+
assert schema.load({"x": timestring}) == A(x=dt)
221+
assert attr.fields(A).x.metadata["foo"] == 1
222+
223+
194224
def test_union(module):
195225
"""Deserialize one of several types."""
196226

0 commit comments

Comments
 (0)