Skip to content

Commit 6494bb9

Browse files
committed
Merge remote-tracking branch 'origin/master' into pr/onursatici/172
2 parents 89d4c6b + 228f0e8 commit 6494bb9

File tree

5 files changed

+38
-12
lines changed

5 files changed

+38
-12
lines changed

.github/workflows/python-package.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
runs-on: ubuntu-latest
1212
strategy:
1313
matrix:
14-
python_version: [3.6, 3.7, 3.8, 3.9, pypy3]
14+
python_version: ["3.6", "3.7", "3.8", "3.9", "3.10", "pypy3"]
1515

1616
steps:
1717
- uses: actions/checkout@v2
@@ -31,7 +31,7 @@ jobs:
3131
python -m pip install --upgrade pip
3232
pip install --pre -e '.[dev]'
3333
- name: Pre-commit hooks
34-
if: ${{ matrix.python_version != 'pypy3' }}
34+
if: ${{ matrix.python_version != 'pypy3' && matrix.python_version != '3.6' }}
3535
run: pre-commit run --all-files
3636
- name: Test with pytest
3737
run: pytest

.pre-commit-config.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
repos:
22
- repo: https://github.com/asottile/pyupgrade
3-
rev: v2.7.4
3+
rev: v2.31.0 # Later versions do not support python 3.6
44
hooks:
55
- id: pyupgrade
66
args: ["--py36-plus"]
77
- repo: https://github.com/python/black
8-
rev: 20.8b1
8+
rev: 22.3.0
99
hooks:
1010
- id: black
1111
language_version: python3
1212
- repo: https://gitlab.com/pycqa/flake8
13-
rev: 3.8.4
13+
rev: 3.9.2
1414
hooks:
1515
- id: flake8
1616
additional_dependencies: ['flake8-bugbear==19.8.0']
1717
- repo: https://github.com/pre-commit/mirrors-mypy
18-
rev: v0.790
18+
rev: v0.931 # Later versions do not support python 3.6
1919
hooks:
2020
- id: mypy
2121
additional_dependencies: [marshmallow-enum,typeguard,marshmallow]
2222
args: [--show-error-codes]
2323
- repo: https://github.com/asottile/blacken-docs
24-
rev: v1.9.1
24+
rev: v1.12.0 # Later versions do not support python 3.6
2525
hooks:
2626
- id: blacken-docs
2727
additional_dependencies: [black==19.3b0]

README.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,32 @@ PersonSchema = marshmallow_dataclass.class_schema(Person)
9292

9393
The type of your fields must be either basic
9494
[types supported by marshmallow](https://marshmallow.readthedocs.io/en/stable/api_reference.html#marshmallow.Schema.TYPE_MAPPING)
95-
(such as `float`, `str`, `bytes`, `datetime`, ...), or other dataclasses.
95+
(such as `float`, `str`, `bytes`, `datetime`, ...), `Union`, or other dataclasses.
96+
97+
### Union (de)serialization coercion
98+
99+
Typically the Union type; `Union[X, Y]` means—from a set theory perspective—either `X` or `Y`, i.e., an unordered set, howevever the order of the sub-types defines the precedence when attempting to ether deserialize or serialize the value per [here](https://github.com/lovasoa/marshmallow_dataclass/blob/master/marshmallow_dataclass/union_field.py).
100+
101+
For example,
102+
103+
```python
104+
from typing import Union
105+
106+
from dataclasses import dataclass
107+
108+
109+
@dataclass
110+
class Person:
111+
name: str
112+
age: Union[int, float]
113+
114+
115+
PersonSchema = marshmallow_dataclass.class_schema(Person)
116+
PersonSchema().load({"name": "jane", "age": 50.0})
117+
# => Person(name="jane", age=50)
118+
```
119+
120+
will first (sucessfully) try to coerce `50.0` to an `int`. If coercion is not desired the `Any` type can be used with the caveat that values will not be type checked without additional [validation](https://marshmallow.readthedocs.io/en/stable/marshmallow.validate.html).
96121

97122
### Customizing generated fields
98123

marshmallow_dataclass/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -596,9 +596,10 @@ def field_for_schema(
596596
if generic_field:
597597
return generic_field
598598

599-
# typing.NewType returns a function with a __supertype__ attribute
599+
# typing.NewType returns a function (in python <= 3.9) or a class (python >= 3.10) with a
600+
# __supertype__ attribute
600601
newtype_supertype = getattr(typ, "__supertype__", None)
601-
if newtype_supertype and inspect.isfunction(typ):
602+
if typing_inspect.is_new_type(typ) and newtype_supertype is not None:
602603
return _field_by_supertype(
603604
typ=typ,
604605
default=default,

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
EXTRAS_REQUIRE = {
1919
"enum": ["marshmallow-enum"],
2020
"union": ["typeguard"],
21-
':python_version == "3.6"': ["dataclasses", "types-dataclasses"],
22-
"lint": ["pre-commit~=1.18"],
21+
"lint": ["pre-commit~=2.17"],
22+
':python_version == "3.6"': ["dataclasses", "types-dataclasses<0.6.4"],
2323
"docs": ["sphinx"],
2424
"tests": [
2525
"pytest>=5.4",

0 commit comments

Comments
 (0)