Skip to content

Commit f5d3a64

Browse files
committed
chore(formats): simplify PoHeaderMixin typing
Avoid making it generic, but rather type limit the store.
1 parent 9b8e470 commit f5d3a64

1 file changed

Lines changed: 17 additions & 15 deletions

File tree

weblate/formats/ttkit.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@
118118
PO_DOCSTRING_LOCATION = re.compile(r":docstring of [a-zA-Z0-9._]+:[0-9]+")
119119
XLIFF_FUZZY_STATES = {"new", "needs-translation", "needs-adaptation", "needs-l10n"}
120120
_CSV_MAX_PLURAL_FORMS = 100
121+
type PoHeaderStore = pofile | PoXliffFile
122+
type PoHeaderUnit = pounit | PoXliffUnit
121123

122124

123125
class CSVMetadataError(ValueError):
@@ -1542,17 +1544,16 @@ def set_state(self, state) -> None:
15421544
self.unit.marktranslatable(False)
15431545

15441546

1545-
class PoHeaderMixin[
1546-
S: pofile | PoXliffFile,
1547-
U: pounit | PoXliffUnit,
1548-
T: TTKitUnit,
1549-
](TTKitFormat[S, U, T]):
1547+
class PoHeaderMixin:
15501548
def _po_header_store(self) -> poheader:
1551-
return self.store
1549+
return cast("poheader", cast("Any", self).store)
15521550

1553-
def _ensure_po_header_first(self, header: U | None = None) -> U | None:
1551+
def _ensure_po_header_first(
1552+
self, header: PoHeaderUnit | None = None
1553+
) -> PoHeaderUnit | None:
15541554
"""Keep PO-style header in the position expected by Translate Toolkit."""
1555-
units = self.store.units
1555+
store = cast("PoHeaderStore", cast("Any", self).store)
1556+
units = store.units
15561557
if header is None:
15571558
header = next(
15581559
(unit for unit in units if getattr(unit, "isheader", lambda: False)()),
@@ -1585,7 +1586,7 @@ def get_plural(self, language: Language) -> Plural:
15851586
try:
15861587
number, formula = Plural.parse_plural_forms(header["Plural-Forms"])
15871588
except (ValueError, KeyError):
1588-
return super().get_plural(language)
1589+
return cast("TTKitFormat[Any, Any, Any]", super()).get_plural(language)
15891590

15901591
# Find matching one
15911592
for plural in language.plural_set.iterator():
@@ -1604,7 +1605,9 @@ def untranslate_store(
16041605
self, language: Language, file_format_params: FileFormatParams | None = None
16051606
) -> None:
16061607
"""Remove translations from Translate Toolkit store."""
1607-
super().untranslate_store(language, file_format_params=file_format_params)
1608+
cast("TTKitFormat[Any, Any, Any]", super()).untranslate_store(
1609+
language, file_format_params=file_format_params
1610+
)
16081611
plural = language.plural
16091612
store = self._po_header_store()
16101613

@@ -1640,7 +1643,9 @@ def update_header(self, file_format_params: FileFormatParams, **kwargs) -> None:
16401643
self._ensure_po_header_first(store.updateheader(add=True, **kwargs))
16411644

16421645

1643-
class BasePoFormat[S: pofile, U: pounit, T: BasePoUnit](PoHeaderMixin[S, U, T]):
1646+
class BasePoFormat[S: pofile, U: pounit, T: BasePoUnit](
1647+
PoHeaderMixin, TTKitFormat[S, U, T]
1648+
):
16441649
loader = pofile # type: ignore[assignment]
16451650
plural_preference: tuple[int, ...] | None = None
16461651
supports_plural: bool = True
@@ -1832,10 +1837,7 @@ class RichXliffFormat(XliffFormat):
18321837
unit_class = RichXliffUnit
18331838

18341839

1835-
class PoXliffFormat(
1836-
PoHeaderMixin[PoXliffFile, PoXliffUnit, XliffUnit[PoXliffUnit, XliffFormat]],
1837-
XliffFormat,
1838-
):
1840+
class PoXliffFormat(PoHeaderMixin, XliffFormat):
18391841
# Translators: File format name
18401842
name = gettext_lazy("XLIFF 1.2 with gettext extensions")
18411843
format_id = "poxliff"

0 commit comments

Comments
 (0)