Skip to content

Commit af0ff66

Browse files
authored
Avoid unnecessary uses of map() (#1180)
1 parent 27e7303 commit af0ff66

File tree

7 files changed

+23
-16
lines changed

7 files changed

+23
-16
lines changed

babel/messages/catalog.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,17 @@ def is_identical(self, other: Message) -> bool:
217217
return self.__dict__ == other.__dict__
218218

219219
def clone(self) -> Message:
220-
return Message(*map(copy, (self.id, self.string, self.locations,
221-
self.flags, self.auto_comments,
222-
self.user_comments, self.previous_id,
223-
self.lineno, self.context)))
220+
return Message(
221+
id=copy(self.id),
222+
string=copy(self.string),
223+
locations=copy(self.locations),
224+
flags=copy(self.flags),
225+
auto_comments=copy(self.auto_comments),
226+
user_comments=copy(self.user_comments),
227+
previous_id=copy(self.previous_id),
228+
lineno=self.lineno, # immutable (str/None)
229+
context=self.context, # immutable (str/None)
230+
)
224231

225232
def check(self, catalog: Catalog | None = None) -> list[TranslationError]:
226233
"""Run various validation checks on the message. Some validations

babel/messages/checkers.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,15 @@ def _check_positional(results: list[tuple[str, str]]) -> bool:
116116
'and named placeholders')
117117
return bool(positional)
118118

119-
a, b = map(_parse, (format, alternative))
119+
a = _parse(format)
120+
b = _parse(alternative)
120121

121122
if not a:
122123
return
123124

124125
# now check if both strings are positional or named
125-
a_positional, b_positional = map(_check_positional, (a, b))
126+
a_positional = _check_positional(a)
127+
b_positional = _check_positional(b)
126128
if a_positional and not b_positional and not b:
127129
raise TranslationError('placeholders are incompatible')
128130
elif a_positional != b_positional:

babel/messages/extract.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def _strip(line: str):
108108
if line.startswith(tag):
109109
return line[len(tag):].strip()
110110
return line
111-
comments[:] = map(_strip, comments)
111+
comments[:] = [_strip(c) for c in comments]
112112

113113

114114
def default_directory_filter(dirpath: str | os.PathLike[str]) -> bool:

babel/messages/jslexer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class Token(NamedTuple):
5353
(0x[a-fA-F0-9]+)
5454
)''', re.VERBOSE)),
5555
('jsx_tag', re.compile(r'(?:</?[^>\s]+|/>)', re.I)), # May be mangled in `get_rules`
56-
('operator', re.compile(r'(%s)' % '|'.join(map(re.escape, operators)))),
56+
('operator', re.compile(r'(%s)' % '|'.join(re.escape(op) for op in operators))),
5757
('template_string', re.compile(r'''`(?:[^`\\]*(?:\\.[^`\\]*)*)`''', re.UNICODE)),
5858
('string', re.compile(r'''(
5959
'(?:[^'\\]*(?:\\.[^'\\]*)*)' |

babel/messages/pofile.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ def denormalize(string: str) -> str:
7373
escaped_lines = string.splitlines()
7474
if string.startswith('""'):
7575
escaped_lines = escaped_lines[1:]
76-
lines = map(unescape, escaped_lines)
77-
return ''.join(lines)
76+
return ''.join(unescape(line) for line in escaped_lines)
7877
else:
7978
return unescape(string)
8079

@@ -144,7 +143,7 @@ def append(self, s: str) -> None:
144143
self._strs.append(s.strip())
145144

146145
def denormalize(self) -> str:
147-
return ''.join(map(unescape, self._strs))
146+
return ''.join(unescape(s) for s in self._strs)
148147

149148
def __bool__(self) -> bool:
150149
return bool(self._strs)

babel/plural.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,8 @@ def compile_relation(self, method, expr, range_list):
586586
if item[0] == item[1]:
587587
rv.append(f"({expr} == {self.compile(item[0])})")
588588
else:
589-
min, max = map(self.compile, item)
589+
min = self.compile(item[0])
590+
max = self.compile(item[1])
590591
rv.append(f"({expr} >= {min} && {expr} <= {max})")
591592
return f"({' || '.join(rv)})"
592593

tests/test_localedata.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import sys
1717
import tempfile
1818
import unittest
19-
from operator import methodcaller
2019

2120
import pytest
2221

@@ -94,14 +93,13 @@ def test_unique_ids():
9493
all_ids = localedata.locale_identifiers()
9594
assert len(all_ids) == len(set(all_ids))
9695
# Check locale IDs don't collide after lower-case normalization.
97-
lower_case_ids = list(map(methodcaller('lower'), all_ids))
96+
lower_case_ids = [id.lower() for id in all_ids]
9897
assert len(lower_case_ids) == len(set(lower_case_ids))
9998

10099

101100
def test_mixedcased_locale():
102101
for locale in localedata.locale_identifiers():
103-
locale_id = ''.join([
104-
methodcaller(random.choice(['lower', 'upper']))(c) for c in locale])
102+
locale_id = ''.join(c.lower() if random.random() < 0.5 else c.upper() for c in locale)
105103
assert localedata.exists(locale_id)
106104

107105

0 commit comments

Comments
 (0)