Skip to content

Commit 32299c8

Browse files
authored
A couple of fixes to make CI tests more resilient (#211)
* Explicitly set PYTHONPATH=. in mypy tests This prevents issues when marshmallow_dataclass is installed in editable mode. (See #209, #207.) * Don't fail tests upon warnings from external packages. E.g. marshmallow<3.15 generates a deprecation warning from distutils. There's no reason that should cause a test failure for us. * Update black target-version config. I have no idea whether this actually affects anything, but might as well keep it up-to-date. * Fix typing of Union._serialize parameter This tracks a change in type of the `attr` parameter to `Field._serialize` made in marshmallow 3.18.0. Here we also switch to using keyword args to typeguard.check_type in an attempt to protect against upcoming [changes in its signature](https://typeguard.readthedocs.io/en/latest/api.html#typeguard.check_type). * Cherry-pick PR #207: Fix broken user types after typing-inspect 0.8.0 This is cherry-picks PR#207 by @vit-zikmund
1 parent ad4d654 commit 32299c8

File tree

4 files changed

+17
-10
lines changed

4 files changed

+17
-10
lines changed

marshmallow_dataclass/__init__.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class User:
4848
Dict,
4949
List,
5050
Mapping,
51+
NewType as typing_NewType,
5152
Optional,
5253
Set,
5354
Tuple,
@@ -805,12 +806,8 @@ def NewType(
805806
marshmallow.exceptions.ValidationError: {'mail': ['Not a valid email address.']}
806807
"""
807808

808-
def new_type(x: _U):
809-
return x
810-
811-
new_type.__name__ = name
812809
# noinspection PyTypeHints
813-
new_type.__supertype__ = typ # type: ignore
810+
new_type = typing_NewType(name, typ) # type: ignore
814811
# noinspection PyTypeHints
815812
new_type._marshmallow_field = field # type: ignore
816813
# noinspection PyTypeHints

marshmallow_dataclass/union_field.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,15 @@ def _bind_to_schema(self, field_name: str, schema: Schema) -> None:
3434

3535
self.union_fields = new_union_fields
3636

37-
def _serialize(self, value: Any, attr: str, obj, **kwargs) -> Any:
37+
def _serialize(self, value: Any, attr: Optional[str], obj, **kwargs) -> Any:
3838
errors = []
3939
if value is None:
4040
return value
4141
for typ, field in self.union_fields:
4242
try:
43-
typeguard.check_type(attr, value, typ)
43+
typeguard.check_type(
44+
value=value, expected_type=typ, argname=attr or "anonymous"
45+
)
4446
return field._serialize(value, attr, obj, **kwargs)
4547
except TypeError as e:
4648
errors.append(e)
@@ -53,7 +55,9 @@ def _deserialize(self, value: Any, attr: Optional[str], data, **kwargs) -> Any:
5355
for typ, field in self.union_fields:
5456
try:
5557
result = field.deserialize(value, **kwargs)
56-
typeguard.check_type(attr or "anonymous", result, typ)
58+
typeguard.check_type(
59+
value=result, expected_type=typ, argname=attr or "anonymous"
60+
)
5761
return result
5862
except (TypeError, ValidationError) as e:
5963
errors.append(e)

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
[tool.black]
22
line-length = 88
3-
target-version = ['py36', 'py37', 'py38']
3+
target-version = ['py36', 'py37', 'py38', 'py39', 'py310']
44

55
[tool.pytest.ini_options]
66
filterwarnings = [
7-
"error",
7+
"error:::marshmallow_dataclass|test",
88
]

tests/test_mypy.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
mypy_config: |
66
follow_imports = silent
77
plugins = marshmallow_dataclass.mypy
8+
env:
9+
- PYTHONPATH=.
810
main: |
911
from dataclasses import dataclass
1012
import marshmallow as ma
@@ -28,6 +30,8 @@
2830
mypy_config: |
2931
follow_imports = silent
3032
plugins = marshmallow_dataclass.mypy
33+
env:
34+
- PYTHONPATH=.
3135
main: |
3236
from marshmallow_dataclass import dataclass
3337
@@ -41,6 +45,8 @@
4145
mypy_config: |
4246
follow_imports = silent
4347
plugins = marshmallow_dataclass.mypy
48+
env:
49+
- PYTHONPATH=.
4450
main: |
4551
from dataclasses import dataclass
4652
import marshmallow

0 commit comments

Comments
 (0)