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(unit): apply autofixes after adjusting plurals #13320

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Not yet released.
**Bug fixes**

* Avoid query parser crash in multi-threaded environments.
* Avoid :ref:`autofix` crash on multi-value strings.

**Compatibility**

Expand Down
12 changes: 8 additions & 4 deletions weblate/trans/autofixes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,19 @@ def fix_target(self, target: str, unit: Unit) -> tuple[list[str], bool]:
for number, examples in source_plural.examples.items()
}
target_examples = target_plural.examples
plurals_map = [
source_examples.get(tuple(target_examples.get(target_index, [])), -1)
plurals_map: dict[int, int] = {
target_index: source_examples.get(
tuple(target_examples.get(target_index, [])), -1
)
for target_index in range(target_plural.number)
]
}
# Ensure we have a source strings for each map
while len(source_strings) <= max(plurals_map):
source_strings.append(source_strings[0])
results = [
self.fix_single_target(text, source_strings[plurals_map[i]], unit)
self.fix_single_target(
text, source_strings[plurals_map.get(i, -1)], unit
)
for i, text in enumerate(target)
]
return [r[0] for r in results], max(r[1] for r in results)
8 changes: 4 additions & 4 deletions weblate/trans/models/unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@
"""Return list of units ordered by ID."""
return sorted(self.filter(id__in=ids), key=lambda unit: ids.index(unit.id))

def select_for_update(self):

Check failure on line 331 in weblate/trans/models/unit.py

View workflow job for this annotation

GitHub Actions / mypy

Signature of "select_for_update" incompatible with supertype "QuerySet"
return super().select_for_update(no_key=using_postgresql())

def annotate_stats(self):
Expand Down Expand Up @@ -459,7 +459,7 @@
name = source
return f"{self.pk}: {name}"

def save(

Check failure on line 462 in weblate/trans/models/unit.py

View workflow job for this annotation

GitHub Actions / mypy

Signature of "save" incompatible with supertype "Model"
self,
*,
same_content: bool = False,
Expand Down Expand Up @@ -1213,7 +1213,7 @@

# Update user stats
if save:
change.author.profile.increase_count("translated")

Check failure on line 1216 in weblate/trans/models/unit.py

View workflow job for this annotation

GitHub Actions / mypy

Item "None" of "User | None" has no attribute "profile"
return change

def update_source_units(
Expand Down Expand Up @@ -1401,7 +1401,7 @@
else:
checks = {} if self.readonly else CHECKS.target
meth = "check_target"
args = src, tgt, self

Check failure on line 1404 in weblate/trans/models/unit.py

View workflow job for this annotation

GitHub Actions / mypy

Incompatible types in assignment (expression has type "tuple[Any, Any, Unit]", variable has type "tuple[Any, Unit]")
if self.translation.component.is_glossary:
checks = CHECKS.glossary
meth = "check_source"
Expand Down Expand Up @@ -1564,10 +1564,6 @@
if isinstance(new_target, str):
new_target = [new_target]

# Apply autofixes
if not self.translation.is_template:
new_target, self.fixups = fix_target(new_target, self)

# Handle managing alternative translations
if add_alternative:
new_target.append("")
Expand All @@ -1579,6 +1575,10 @@
if not component.is_multivalue:
new_target = self.adjust_plurals(new_target)

# Apply autofixes
if not self.translation.is_template:
new_target, self.fixups = fix_target(new_target, self)

# Update unit and save it
self.target = join_plural(new_target)
not_empty = any(new_target)
Expand Down
Loading