summaryrefslogtreecommitdiff
path: root/babel
diff options
context:
space:
mode:
authorAarni Koskela <akx@iki.fi>2023-01-18 20:27:44 +0200
committerAarni Koskela <akx@iki.fi>2023-01-18 21:20:51 +0200
commit82d345a66e850f643607ac9fcafd9b9a2a89d1e1 (patch)
treead66b7a13fb05f30628b6710dacab554505d55e8 /babel
parentcb0269fc63f5af4ade770f7a8872de8090bf697d (diff)
downloadbabel-82d345a66e850f643607ac9fcafd9b9a2a89d1e1.tar.gz
Apply some small miscellaneous formatting fixes
Diffstat (limited to 'babel')
-rw-r--r--babel/core.py1
-rw-r--r--babel/dates.py10
-rw-r--r--babel/localtime/_helpers.py1
-rw-r--r--babel/messages/catalog.py4
-rw-r--r--babel/messages/extract.py7
-rw-r--r--babel/messages/frontend.py3
-rw-r--r--babel/messages/jslexer.py4
-rw-r--r--babel/messages/pofile.py9
-rw-r--r--babel/plural.py2
-rw-r--r--babel/support.py1
-rw-r--r--babel/units.py1
-rw-r--r--babel/util.py2
12 files changed, 26 insertions, 19 deletions
diff --git a/babel/core.py b/babel/core.py
index 604e5d9..ce564d7 100644
--- a/babel/core.py
+++ b/babel/core.py
@@ -46,6 +46,7 @@ if TYPE_CHECKING:
_global_data = None
_default_plural_rule = PluralRule({})
+
def _raise_no_data_error():
raise RuntimeError('The babel data files are not available. '
'This usually happens because you are using '
diff --git a/babel/dates.py b/babel/dates.py
index 26766a6..e626df5 100644
--- a/babel/dates.py
+++ b/babel/dates.py
@@ -1349,8 +1349,7 @@ def parse_date(
month_idx = format_str.index('l')
day_idx = format_str.index('d')
- indexes = [(year_idx, 'Y'), (month_idx, 'M'), (day_idx, 'D')]
- indexes.sort()
+ indexes = sorted([(year_idx, 'Y'), (month_idx, 'M'), (day_idx, 'D')])
indexes = {item[1]: idx for idx, item in enumerate(indexes)}
# FIXME: this currently only supports numbers, but should also support month
@@ -1399,8 +1398,7 @@ def parse_time(
min_idx = format_str.index('m')
sec_idx = format_str.index('s')
- indexes = [(hour_idx, 'H'), (min_idx, 'M'), (sec_idx, 'S')]
- indexes.sort()
+ indexes = sorted([(hour_idx, 'H'), (min_idx, 'M'), (sec_idx, 'S')])
indexes = {item[1]: idx for idx, item in enumerate(indexes)}
# TODO: support time zones
@@ -1436,7 +1434,7 @@ class DateTimePattern:
return pat
def __mod__(self, other: DateTimeFormat) -> str:
- if type(other) is not DateTimeFormat:
+ if not isinstance(other, DateTimeFormat):
return NotImplemented
return self.format % other
@@ -1829,7 +1827,7 @@ def parse_pattern(pattern: str) -> DateTimePattern:
:param pattern: the formatting pattern to parse
"""
- if type(pattern) is DateTimePattern:
+ if isinstance(pattern, DateTimePattern):
return pattern
if pattern in _pattern_cache:
diff --git a/babel/localtime/_helpers.py b/babel/localtime/_helpers.py
index b7238f6..159f9a5 100644
--- a/babel/localtime/_helpers.py
+++ b/babel/localtime/_helpers.py
@@ -24,6 +24,7 @@ def _get_tzinfo(tzenv: str):
return None
+
def _get_tzinfo_or_raise(tzenv: str):
tzinfo = _get_tzinfo(tzenv)
if tzinfo is None:
diff --git a/babel/messages/catalog.py b/babel/messages/catalog.py
index 85f16b2..dead4aa 100644
--- a/babel/messages/catalog.py
+++ b/babel/messages/catalog.py
@@ -240,6 +240,7 @@ DEFAULT_HEADER = """\
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#"""
+
def parse_separated_header(value: str) -> dict[str, str]:
# Adapted from https://peps.python.org/pep-0594/#cgi
from email.message import Message
@@ -736,7 +737,8 @@ class Catalog:
if key in self._messages:
del self._messages[key]
- def update(self,
+ def update(
+ self,
template: Catalog,
no_fuzzy_matching: bool = False,
update_header_comment: bool = False,
diff --git a/babel/messages/extract.py b/babel/messages/extract.py
index 0934937..453742e 100644
--- a/babel/messages/extract.py
+++ b/babel/messages/extract.py
@@ -90,7 +90,6 @@ DEFAULT_KEYWORDS: dict[str, _Keyword] = {
DEFAULT_MAPPING: list[tuple[str, str]] = [('**.py', 'python')]
-
def _strip_comment_tags(comments: MutableSequence[str], tags: Iterable[str]):
"""Helper function for `extract` that strips comment tags from strings
in a list of comment lines. This functions operates in-place.
@@ -660,8 +659,7 @@ def extract_javascript(
token = Token('operator', ')', token.lineno)
if options.get('parse_template_string') and not funcname and token.type == 'template_string':
- for item in parse_template_string(token.value, keywords, comment_tags, options, token.lineno):
- yield item
+ yield from parse_template_string(token.value, keywords, comment_tags, options, token.lineno)
elif token.type == 'operator' and token.value == '(':
if funcname:
@@ -794,8 +792,7 @@ def parse_template_string(
if level == 0 and expression_contents:
expression_contents = expression_contents[0:-1]
fake_file_obj = io.BytesIO(expression_contents.encode())
- for item in extract_javascript(fake_file_obj, keywords, comment_tags, options, lineno):
- yield item
+ yield from extract_javascript(fake_file_obj, keywords, comment_tags, options, lineno)
lineno += len(line_re.findall(expression_contents))
expression_contents = ''
prev_character = character
diff --git a/babel/messages/frontend.py b/babel/messages/frontend.py
index de17b1e..ab094ec 100644
--- a/babel/messages/frontend.py
+++ b/babel/messages/frontend.py
@@ -56,7 +56,6 @@ except ImportError:
from distutils.errors import DistutilsSetupError as SetupError
-
def listify_value(arg, split=None):
"""
Make a list out of an argument.
@@ -853,7 +852,7 @@ class update_catalog(Command):
omit_header=self.omit_header,
ignore_obsolete=self.ignore_obsolete,
include_previous=self.previous, width=self.width)
- except:
+ except Exception:
os.remove(tmpname)
raise
diff --git a/babel/messages/jslexer.py b/babel/messages/jslexer.py
index d2ffbbe..0563f62 100644
--- a/babel/messages/jslexer.py
+++ b/babel/messages/jslexer.py
@@ -32,11 +32,13 @@ line_join_re = re.compile(r'\\' + line_re.pattern)
uni_escape_re = re.compile(r'[a-fA-F0-9]{1,4}')
hex_escape_re = re.compile(r'[a-fA-F0-9]{1,2}')
+
class Token(NamedTuple):
type: str
value: str
lineno: int
+
_rules: list[tuple[str | None, re.Pattern[str]]] = [
(None, re.compile(r'\s+', re.UNICODE)),
(None, re.compile(r'<!--.*')),
@@ -100,7 +102,7 @@ def unquote_string(string: str) -> str:
add = result.append
pos = 0
- while 1:
+ while True:
# scan for the next escape
escape_pos = string.find('\\', pos)
if escape_pos < 0:
diff --git a/babel/messages/pofile.py b/babel/messages/pofile.py
index a9180a7..88cc043 100644
--- a/babel/messages/pofile.py
+++ b/babel/messages/pofile.py
@@ -134,7 +134,6 @@ class _NormalizedString:
return self.__cmp__(other) != 0
-
class PoFileParser:
"""Support class to read messages from a ``gettext`` PO (portable object) file
and add them to a `Catalog`
@@ -615,11 +614,13 @@ def write_po(
locs.append(location)
_write_comment(' '.join(locs), prefix=':')
if message.flags:
- _write('#%s\n' % ', '.join([''] + sorted(message.flags)))
+ _write(f"#{', '.join(['', *sorted(message.flags)])}\n")
if message.previous_id and include_previous:
- _write_comment('msgid %s' % _normalize(message.previous_id[0]),
- prefix='|')
+ _write_comment(
+ f'msgid {_normalize(message.previous_id[0])}',
+ prefix='|',
+ )
if len(message.previous_id) > 1:
_write_comment('msgid_plural %s' % _normalize(
message.previous_id[1]
diff --git a/babel/plural.py b/babel/plural.py
index fd0d0da..fe1ee25 100644
--- a/babel/plural.py
+++ b/babel/plural.py
@@ -325,6 +325,7 @@ def cldr_modulo(a: float, b: float) -> float:
class RuleError(Exception):
"""Raised if a rule is malformed."""
+
_VARS = {
'n', # absolute value of the source number.
'i', # integer digits of n.
@@ -363,6 +364,7 @@ def tokenize_rule(s: str) -> list[tuple[str, str]]:
'Got unexpected %r' % s[pos])
return result[::-1]
+
def test_next_token(
tokens: list[tuple[str, str]],
type_: str,
diff --git a/babel/support.py b/babel/support.py
index 7092599..242b492 100644
--- a/babel/support.py
+++ b/babel/support.py
@@ -35,6 +35,7 @@ if TYPE_CHECKING:
from babel.dates import _PredefinedTimeFormat
+
class Format:
"""Wrapper class providing the various date and number formatting functions
bound to a specific locale and time-zone.
diff --git a/babel/units.py b/babel/units.py
index 1180bd1..0c72ee9 100644
--- a/babel/units.py
+++ b/babel/units.py
@@ -9,6 +9,7 @@ from babel.numbers import LC_NUMERIC, format_decimal
if TYPE_CHECKING:
from typing_extensions import Literal
+
class UnknownUnitError(ValueError):
def __init__(self, unit: str, locale: Locale) -> None:
ValueError.__init__(self, f"{unit} is not a known unit in {locale}")
diff --git a/babel/util.py b/babel/util.py
index cf86f20..a5403e6 100644
--- a/babel/util.py
+++ b/babel/util.py
@@ -24,6 +24,7 @@ missing = object()
_T = TypeVar("_T")
+
def distinct(iterable: Iterable[_T]) -> Generator[_T, None, None]:
"""Yield all items in an iterable collection that are distinct.
@@ -43,6 +44,7 @@ def distinct(iterable: Iterable[_T]) -> Generator[_T, None, None]:
yield item
seen.add(item)
+
# Regexp to match python magic encoding line
PYTHON_MAGIC_COMMENT_re = re.compile(
br'[ \t\f]* \# .* coding[=:][ \t]*([-\w.]+)', re.VERBOSE)