pytest-dev / pytest-dev/pytest
use `expand=True` for `pformat(...)` in python 3.15+
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 14.5k
- Forks
- 3.4k
- Avg merge
- 2d 9h
- Merged PRs (30d)
- 35
Description
What's the problem this feature will solve?
pformat is used in a few places throughout pytest (though apparently nothing actually tests these outputs because my attempt at a branch doesn't break any tests!) -- python3.15 adds expand: bool as an option which produces "better" reprs for nested objects:
import pprint
obj = {
'a' * 12: 1, 'b' * 20: 2,
'c' * 30: {'d' * 5: 3, 'e' * 20: 3, 'f' * 10: 5, 'g' * 20: 6},
}
pprint.pprint(obj)
print('===')
pprint.pprint(obj, expand=True)
{'aaaaaaaaaaaa': 1,
'bbbbbbbbbbbbbbbbbbbb': 2,
'cccccccccccccccccccccccccccccc': {'ddddd': 3,
'eeeeeeeeeeeeeeeeeeee': 3,
'ffffffffff': 5,
'gggggggggggggggggggg': 6}}
===
{
'aaaaaaaaaaaa': 1,
'bbbbbbbbbbbbbbbbbbbb': 2,
'cccccccccccccccccccccccccccccc': {
'ddddd': 3,
'eeeeeeeeeeeeeeeeeeee': 3,
'ffffffffff': 5,
'gggggggggggggggggggg': 6,
},
}
(this is, admittedly a cherry-picked example -- the expand option seems a lot more likely to go past the requested width option -- not really sure why -- for instance try comparing this object: {'a' * 12: 1, 'b' * 20: 2, 'c' * 30: {'d' * 5: 3, 'e' * 40: 3}})
Describe the solution you'd like
can probably run with something similar to this if it's a good idea -- but needs tests:
$ git diff -- src/_pytest/compat.py
diff --git a/src/_pytest/compat.py b/src/_pytest/compat.py
index d3b2a4696..47bdc4bd0 100644
--- a/src/_pytest/compat.py
+++ b/src/_pytest/compat.py
@@ -327,3 +327,13 @@ else:
return func
return decorator
+
+
+if sys.version_info >= (3, 15):
+ import pprint
+
+ pformat = functools.partial(pprint.pformat, expand=True)
+else:
+ import pprint
+
+ pformat = pprint.pformat
Alternative Solutions
shrugs
Additional context
there's definitely a duplicate of this but I could not find it :(
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in src/_pytest/compat.py and search pytest for its pformat uses. Review the proposed Python-version conditional and existing tests around formatted output, then add coverage for the relevant representations. Done means Python 3.15+ uses pprint's expand option while supported older versions continue to work.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- testing-qa
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 68/100