Skip to content

Commit 32821d1

Browse files
committed
Apply manual formatting/fmt:skip to things that would be poorly autoformatted
1 parent 666947a commit 32821d1

File tree

15 files changed

+201
-149
lines changed

15 files changed

+201
-149
lines changed

babel/core.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def get_global(key: _GLOBAL_KEY) -> Mapping[str, Any]:
119119
'mk': 'mk_MK', 'nl': 'nl_NL', 'nn': 'nn_NO', 'no': 'nb_NO', 'pl': 'pl_PL',
120120
'pt': 'pt_PT', 'ro': 'ro_RO', 'ru': 'ru_RU', 'sk': 'sk_SK', 'sl': 'sl_SI',
121121
'sv': 'sv_SE', 'th': 'th_TH', 'tr': 'tr_TR', 'uk': 'uk_UA',
122-
}
122+
} # fmt: skip
123123

124124

125125
class UnknownLocaleError(Exception):
@@ -346,7 +346,8 @@ def parse(
346346
f"variables for the API you tried to use."
347347
)
348348
if isinstance(identifier, str):
349-
raise ValueError(msg) # `parse_locale` would raise a ValueError, so let's do that here
349+
# `parse_locale` would raise a ValueError, so let's do that here
350+
raise ValueError(msg)
350351
raise TypeError(msg)
351352

352353
if not isinstance(identifier, str):

babel/dates.py

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
_Instant: TypeAlias = datetime.date | datetime.time | float | None
4242
_PredefinedTimeFormat: TypeAlias = Literal['full', 'long', 'medium', 'short']
4343
_Context: TypeAlias = Literal['format', 'stand-alone']
44-
_DtOrTzinfo: TypeAlias = datetime.datetime | datetime.tzinfo | str | int | datetime.time | None
44+
_DtOrTzinfo: TypeAlias = datetime.datetime | datetime.tzinfo | str | int | datetime.time | None # fmt: skip
4545

4646
# "If a given short metazone form is known NOT to be understood in a given
4747
# locale and the parent locale has this value such that it would normally
@@ -1018,9 +1018,17 @@ def _format_fallback_interval(
10181018
) -> str:
10191019
if skeleton in locale.datetime_skeletons: # Use the given skeleton
10201020
format = lambda dt: format_skeleton(skeleton, dt, tzinfo, locale=locale)
1021-
elif all((isinstance(d, datetime.date) and not isinstance(d, datetime.datetime)) for d in (start, end)): # Both are just dates
1021+
elif all(
1022+
# Both are just dates
1023+
(isinstance(d, datetime.date) and not isinstance(d, datetime.datetime))
1024+
for d in (start, end)
1025+
):
10221026
format = lambda dt: format_date(dt, locale=locale)
1023-
elif all((isinstance(d, datetime.time) and not isinstance(d, datetime.date)) for d in (start, end)): # Both are times
1027+
elif all(
1028+
# Both are times
1029+
(isinstance(d, datetime.time) and not isinstance(d, datetime.date))
1030+
for d in (start, end)
1031+
):
10241032
format = lambda dt: format_time(dt, tzinfo=tzinfo, locale=locale)
10251033
else:
10261034
format = lambda dt: format_datetime(dt, tzinfo=tzinfo, locale=locale)
@@ -1266,8 +1274,11 @@ def parse_date(
12661274
use_predefined_format = format in ('full', 'long', 'medium', 'short')
12671275
# we try ISO-8601 format first, meaning similar to formats
12681276
# extended YYYY-MM-DD or basic YYYYMMDD
1269-
iso_alike = re.match(r'^(\d{4})-?([01]\d)-?([0-3]\d)$',
1270-
string, flags=re.ASCII) # allow only ASCII digits
1277+
iso_alike = re.match(
1278+
r'^(\d{4})-?([01]\d)-?([0-3]\d)$',
1279+
string,
1280+
flags=re.ASCII, # allow only ASCII digits
1281+
)
12711282
if iso_alike and use_predefined_format:
12721283
try:
12731284
return datetime.date(*map(int, iso_alike.groups()))
@@ -1637,35 +1648,24 @@ def format_timezone(self, char: str, num: int) -> str:
16371648
return get_timezone_gmt(value, width, locale=self.locale)
16381649
# TODO: To add support for O:1
16391650
elif char == 'v':
1640-
return get_timezone_name(value.tzinfo, width,
1641-
locale=self.locale)
1651+
return get_timezone_name(value.tzinfo, width, locale=self.locale)
16421652
elif char == 'V':
16431653
if num == 1:
1644-
return get_timezone_name(value.tzinfo, width,
1645-
uncommon=True, locale=self.locale)
1654+
return get_timezone_name(value.tzinfo, width, locale=self.locale)
16461655
elif num == 2:
16471656
return get_timezone_name(value.tzinfo, locale=self.locale, return_zone=True)
16481657
elif num == 3:
1649-
return get_timezone_location(value.tzinfo, locale=self.locale, return_city=True)
1658+
return get_timezone_location(value.tzinfo, locale=self.locale, return_city=True) # fmt: skip
16501659
return get_timezone_location(value.tzinfo, locale=self.locale)
1651-
# Included additional elif condition to add support for 'Xx' in timezone format
1652-
elif char == 'X':
1653-
if num == 1:
1654-
return get_timezone_gmt(value, width='iso8601_short', locale=self.locale,
1655-
return_z=True)
1656-
elif num in (2, 4):
1657-
return get_timezone_gmt(value, width='short', locale=self.locale,
1658-
return_z=True)
1659-
elif num in (3, 5):
1660-
return get_timezone_gmt(value, width='iso8601', locale=self.locale,
1661-
return_z=True)
1662-
elif char == 'x':
1660+
elif char in 'Xx':
1661+
return_z = char == 'X'
16631662
if num == 1:
1664-
return get_timezone_gmt(value, width='iso8601_short', locale=self.locale)
1663+
width = 'iso8601_short'
16651664
elif num in (2, 4):
1666-
return get_timezone_gmt(value, width='short', locale=self.locale)
1665+
width = 'short'
16671666
elif num in (3, 5):
1668-
return get_timezone_gmt(value, width='iso8601', locale=self.locale)
1667+
width = 'iso8601'
1668+
return get_timezone_gmt(value, width=width, locale=self.locale, return_z=return_z) # fmt: skip
16691669

16701670
def format(self, value: SupportsInt, length: int) -> str:
16711671
return '%0*d' % (length, value)
@@ -1739,7 +1739,7 @@ def get_week_number(self, day_of_period: int, day_of_week: int | None = None) ->
17391739
's': [1, 2], 'S': None, 'A': None, # second
17401740
'z': [1, 2, 3, 4], 'Z': [1, 2, 3, 4, 5], 'O': [1, 4], 'v': [1, 4], # zone
17411741
'V': [1, 2, 3, 4], 'x': [1, 2, 3, 4, 5], 'X': [1, 2, 3, 4, 5], # zone
1742-
}
1742+
} # fmt: skip
17431743

17441744
#: The pattern characters declared in the Date Field Symbol Table
17451745
#: (https://www.unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table)
@@ -1967,11 +1967,11 @@ def match_skeleton(skeleton: str, options: Iterable[str], allow_different_fields
19671967
if 'b' in skeleton and not any('b' in option for option in options):
19681968
skeleton = skeleton.replace('b', '')
19691969

1970-
get_input_field_width = dict(t[1] for t in tokenize_pattern(skeleton) if t[0] == "field").get
1970+
get_input_field_width = dict(t[1] for t in tokenize_pattern(skeleton) if t[0] == "field").get # fmt: skip
19711971
best_skeleton = None
19721972
best_distance = None
19731973
for option in options:
1974-
get_opt_field_width = dict(t[1] for t in tokenize_pattern(option) if t[0] == "field").get
1974+
get_opt_field_width = dict(t[1] for t in tokenize_pattern(option) if t[0] == "field").get # fmt: skip
19751975
distance = 0
19761976
for field in PATTERN_CHARS:
19771977
input_width = get_input_field_width(field, 0)
@@ -1982,13 +1982,18 @@ def match_skeleton(skeleton: str, options: Iterable[str], allow_different_fields
19821982
if not allow_different_fields: # This one is not okay
19831983
option = None
19841984
break
1985-
distance += 0x1000 # Magic weight constant for "entirely different fields"
1986-
elif field == 'M' and ((input_width > 2 and opt_width <= 2) or (input_width <= 2 and opt_width > 2)):
1987-
distance += 0x100 # Magic weight for "text turns into a number"
1985+
# Magic weight constant for "entirely different fields"
1986+
distance += 0x1000
1987+
elif field == 'M' and (
1988+
(input_width > 2 and opt_width <= 2) or (input_width <= 2 and opt_width > 2)
1989+
):
1990+
# Magic weight constant for "text turns into a number"
1991+
distance += 0x100
19881992
else:
19891993
distance += abs(input_width - opt_width)
19901994

1991-
if not option: # We lost the option along the way (probably due to "allow_different_fields")
1995+
if not option:
1996+
# We lost the option along the way (probably due to "allow_different_fields")
19921997
continue
19931998

19941999
if not best_skeleton or distance < best_distance:

babel/localtime/_fallback.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def tzname(self, dt: datetime.datetime) -> str:
3838
def _isdst(self, dt: datetime.datetime) -> bool:
3939
tt = (dt.year, dt.month, dt.day,
4040
dt.hour, dt.minute, dt.second,
41-
dt.weekday(), 0, -1)
41+
dt.weekday(), 0, -1) # fmt: skip
4242
stamp = time.mktime(tt)
4343
tt = time.localtime(stamp)
4444
return tt.tm_isdst > 0

babel/messages/catalog.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ def _get_mime_headers(self) -> list[tuple[str, str]]:
516516
('POT-Creation-Date', format_datetime(self.creation_date, 'yyyy-MM-dd HH:mmZ', locale='en')),
517517
('PO-Revision-Date', revision_date),
518518
('Last-Translator', self.last_translator),
519-
]
519+
] # fmt: skip
520520
if self.locale_identifier:
521521
headers.append(('Language', str(self.locale_identifier)))
522522
headers.append(('Language-Team', language_team))
@@ -739,13 +739,13 @@ def __setitem__(self, id: _MessageID, message: Message) -> None:
739739
current.id = message.id
740740
current.string = message.string
741741
current.locations = list(dict.fromkeys([*current.locations, *message.locations]))
742-
current.auto_comments = list(dict.fromkeys([*current.auto_comments, *message.auto_comments]))
743-
current.user_comments = list(dict.fromkeys([*current.user_comments, *message.user_comments]))
742+
current.auto_comments = list(dict.fromkeys([*current.auto_comments, *message.auto_comments])) # fmt:skip
743+
current.user_comments = list(dict.fromkeys([*current.user_comments, *message.user_comments])) # fmt:skip
744744
current.flags |= message.flags
745745
elif id == '':
746746
# special treatment for the header message
747747
self.mime_headers = message_from_string(message.string).items()
748-
self.header_comment = "\n".join([f"# {c}".rstrip() for c in message.user_comments])
748+
self.header_comment = "\n".join(f"# {c}".rstrip() for c in message.user_comments)
749749
self.fuzzy = message.fuzzy
750750
else:
751751
if isinstance(id, (list, tuple)):

babel/messages/checkers.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ def num_plurals(catalog: Catalog | None, message: Message) -> None:
2727
"""Verify the number of plurals in the translation."""
2828
if not message.pluralizable:
2929
if not isinstance(message.string, str):
30-
raise TranslationError("Found plural forms for non-pluralizable "
31-
"message")
30+
raise TranslationError("Found plural forms for non-pluralizable message")
3231
return
3332

3433
# skip further tests if no catalog is provided.
@@ -39,8 +38,9 @@ def num_plurals(catalog: Catalog | None, message: Message) -> None:
3938
if not isinstance(msgstrs, (list, tuple)):
4039
msgstrs = (msgstrs,)
4140
if len(msgstrs) != catalog.num_plurals:
42-
raise TranslationError("Wrong number of plural forms (expected %d)" %
43-
catalog.num_plurals)
41+
raise TranslationError(
42+
f"Wrong number of plural forms (expected {catalog.num_plurals})",
43+
)
4444

4545

4646
def python_format(catalog: Catalog | None, message: Message) -> None:
@@ -137,13 +137,13 @@ def _check_positional(results: list[tuple[str, str]]) -> bool:
137137
# same number of format chars and those must be compatible
138138
if a_positional:
139139
if len(a) != len(b):
140-
raise TranslationError('positional format placeholders are '
141-
'unbalanced')
140+
raise TranslationError('positional format placeholders are unbalanced')
142141
for idx, ((_, first), (_, second)) in enumerate(zip(a, b)):
143142
if not _compatible(first, second):
144-
raise TranslationError('incompatible format for placeholder '
145-
'%d: %r and %r are not compatible' %
146-
(idx + 1, first, second))
143+
raise TranslationError(
144+
f'incompatible format for placeholder {idx + 1:d}: '
145+
f'{first!r} and {second!r} are not compatible',
146+
)
147147

148148
# otherwise the second string must not have names the first one
149149
# doesn't have and the types of those included must be compatible

babel/messages/extract.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def tell(self) -> int: ...
6262
_Keyword: TypeAlias = dict[int | None, _SimpleKeyword] | _SimpleKeyword
6363

6464
# 5-tuple of (filename, lineno, messages, comments, context)
65-
_FileExtractionResult: TypeAlias = tuple[str, int, str | tuple[str, ...], list[str], str | None]
65+
_FileExtractionResult: TypeAlias = tuple[str, int, str | tuple[str, ...], list[str], str | None] # fmt: skip
6666

6767
# 4-tuple of (lineno, message, comments, context)
6868
_ExtractionResult: TypeAlias = tuple[int, str | tuple[str, ...], list[str], str | None]
@@ -72,7 +72,7 @@ def tell(self) -> int: ...
7272
_CallableExtractionMethod: TypeAlias = Callable[
7373
[_FileObj | IO[bytes], Mapping[str, _Keyword], Collection[str], Mapping[str, Any]],
7474
Iterable[_ExtractionResult],
75-
]
75+
] # fmt: skip
7676

7777
_ExtractionMethod: TypeAlias = _CallableExtractionMethod | str
7878

@@ -696,9 +696,12 @@ def extract_javascript(
696696
lineno=lineno,
697697
):
698698
if ( # Turn keyword`foo` expressions into keyword("foo") calls:
699-
funcname and # have a keyword...
700-
(last_token and last_token.type == 'name') and # we've seen nothing after the keyword...
701-
token.type == 'template_string' # this is a template string
699+
# have a keyword...
700+
funcname
701+
# and we've seen nothing after the keyword...
702+
and (last_token and last_token.type == 'name')
703+
# and this is a template string
704+
and token.type == 'template_string'
702705
):
703706
message_lineno = token.lineno
704707
messages = [unquote_string(token.value)]

0 commit comments

Comments
 (0)