Skip to content

Commit fe8a756

Browse files
tomasr8akx
andauthored
Optimize checking for format parameters
Co-authored-by: Aarni Koskela <[email protected]>
1 parent 08512c7 commit fe8a756

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

babel/messages/catalog.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,16 @@ def get_close_matches(word, possibilities, n=3, cutoff=0.6):
7171

7272

7373
def _has_python_brace_format(string: str) -> bool:
74+
if "{" not in string:
75+
return False
7476
fmt = Formatter()
7577
try:
76-
parsed = list(fmt.parse(string))
78+
# `fmt.parse` returns 3-or-4-tuples of the form
79+
# `(literal_text, field_name, format_spec, conversion)`;
80+
# if `field_name` is set, this smells like brace format
81+
return any(t[1] is not None for t in fmt.parse(string))
7782
except ValueError:
7883
return False
79-
return any(True for _, field_name, *_ in parsed if field_name is not None)
8084

8185

8286
def _parse_datetime_header(value: str) -> datetime.datetime:

0 commit comments

Comments
 (0)