Skip to content

Conversation

radoering
Copy link

Resolves: #202

Comment on lines +301 to +304
def test_foo(self):
for i in range(5):
with self.subTest(msg=MyEnum.CUSTOM, i=i, p=Path("test")):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for being pedantic what happens if msg is dict of name to enum

Although i wouldn't expect people to use that people are always in for horrible surprises

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to work - probably because attr.asdict() handles dicts per default by converting their elements: https://github.com/python-attrs/attrs/blob/a296a129c60f5c736165086621f03c10a4d14065/src/attr/_funcs.py#L104-L126

Copy link
Member

@nicoddemus nicoddemus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @radoering!

Please take a look at my comment.

del data["context"]

def serialize(inst: type, field: attr.Attribute, value: Any) -> Any:
if isinstance(value, (Enum, Path)):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should not special-case Enum and Path, seems subTest parameters are reported as::

  • The msg parameter is always converted to string using str().
  • The dict contents are converted to string using repr().

The example below uses a custom class to demonstrate that.

import unittest
from enum import StrEnum, auto, Enum


class MyEnum(Enum):
    A = 1
    B = 2

class C:
    def __str__(self):
        return "C:str"
    def __repr__(self):
        return "C:repr"


class MyUnitTest(unittest.TestCase):

    def test_enum_values(self):
        for value in (C(), MyEnum.B):
            with self.subTest(value, p=value):
                assert False

if __name__ == '__main__':
    unittest.main()
test_enum_values (__main__.MyUnitTest.test_enum_values) ...
  test_enum_values (__main__.MyUnitTest.test_enum_values) [C:str] (p=C:repr) ... FAIL
  test_enum_values (__main__.MyUnitTest.test_enum_values) [MyEnum.B] (p=<MyEnum.B: 2>) ... FAIL

Ideally, we should find a way to perform the same conversions (str for msg and repr for the rest) during serialization.

Copy link
Author

@radoering radoering Aug 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should not special-case Enum and Path

That seems to be tricky. I tried to kind of invert the logic:

        def serialize(inst: type, field: attr.Attribute, value: Any) -> Any:
            if (
                value is None
                or attr.has(value.__class__)
                or isinstance(value, (tuple, list, set, frozenset, dict))
            ):
                return value
            elif isinstance(value, str):
                return str(value)
            return repr(value)

which results in

(p='C:repr') SUBFAIL tests/test_.py::MyUnitTest::test_enum_values - AssertionError: assert False
(p='<MyEnum.B: 2>') SUBFAIL tests/test_.py::MyUnitTest::test_enum_values - AssertionError: assert False

which is close but has additional apostrophes around the representations.

Further, it is quite brittle to list all types that should not be special-cased.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we should instead see how unittest.subTest produces this output:

test_enum_values (__main__.MyUnitTest.test_enum_values) ...
  test_enum_values (__main__.MyUnitTest.test_enum_values) [C:str] (p=C:repr) ... FAIL
  test_enum_values (__main__.MyUnitTest.test_enum_values) [MyEnum.B] (p=<MyEnum.B: 2>) ... FAIL

And follow the same approach.

Further, it is quite brittle to list all types that should not be special-cased.

Definitely agree!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Regression 0.14.0 -> 0.14.1 in combination with pytest-xdist: execnet.gateway_base.DumpError: can't serialize ...
3 participants