Skip to content

Commit

Permalink
chore: add type annotations to Unit.translate
Browse files Browse the repository at this point in the history
Also makes some args kwarg only.
  • Loading branch information
nijel committed Dec 11, 2024
1 parent 99aeec3 commit 59e9dcc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
25 changes: 18 additions & 7 deletions weblate/trans/autotranslate.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@
from django.db import transaction
from django.db.models.functions import MD5, Lower

from weblate.machinery.base import MachineTranslationError
from weblate.machinery.base import BatchMachineTranslation, MachineTranslationError
from weblate.machinery.models import MACHINERY
from weblate.trans.models import Change, Component, Suggestion, Unit
from weblate.trans.util import split_plural
from weblate.utils.state import STATE_APPROVED, STATE_FUZZY, STATE_TRANSLATED
from weblate.utils.state import (
STATE_APPROVED,
STATE_FUZZY,
STATE_TRANSLATED,
StringState,
)


class AutoTranslate:
Expand Down Expand Up @@ -56,7 +61,9 @@ def set_progress(self, current) -> None:
},
)

def update(self, unit, state: int, target: list[str], user=None) -> None:
def update(
self, unit: Unit, state: StringState, target: list[str], user=None
) -> None:
if isinstance(target, str):
target = [target]
if self.mode == "suggest" or any(
Expand All @@ -70,7 +77,11 @@ def update(self, unit, state: int, target: list[str], user=None) -> None:
else:
unit.is_batch_update = True
unit.translate(
user or self.user, target, state, Change.ACTION_AUTO, propagate=False
user or self.user,
target,
state,
change_action=Change.ACTION_AUTO,
propagate=False,
)
self.updated += 1

Expand Down Expand Up @@ -163,18 +174,18 @@ def process_others(self, source: int | None) -> None:

self.post_process()

def fetch_mt(self, engines, threshold):
def fetch_mt(self, engines_list: list[str], threshold: int):
"""Get the translations."""
units = self.get_units()
num_units = len(units)

machinery_settings = self.translation.component.project.get_machinery_settings()

engines = sorted(
engines: list[BatchMachineTranslation] = sorted(
(
MACHINERY[engine](setting)
for engine, setting in machinery_settings.items()
if engine in MACHINERY and engine in engines
if engine in MACHINERY and engine in engines_list
),
key=lambda engine: engine.get_rank(),
reverse=True,
Expand Down
15 changes: 8 additions & 7 deletions weblate/trans/models/unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
from collections.abc import Generator, Iterable
from datetime import datetime

from weblate.auth.models import User
from weblate.auth.models import AuthenticatedHttpRequest, User
from weblate.machinery.base import UnitMemoryResultDict

SIMPLE_FILTERS: dict[str, dict[str, Any]] = {
Expand Down Expand Up @@ -1531,13 +1531,14 @@ def variants(self) -> Iterable[Unit]:
@transaction.atomic
def translate(
self,
user,
new_target,
new_state,
change_action=None,
user: User | None,
new_target: str | list[str],
new_state: StringState,
*,
change_action: int | None = None,
propagate: bool = True,
author=None,
request=None,
author: User | None = None,
request: AuthenticatedHttpRequest | None = None,
add_alternative: bool = False,
) -> bool:
"""
Expand Down

0 comments on commit 59e9dcc

Please sign in to comment.