Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Message.python_brace_format #1169

Merged
merged 3 commits into from
Jan 16, 2025
Merged

Add Message.python_brace_format #1169

merged 3 commits into from
Jan 16, 2025

Conversation

tomasr8
Copy link
Member

@tomasr8 tomasr8 commented Jan 12, 2025

Relevant issue: #333

Part 1 of adding support for checking f-string parameters. This simply adds a new property on the Message object analogous to python_format which sets the corresponding flag. In a followup PR I'm going to add a checker for the f-string format (reusing my implementation from #1168)

@akx (I can't ask for a review so I'm tagging you ;))

Copy link

codecov bot commented Jan 12, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.37%. Comparing base (c2b397e) to head (2529cff).
Report is 16 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1169      +/-   ##
==========================================
+ Coverage   91.31%   91.37%   +0.05%     
==========================================
  Files          27       27              
  Lines        4654     4672      +18     
==========================================
+ Hits         4250     4269      +19     
+ Misses        404      403       -1     
Flag Coverage Δ
macos-14-3.10 90.38% <100.00%> (+0.05%) ⬆️
macos-14-3.11 90.32% <100.00%> (+0.05%) ⬆️
macos-14-3.12 90.53% <100.00%> (+0.05%) ⬆️
macos-14-3.13 90.53% <100.00%> (+0.05%) ⬆️
macos-14-3.8 90.25% <100.00%> (+0.05%) ⬆️
macos-14-3.9 90.31% <100.00%> (+0.05%) ⬆️
macos-14-pypy3.10 90.38% <100.00%> (+0.05%) ⬆️
ubuntu-24.04-3.10 90.41% <100.00%> (+0.05%) ⬆️
ubuntu-24.04-3.11 90.34% <100.00%> (+0.05%) ⬆️
ubuntu-24.04-3.12 90.56% <100.00%> (+0.05%) ⬆️
ubuntu-24.04-3.13 90.56% <100.00%> (+0.05%) ⬆️
ubuntu-24.04-3.8 90.27% <100.00%> (+0.05%) ⬆️
ubuntu-24.04-3.9 90.34% <100.00%> (+0.05%) ⬆️
ubuntu-24.04-pypy3.10 90.41% <100.00%> (+0.05%) ⬆️
windows-2022-3.10 90.42% <100.00%> (+0.05%) ⬆️
windows-2022-3.11 90.35% <100.00%> (+0.05%) ⬆️
windows-2022-3.12 90.57% <100.00%> (+0.05%) ⬆️
windows-2022-3.13 90.57% <100.00%> (+0.05%) ⬆️
windows-2022-3.8 90.39% <100.00%> (+0.05%) ⬆️
windows-2022-3.9 90.35% <100.00%> (+0.05%) ⬆️
windows-2022-pypy3.10 90.42% <100.00%> (+0.05%) ⬆️

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

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

@akx akx self-requested a review January 13, 2025 10:52
Comment on lines 73 to 79
def _has_python_brace_format(string: str) -> bool:
fmt = Formatter()
try:
parsed = list(fmt.parse(string))
except ValueError:
return False
return any(True for _, field_name, *_ in parsed if field_name is not None)
Copy link
Member

Choose a reason for hiding this comment

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

How about

Suggested change
def _has_python_brace_format(string: str) -> bool:
fmt = Formatter()
try:
parsed = list(fmt.parse(string))
except ValueError:
return False
return any(True for _, field_name, *_ in parsed if field_name is not None)
def _has_python_brace_format(string: str) -> bool:
if "{" not in string:
return False
fmt = Formatter()
try:
# `fmt.parse` returns 3-or-4-tuples of the form
# `(literal_text, field_name, format_spec, conversion)`;
# if `field_name` is set, this smells like brace format
return any(t[1] is not None for t in fmt.parse(string))
except ValueError:
return False

?

  • fast path first
  • no reason to gather the result to a list first that I can think of

Copy link
Member Author

Choose a reason for hiding this comment

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

Agreed on both, there is no need to convert the result to a list, it just didn't occur to me when I wrote it 😅

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, there is a subtle difference. Consider the string {} {. If you try to format it, you'll get a ValueError because of the unclosed second brace which makes it invalid. However, _has_python_brace_format will return True because the any short-circuits before the parser gets to the invalid brace.

This might be a bit surprising. Perhaps we should only return True if the whole string is valid?

Copy link
Member

Choose a reason for hiding this comment

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

Mm, fair! Maybe the any could be rewritten as a not all(...) (with a suitable comment above it to explain why the strange construction!) so we do always parse the entire thing?

Copy link
Member Author

Choose a reason for hiding this comment

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

I went for a loop in the end because I find it to be a bit easier to read compared to not all(...) but I'm happy to change it if you prefer!

tomasr8 and others added 2 commits January 13, 2025 22:32
Co-authored-by: Aarni Koskela <akx@iki.fi>
Copy link
Member

@akx akx left a comment

Choose a reason for hiding this comment

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

LGTM. A verbose for loop with comments is better than a hard-to-comprehend any()/all()/not all() expression!

@akx akx merged commit 98b9562 into python-babel:master Jan 16, 2025
26 checks passed
@akx akx added this to the Babel 2.17 milestone Jan 16, 2025
@tomasr8 tomasr8 deleted the braces branch January 16, 2025 11:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants