This repository was archived by the owner on Nov 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
Fix serialization of Enum and Path
#203
Open
radoering
wants to merge
2
commits into
pytest-dev:main
Choose a base branch
from
radoering:fix-serialization-issue
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -271,6 +271,76 @@ def test_simple_terminal_verbose( | |
| ] | ||
| ) | ||
|
|
||
| @pytest.mark.parametrize("runner", ["unittest", "pytest-normal", "pytest-xdist"]) | ||
| def test_serialization( | ||
| self, | ||
| pytester: pytest.Pytester, | ||
| runner: Literal["unittest", "pytest-normal", "pytest-xdist"], | ||
| ) -> None: | ||
| p = pytester.makepyfile( | ||
| """ | ||
| import sys | ||
| from unittest import TestCase, main | ||
| from pathlib import Path | ||
|
|
||
| if sys.version_info[:2] < (3, 11): | ||
| from enum import Enum | ||
|
|
||
| class MyEnum(str, Enum): | ||
| CUSTOM = "custom" | ||
|
|
||
| def __str__(self) -> str: | ||
| return self.value | ||
| else: | ||
| from enum import StrEnum | ||
|
|
||
| class MyEnum(StrEnum): | ||
| CUSTOM = "custom" | ||
|
|
||
| class T(TestCase): | ||
|
|
||
| def test_foo(self): | ||
| for i in range(5): | ||
| with self.subTest(msg=MyEnum.CUSTOM, i=i, p=Path("test")): | ||
|
Comment on lines
+301
to
+304
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems to work - probably because |
||
| self.assertEqual(i % 2, 0) | ||
|
|
||
| if __name__ == '__main__': | ||
| main() | ||
| """ | ||
| ) | ||
| suffix = ".test_foo" if IS_PY311 else "" | ||
| if runner == "unittest": | ||
| result = pytester.run(sys.executable, p) | ||
| result.stderr.fnmatch_lines( | ||
| [ | ||
| f"FAIL: test_foo (__main__.T{suffix}) [[]custom[]] (i=1, p=*)", | ||
| "AssertionError: 1 != 0", | ||
| f"FAIL: test_foo (__main__.T{suffix}) [[]custom[]] (i=3, p=*)", | ||
| "AssertionError: 1 != 0", | ||
| "Ran 1 test in *", | ||
| "FAILED (failures=2)", | ||
| ] | ||
| ) | ||
| else: | ||
| if runner == "pytest-normal": | ||
| result = pytester.runpytest(p) | ||
| expected_lines = ["collected 1 item"] | ||
| else: | ||
| assert runner == "pytest-xdist" | ||
| pytest.importorskip("xdist") | ||
| result = pytester.runpytest(p, "-n1") | ||
| expected_lines = ["1 worker [1 item]"] | ||
| result.stdout.fnmatch_lines( | ||
| expected_lines | ||
| + [ | ||
| "* T.test_foo [[]custom[]] (i=1, p=*) *", | ||
| "E * AssertionError: 1 != 0", | ||
| "* T.test_foo [[]custom[]] (i=3, p=*) *", | ||
| "E * AssertionError: 1 != 0", | ||
| "* 2 failed, 1 passed, 3 subtests passed in *", | ||
| ] | ||
| ) | ||
|
|
||
| @pytest.mark.parametrize("runner", ["unittest", "pytest-normal", "pytest-xdist"]) | ||
| def test_skip( | ||
| self, | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
EnumandPath, seemssubTestparameters are reported as::msgparameter is always converted to string usingstr().repr().The example below uses a custom class to demonstrate that.
Ideally, we should find a way to perform the same conversions (
strformsgandreprfor the rest) during serialization.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That seems to be tricky. I tried to kind of invert the logic:
which results in
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.
There was a problem hiding this comment.
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.subTestproduces this output:And follow the same approach.
Definitely agree!