]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
Avoid unnecessary uses of `map()` avoid-silly-map 1180/head
authorAarni Koskela <akx@iki.fi>
Fri, 31 Jan 2025 13:31:25 +0000 (15:31 +0200)
committerAarni Koskela <akx@iki.fi>
Fri, 31 Jan 2025 13:31:25 +0000 (15:31 +0200)
babel/messages/catalog.py
babel/messages/checkers.py
babel/messages/extract.py
babel/messages/jslexer.py
babel/messages/pofile.py
babel/plural.py
tests/test_localedata.py

index aaff8df010eae61c563c1aefd53a625c5db1c37d..676a3ddceb161e22b5598ae95ba670e8c310d3f8 100644 (file)
@@ -217,10 +217,17 @@ class Message:
         return self.__dict__ == other.__dict__
 
     def clone(self) -> Message:
-        return Message(*map(copy, (self.id, self.string, self.locations,
-                                   self.flags, self.auto_comments,
-                                   self.user_comments, self.previous_id,
-                                   self.lineno, self.context)))
+        return Message(
+            id=copy(self.id),
+            string=copy(self.string),
+            locations=copy(self.locations),
+            flags=copy(self.flags),
+            auto_comments=copy(self.auto_comments),
+            user_comments=copy(self.user_comments),
+            previous_id=copy(self.previous_id),
+            lineno=self.lineno,  # immutable (str/None)
+            context=self.context,  # immutable (str/None)
+        )
 
     def check(self, catalog: Catalog | None = None) -> list[TranslationError]:
         """Run various validation checks on the message.  Some validations
index 79b3a6faa8549b4a7813513458199eaa3c9baccd..71fd01fe6689ea8253fd092f26459ba8b89c4626 100644 (file)
@@ -116,13 +116,15 @@ def _validate_format(format: str, alternative: str) -> None:
                                            'and named placeholders')
         return bool(positional)
 
-    a, b = map(_parse, (format, alternative))
+    a = _parse(format)
+    b = _parse(alternative)
 
     if not a:
         return
 
     # now check if both strings are positional or named
-    a_positional, b_positional = map(_check_positional, (a, b))
+    a_positional = _check_positional(a)
+    b_positional = _check_positional(b)
     if a_positional and not b_positional and not b:
         raise TranslationError('placeholders are incompatible')
     elif a_positional != b_positional:
index 6dd0313822a3381c33d0d992d9a0d53dd4eda69c..34d90f66e56e37744c6ff4b8a82a9fef324724df 100644 (file)
@@ -108,7 +108,7 @@ def _strip_comment_tags(comments: MutableSequence[str], tags: Iterable[str]):
             if line.startswith(tag):
                 return line[len(tag):].strip()
         return line
-    comments[:] = map(_strip, comments)
+    comments[:] = [_strip(c) for c in comments]
 
 
 def default_directory_filter(dirpath: str | os.PathLike[str]) -> bool:
index c7cf0577cc41597d6a3be54ccfae28126f978138..31b02c8525a1c82f991dc10c3c78853e78fb6f0a 100644 (file)
@@ -53,7 +53,7 @@ _rules: list[tuple[str | None, re.Pattern[str]]] = [
         (0x[a-fA-F0-9]+)
     )''', re.VERBOSE)),
     ('jsx_tag', re.compile(r'(?:</?[^>\s]+|/>)', re.I)),  # May be mangled in `get_rules`
-    ('operator', re.compile(r'(%s)' % '|'.join(map(re.escape, operators)))),
+    ('operator', re.compile(r'(%s)' % '|'.join(re.escape(op) for op in operators))),
     ('template_string', re.compile(r'''`(?:[^`\\]*(?:\\.[^`\\]*)*)`''', re.UNICODE)),
     ('string', re.compile(r'''(
         '(?:[^'\\]*(?:\\.[^'\\]*)*)'  |
index ad9b07ad0e5524c9738075e1eec71a12f3a237db..75905d57556f27daba728aa2c079456a04faaf4e 100644 (file)
@@ -73,8 +73,7 @@ def denormalize(string: str) -> str:
         escaped_lines = string.splitlines()
         if string.startswith('""'):
             escaped_lines = escaped_lines[1:]
-        lines = map(unescape, escaped_lines)
-        return ''.join(lines)
+        return ''.join(unescape(line) for line in escaped_lines)
     else:
         return unescape(string)
 
@@ -144,7 +143,7 @@ class _NormalizedString:
         self._strs.append(s.strip())
 
     def denormalize(self) -> str:
-        return ''.join(map(unescape, self._strs))
+        return ''.join(unescape(s) for s in self._strs)
 
     def __bool__(self) -> bool:
         return bool(self._strs)
index a2e30f456c8ca352b6c2202750b3ca7c7ec6b36e..1ee8b87caefb225f885264616c01f3c6c30db8aa 100644 (file)
@@ -586,7 +586,8 @@ class _GettextCompiler(_Compiler):
             if item[0] == item[1]:
                 rv.append(f"({expr} == {self.compile(item[0])})")
             else:
-                min, max = map(self.compile, item)
+                min = self.compile(item[0])
+                max = self.compile(item[1])
                 rv.append(f"({expr} >= {min} && {expr} <= {max})")
         return f"({' || '.join(rv)})"
 
index 4a1678467d197f0213a848445ddd389f7b6797ac..9856d7435e186f7082358664919738554eaf1318 100644 (file)
@@ -16,7 +16,6 @@ import random
 import sys
 import tempfile
 import unittest
-from operator import methodcaller
 
 import pytest
 
@@ -94,14 +93,13 @@ def test_unique_ids():
     all_ids = localedata.locale_identifiers()
     assert len(all_ids) == len(set(all_ids))
     # Check locale IDs don't collide after lower-case normalization.
-    lower_case_ids = list(map(methodcaller('lower'), all_ids))
+    lower_case_ids = [id.lower() for id in all_ids]
     assert len(lower_case_ids) == len(set(lower_case_ids))
 
 
 def test_mixedcased_locale():
     for locale in localedata.locale_identifiers():
-        locale_id = ''.join([
-            methodcaller(random.choice(['lower', 'upper']))(c) for c in locale])
+        locale_id = ''.join(c.lower() if random.random() < 0.5 else c.upper() for c in locale)
         assert localedata.exists(locale_id)