Skip to content

Commit 76711d7

Browse files
committed
Add suport for python-brace-format
Building upon python-babel#838, this adds support for python-brace-format flags in the python extractor. Fixes python-babel#333
1 parent 5c20283 commit 76711d7

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

babel/messages/extract.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import sys
2323
from tokenize import generate_tokens, COMMENT, NAME, OP, STRING
2424

25-
from babel.util import parse_encoding, parse_future_flags, pathmatch, has_python_format
25+
from babel.util import parse_encoding, parse_future_flags, pathmatch, has_python_format, has_python_brace_format
2626
from textwrap import dedent
2727

2828

@@ -480,6 +480,9 @@ def extract_python(fileobj, keywords, comment_tags, options):
480480
if has_python_format(message for message in messages if message):
481481
flags.add("python-format")
482482

483+
if has_python_brace_format(message for message in messages if message):
484+
flags.add('python-brace-format')
485+
483486
if len(messages) > 1:
484487
messages = tuple(messages)
485488
else:

babel/util.py

+14
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import re
1717
import textwrap
1818
import pytz as _pytz
19+
import string
1920
from babel import localtime
2021

2122
missing = object()
@@ -281,3 +282,16 @@ def has_python_format(ids):
281282
if isinstance(ids, str):
282283
ids = [ids]
283284
return any(PYTHON_FORMAT.search(id) for id in ids)
285+
286+
287+
FORMATTER = string.Formatter()
288+
289+
290+
def has_python_brace_format(ids):
291+
if isinstance(ids, str):
292+
ids = [ids]
293+
return any(
294+
field_name
295+
for message_id in ids
296+
for _, field_name, format_spec, conversion in FORMATTER.parse(message_id)
297+
)

tests/test_util.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import pytest
1919

2020
from babel import util
21-
from babel.util import parse_future_flags, has_python_format
21+
from babel.util import parse_future_flags, has_python_format, has_python_brace_format
2222

2323

2424
class _FF:
@@ -124,6 +124,23 @@ def test_has_python_format(ids):
124124

125125
@pytest.mark.parametrize('ids', [
126126
('foo',),
127+
('foo {name}',),
127128
])
128129
def test_not_has_python_format(ids):
129130
assert not has_python_format(ids)
131+
132+
@pytest.mark.parametrize('ids', [
133+
('foo {name} bar',),
134+
('foo {name:.3f} bar',),
135+
('foo {name!r:20} bar',),
136+
])
137+
def test_has_python_brace_format(ids):
138+
assert has_python_brace_format(ids)
139+
140+
@pytest.mark.parametrize('ids', [
141+
('foo',),
142+
('fo {}', ),
143+
('foo %d bar',),
144+
])
145+
def test_not_has_python_brace_format(ids):
146+
assert not has_python_brace_format(ids)

0 commit comments

Comments
 (0)