Skip to content

Support format specs and pickling in LazyProxy - #1499

Open
chrikrah wants to merge 1 commit into
python-babel:masterfrom
chrikrah:fix/lazyproxy-format-pickle
Open

chrikrah wants to merge 1 commit into
python-babel:masterfrom
chrikrah:fix/lazyproxy-format-pickle

Conversation

@chrikrah

Copy link
Copy Markdown
Contributor

A format spec on LazyProxy raises, though '(%s)' % lazy_greeting works. Unpickling recurses. No issue exists. At b778b94:

$ python -c 'from babel.support import LazyProxy; lazy = LazyProxy(str.upper, "name"); print(f"{lazy:>8}|")'
TypeError: unsupported format string passed to LazyProxy.__format__
$ python -c 'import pickle; from babel.support import LazyProxy; lazy = LazyProxy(str.upper, "name"); pickle.loads(pickle.dumps(lazy))'
RecursionError: maximum recursion depth exceeded

__format__ delegates; __reduce__ rebuilds through __init__, as __copy__ has since #100. Proxies over Translations().gettext still won't pickle.

$ python -X utf8 -m pytest -q   # Python 3.15.2
7075 passed, 1218 skipped, 1 xfailed in 11.93s
# b778b94 alone: 7072 passed, 1218 skipped, 1 xfailed
# support.py reverted, tests kept: the 3 new tests fail
$ pre-commit run --all-files   # ruff v0.14.10
ruff check...............................................................Passed

@akx, I'd re-evaluate on load, since the locale may differ. If pickling is out of scope, shall I drop __reduce__?

LazyProxy had no __format__, so f"{proxy:>8}" raised TypeError, and
unpickling one recursed in __getattr__ on the unset slots. Delegate
__format__ to the value and rebuild through __init__ in __reduce__.

Signed-off-by: Christopher Krah <48338417+chrikrah@users.noreply.github.com>
@codspeed

codspeed Bot commented Nov 27, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 88 untouched benchmarks


Comparing chrikrah:fix/lazyproxy-format-pickle (ada1743) with master (b778b94)

Open in CodSpeed

@codecov

codecov Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.09%. Comparing base (b778b94) to head (ada1743).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1499   +/-   ##
=======================================
  Coverage   94.09%   94.09%           
=======================================
  Files          27       27           
  Lines        5077     5083    +6     
=======================================
+ Hits         4777     4783    +6     
  Misses        300      300           
Flag Coverage Δ
macos-26-3.10 93.19% <100.00%> (+<0.01%) ⬆️
macos-26-3.11 93.13% <100.00%> (+<0.01%) ⬆️
macos-26-3.12 93.38% <100.00%> (+<0.01%) ⬆️
macos-26-3.13 93.38% <100.00%> (+<0.01%) ⬆️
macos-26-3.14 93.37% <100.00%> (+<0.01%) ⬆️
macos-26-3.15 93.37% <100.00%> (+<0.01%) ⬆️
macos-26-3.9 93.12% <100.00%> (+<0.01%) ⬆️
macos-26-pypy3.11 93.13% <100.00%> (+<0.01%) ⬆️
macos-26-pypy3.12 93.38% <100.00%> (+<0.01%) ⬆️
ubuntu-24.04-3.10 93.21% <100.00%> (+<0.01%) ⬆️
ubuntu-24.04-3.11 93.15% <100.00%> (+<0.01%) ⬆️
ubuntu-24.04-3.12 93.40% <100.00%> (+<0.01%) ⬆️
ubuntu-24.04-3.13 93.40% <100.00%> (+<0.01%) ⬆️
ubuntu-24.04-3.14 93.38% <100.00%> (+<0.01%) ⬆️
ubuntu-24.04-3.15 93.38% <100.00%> (+<0.01%) ⬆️
ubuntu-24.04-3.9 93.14% <100.00%> (+<0.01%) ⬆️
ubuntu-24.04-pypy3.11 93.15% <100.00%> (+<0.01%) ⬆️
ubuntu-24.04-pypy3.12 93.40% <100.00%> (+<0.01%) ⬆️
windows-2025-3.10 93.20% <100.00%> (+<0.01%) ⬆️
windows-2025-3.11 93.14% <100.00%> (+<0.01%) ⬆️
windows-2025-3.12 93.40% <100.00%> (+<0.01%) ⬆️
windows-2025-3.13 93.40% <100.00%> (+<0.01%) ⬆️
windows-2025-3.14 93.38% <100.00%> (+<0.01%) ⬆️
windows-2025-3.15 93.38% <100.00%> (+<0.01%) ⬆️
windows-2025-3.9 93.14% <100.00%> (+<0.01%) ⬆️
windows-2025-pypy3.11 93.14% <100.00%> (+<0.01%) ⬆️
windows-2025-pypy3.12 93.40% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@akx

akx commented Nov 27, 2026

Copy link
Copy Markdown
Member

Sorry, are these actual issues for some use cases?

@chrikrah chrikrah left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No user report behind it. The class docstring says the lazy objects "should mostly work as expected" (support.py#L270-L271), and its doctests show + and %. On master (b778b94) f"{p:<5}" is where that stops:

$ python3 -c "from babel.support import LazyProxy; p = LazyProxy(lambda: 'Hi'); print(repr('  ' + p), repr('(%s)' % p)); f'{p:<5}'"
'  Hi' '(Hi)'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
TypeError: unsupported format string passed to LazyProxy.__format__

Pickling has no caller I can point to. If the format spec is worth keeping I'll cut the PR down to __format__; if not, I'll close it. Either is fine with me.

This branch has not been deployed

No deployments
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.

2 participants