Skip to content

Commit f315d71

Browse files
dairikilovasoa
andauthored
Do not ignore Union argument ordering in tests (#248)
* Revert "Ignore union ordering" This reverts commit b61cd50. * test: mark expected failure, add separate test of its non-failing bits * ci(tests): report skipped/xfailed tests * docs(test): add link for context Co-authored-by: Ophir LOJKINE <contact@ophir.dev> --------- Co-authored-by: Ophir LOJKINE <contact@ophir.dev>
1 parent 260c4fe commit f315d71

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

.github/workflows/python-package.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,4 @@ jobs:
4747
if: ${{ matrix.python_version != 'pypy3' && matrix.python_version != '3.6' }}
4848
run: pre-commit run --all-files
4949
- name: Test with pytest
50-
run: pytest
50+
run: pytest -ra

tests/test_field_for_schema.py

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import copy
12
import inspect
23
import sys
34
import typing
@@ -26,18 +27,11 @@ class TestFieldForSchema(unittest.TestCase):
2627
def assertFieldsEqual(self, a: fields.Field, b: fields.Field):
2728
self.assertEqual(a.__class__, b.__class__, "field class")
2829

29-
def canonical(k, v):
30-
if k == "union_fields":
31-
# See https://github.com/lovasoa/marshmallow_dataclass/pull/246#issuecomment-1722291806
32-
return k, sorted(map(repr, v))
33-
elif inspect.isclass(v):
34-
return k, f"{v!r} ({v.__mro__!r})"
35-
else:
36-
return k, repr(v)
37-
3830
def attrs(x):
3931
return sorted(
40-
canonical(k, v) for k, v in vars(x).items() if not k.startswith("_")
32+
(k, f"{v!r} ({v.__mro__!r})" if inspect.isclass(v) else repr(v))
33+
for k, v in x.__dict__.items()
34+
if not k.startswith("_")
4135
)
4236

4337
self.assertEqual(attrs(a), attrs(b))
@@ -189,7 +183,11 @@ def test_union_multiple_types_with_none(self):
189183
),
190184
)
191185

186+
@unittest.expectedFailure
192187
def test_optional_multiple_types(self):
188+
# excercise bug (see #247)
189+
Optional[Union[str, int]]
190+
193191
self.assertFieldsEqual(
194192
field_for_schema(Optional[Union[int, str]]),
195193
union_field.Union(
@@ -203,6 +201,26 @@ def test_optional_multiple_types(self):
203201
),
204202
)
205203

204+
def test_optional_multiple_types_ignoring_union_field_order(self):
205+
# see https://github.com/lovasoa/marshmallow_dataclass/pull/246#issuecomment-1722204048
206+
result = field_for_schema(Optional[Union[int, str]])
207+
expected = union_field.Union(
208+
[
209+
(int, fields.Integer(required=True)),
210+
(str, fields.String(required=True)),
211+
],
212+
required=False,
213+
dump_default=None,
214+
load_default=None,
215+
)
216+
217+
def sort_union_fields(field):
218+
rv = copy.copy(field)
219+
rv.union_fields = sorted(field.union_fields, key=repr)
220+
return rv
221+
222+
self.assertFieldsEqual(sort_union_fields(result), sort_union_fields(expected))
223+
206224
def test_newtype(self):
207225
self.assertFieldsEqual(
208226
field_for_schema(typing.NewType("UserId", int), default=0),

0 commit comments

Comments
 (0)