Skip to content

Commit

Permalink
chore(unit): add type annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
nijel committed Oct 1, 2024
1 parent 7ca2a89 commit ce75a07
Showing 1 changed file with 27 additions and 24 deletions.
51 changes: 27 additions & 24 deletions weblate/trans/models/unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@

if TYPE_CHECKING:
from collections.abc import Generator, Iterable
from datetime import datetime

from weblate.auth.models import User
from weblate.machinery.base import UnitMemoryResultDict
Expand Down Expand Up @@ -564,33 +565,33 @@ def store_old_unit(self, unit) -> None:
}

@property
def approved(self):
def approved(self) -> bool:
return self.state == STATE_APPROVED

@property
def translated(self):
def translated(self) -> bool:
return self.state >= STATE_TRANSLATED

@property
def readonly(self):
def readonly(self) -> bool:
return self.state == STATE_READONLY

@property
def fuzzy(self):
def fuzzy(self) -> bool:
return self.state == STATE_FUZZY

@property
def has_failing_check(self):
def has_failing_check(self) -> bool:
return bool(self.active_checks)

@property
def has_comment(self):
def has_comment(self) -> bool:
# Use bool here as unresolved_comments might be list
# or a queryset (from prefetch)
return bool(self.unresolved_comments)

@property
def has_suggestion(self):
def has_suggestion(self) -> bool:
return bool(self.suggestions)

def source_unit_save(self) -> None:
Expand Down Expand Up @@ -1067,7 +1068,7 @@ def propagate(self, user: User, change_action=None, author=None, request=None):
result = True
return result

def commit_if_pending(self, author) -> None:
def commit_if_pending(self, author: User) -> None:
"""Commit possible previous changes on this unit."""
if self.pending:
change_author = self.get_last_content_change()[0]
Expand Down Expand Up @@ -1168,7 +1169,9 @@ def save_backend(

return True

def update_source_units(self, previous_source, user: User, author) -> None:
def update_source_units(
self, previous_source: str, user: User, author: User | None
) -> None:
"""
Update source for units within same component.
Expand Down Expand Up @@ -1229,7 +1232,7 @@ def generate_change(
save: bool = True,
old: str | None = None,
target: str | None = None,
):
) -> Change:
"""Create Change entry for saving unit."""
# Notify about new contributor
if (
Expand Down Expand Up @@ -1279,12 +1282,12 @@ def generate_change(
return change

@cached_property
def suggestions(self):
def suggestions(self) -> models.QuerySet[Suggestion]:
"""Return all suggestions for this unit."""
return self.suggestion_set.order()

@cached_property
def all_checks(self):
def all_checks(self) -> models.QuerySet[Check]:
result = self.check_set.all()
# Force fetching
list(result)
Expand All @@ -1295,20 +1298,20 @@ def clear_checks_cache(self) -> None:
del self.__dict__["all_checks"]

@property
def all_checks_names(self):
def all_checks_names(self) -> set[str]:
return {check.name for check in self.all_checks}

@property
def dismissed_checks(self):
def dismissed_checks(self) -> list[Check]:
return [check for check in self.all_checks if check.dismissed]

@property
def active_checks(self):
def active_checks(self) -> list[Check]:
"""Return all active (not ignored) checks for this unit."""
return [check for check in self.all_checks if not check.dismissed]

@cached_property
def all_comments(self):
def all_comments(self) -> models.QuerySet[Comment]:
"""Return list of target comments."""
if self.is_source:
# Add all comments on translation on source string comment
Expand All @@ -1319,7 +1322,7 @@ def all_comments(self):
return Comment.objects.filter(query).prefetch_related("unit", "user").order()

@cached_property
def unresolved_comments(self):
def unresolved_comments(self) -> list[Comment]:
return [
comment
for comment in self.all_comments
Expand Down Expand Up @@ -1423,7 +1426,7 @@ def run_checks(self, propagate: bool | None = None) -> None: # noqa: C901
if not self.is_batch_update and (create or old_checks):
self.translation.invalidate_cache()

def nearby(self, count: int):
def nearby(self, count: int) -> models.QuerySet[Unit]:
"""Return list of nearby messages based on location."""
if self.position == 0:
return Unit.objects.none()
Expand All @@ -1442,7 +1445,7 @@ def nearby(self, count: int):
list(result)
return result

def nearby_keys(self, count):
def nearby_keys(self, count: int) -> Iterable[Unit]:
# Do not show nearby keys on bilingual
if not self.translation.component.has_template():
return []
Expand All @@ -1462,7 +1465,7 @@ def nearby_keys(self, count):
list(result)
return result

def variants(self):
def variants(self) -> Iterable[Unit]:
if not self.variant:
return []
return (
Expand All @@ -1482,7 +1485,7 @@ def translate(
author=None,
request=None,
add_alternative: bool = False,
):
) -> bool:
"""
Store new translation of a unit.
Expand Down Expand Up @@ -1670,11 +1673,11 @@ def get_max_length(self):

return fallback

def get_target_hash(self):
def get_target_hash(self) -> int:
return calculate_hash(self.target)

@cached_property
def content_hash(self):
def content_hash(self) -> int:
return calculate_hash(self.source, self.context)

@cached_property
Expand All @@ -1686,7 +1689,7 @@ def recent_content_changes(self):
"""
return self.change_set.content().select_related("author").order_by("-timestamp")

def get_last_content_change(self, silent=False):
def get_last_content_change(self, silent: bool = False) -> tuple[User, datetime]:
"""
Get last content change metadata.
Expand Down

0 comments on commit ce75a07

Please sign in to comment.