Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(component): avoid crash on getting unused enforcements #12711

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion weblate/trans/models/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,12 @@
from weblate.vcs.ssh import add_host_key

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

from weblate.addons.models import Addon
from weblate.auth.models import AuthenticatedHttpRequest, User
from weblate.checks.base import BaseCheck
from weblate.trans.models import Unit

NEW_LANG_CHOICES = (
Expand Down Expand Up @@ -1104,7 +1106,7 @@
# Calculate progress for translations
if progress is None:
self.translations_progress += 1
progress = 100 * self.translations_progress // self.translations_count

Check failure on line 1109 in weblate/trans/models/component.py

View workflow job for this annotation

GitHub Actions / mypy

Unsupported operand types for // ("int" and "None")
# Store task state
current_task.update_state(
state="PROGRESS", meta={"progress": progress, "component": self.pk}
Expand Down Expand Up @@ -1753,7 +1755,7 @@
from weblate.trans.tasks import perform_push

self.log_info("scheduling push")
perform_push.delay_on_commit(

Check failure on line 1758 in weblate/trans/models/component.py

View workflow job for this annotation

GitHub Actions / mypy

"Task[[Any, VarArg(Any), KwArg(Any)], None]" has no attribute "delay_on_commit"
self.pk, None, force_commit=False, do_update=do_update
)

Expand Down Expand Up @@ -2340,7 +2342,7 @@
.order_by("-id")[0]
.auto_status
):
self.do_lock(user=None, lock=False, auto=True)

Check failure on line 2345 in weblate/trans/models/component.py

View workflow job for this annotation

GitHub Actions / mypy

Argument "user" to "do_lock" of "Component" has incompatible type "None"; expected "User"

if ALERTS[alert].link_wide:
for component in self.linked_childs:
Expand All @@ -2358,7 +2360,7 @@

# Automatically lock on error
if created and self.auto_lock_error and alert in LOCKING_ALERTS:
self.do_lock(user=None, lock=True, auto=True)

Check failure on line 2363 in weblate/trans/models/component.py

View workflow job for this annotation

GitHub Actions / mypy

Argument "user" to "do_lock" of "Component" has incompatible type "None"; expected "User"

# Update details with exception of component removal
if not created and not noupdate:
Expand Down Expand Up @@ -2563,7 +2565,7 @@
)
self.handle_parse_error(error.__cause__, filename=self.template)
self.update_import_alerts()
raise error.__cause__ from error # pylint: disable=E0710

Check failure on line 2568 in weblate/trans/models/component.py

View workflow job for this annotation

GitHub Actions / mypy

Exception must be derived from BaseException
was_change |= bool(translation.reason)
translations[translation.id] = translation
languages[lang.code] = translation
Expand Down Expand Up @@ -2627,7 +2629,7 @@
if self.needs_cleanup and not self.template:
from weblate.trans.tasks import cleanup_component

cleanup_component.delay_on_commit(self.id)

Check failure on line 2632 in weblate/trans/models/component.py

View workflow job for this annotation

GitHub Actions / mypy

"Task[[Any], None]" has no attribute "delay_on_commit"

if was_change:
if self.needs_variants_update:
Expand Down Expand Up @@ -2659,7 +2661,7 @@
if settings.CELERY_TASK_ALWAYS_EAGER:
batch_update_checks(self.id, batched_checks, component=self)
else:
batch_update_checks.delay_on_commit(self.id, batched_checks)

Check failure on line 2664 in weblate/trans/models/component.py

View workflow job for this annotation

GitHub Actions / mypy

"Task[[Any, Any, Component | None], None]" has no attribute "delay_on_commit"
self.batch_checks = False
self.batched_checks = set()

Expand Down Expand Up @@ -2810,7 +2812,7 @@
raise ValidationError(
{"filemask": gettext("The file mask did not match any files.")}
)
langs = {}

Check failure on line 2815 in weblate/trans/models/component.py

View workflow job for this annotation

GitHub Actions / mypy

Need type annotation for "langs" (hint: "langs: dict[<type>, <type>] = ...")
existing_langs = set()

for match in matches:
Expand Down Expand Up @@ -2878,7 +2880,7 @@
if (not self.new_base and self.new_lang != "add") or not self.file_format:
return
# File is valid or no file is needed
errors = []

Check failure on line 2883 in weblate/trans/models/component.py

View workflow job for this annotation

GitHub Actions / mypy

Need type annotation for "errors" (hint: "errors: list[<type>] = ...")
if self.is_valid_base_for_new(errors):
return
# File is needed, but not present
Expand Down Expand Up @@ -3249,7 +3251,7 @@
if self.variant_regex:
variant_re = re.compile(self.variant_regex)
units = process_units.filter(context__regex=self.variant_regex)
variant_updates = {}

Check failure on line 3254 in weblate/trans/models/component.py

View workflow job for this annotation

GitHub Actions / mypy

Need type annotation for "variant_updates" (hint: "variant_updates: dict[<type>, <type>] = ...")
for unit in units.iterator():
if variant_re.findall(unit.context):
key = variant_re.sub("", unit.context)
Expand Down Expand Up @@ -3747,14 +3749,15 @@
for glossary in self.project.glossaries:
sync_glossary_languages.delay(glossary.pk)

def get_unused_enforcements(self):
def get_unused_enforcements(self) -> Iterable[dict | BaseCheck]:
from weblate.trans.models import Unit

for current in self.enforced_checks:
try:
check = CHECKS[current]
except KeyError:
yield {"name": current, "notsupported": True}
continue

Check warning on line 3760 in weblate/trans/models/component.py

View check run for this annotation

Codecov / codecov/patch

weblate/trans/models/component.py#L3760

Added line #L3760 was not covered by tests
# Check is always enabled
if not check.default_disabled:
continue
Expand Down
Loading