Coverage for app/venv/lib/python3.14/site-packages/weblate/trans/autofixes/base.py: 39%

33 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-10-07 07:15 +0000

1# Copyright © Michal Čihař <michal@weblate.org> 

2# 

3# SPDX-License-Identifier: GPL-3.0-or-later 

4 

5from __future__ import annotations 

6 

7from typing import TYPE_CHECKING 

8 

9from weblate.utils.classloader import ClassLoaderProtocol 

10 

11if TYPE_CHECKING: 11 ↛ 12line 11 didn't jump to line 12 because the condition on line 11 was never true

12 from django_stubs_ext import StrOrPromise 

13 

14 from weblate.trans.models import Unit 

15 

16 

17class AutoFix(ClassLoaderProtocol): 

18 """Base class for AutoFixes.""" 

19 

20 fix_id = "auto" 

21 

22 @property 

23 def name(self) -> StrOrPromise: 

24 return self.fix_id 

25 

26 def get_identifier(self): 

27 return self.fix_id 

28 

29 @staticmethod 

30 def get_related_checks(): 

31 return [] 

32 

33 def fix_single_target( 

34 self, target: str, source: str, unit: Unit 

35 ) -> tuple[str, bool]: 

36 """Fix a single target, implement this method in subclasses.""" 

37 raise NotImplementedError 

38 

39 def fix_target(self, target: list[str], unit: Unit) -> tuple[list[str], bool]: 

40 """Return a target translation array with a single fix applied.""" 

41 source_strings = unit.get_source_plurals() 

42 if unit.translation.component.is_multivalue: 

43 results = [ 

44 self.fix_single_target(text, source_strings[0], unit) for text in target 

45 ] 

46 elif len(source_strings) == 1 and len(target) == 1: 

47 results = [self.fix_single_target(target[0], source_strings[0], unit)] 

48 else: 

49 source_plural = unit.translation.component.source_language.plural 

50 target_plural = unit.translation.plural 

51 source_examples = { 

52 tuple(examples): number 

53 for number, examples in source_plural.examples.items() 

54 } 

55 target_examples = target_plural.examples 

56 plurals_map: dict[int, int] = { 

57 target_index: source_examples.get( 

58 tuple(target_examples.get(target_index, [])), -1 

59 ) 

60 for target_index in range(target_plural.number) 

61 } 

62 # Ensure we have a source strings for each map 

63 while len(source_strings) <= max(plurals_map): 

64 source_strings.append(source_strings[0]) 

65 results = [ 

66 self.fix_single_target( 

67 text, source_strings[plurals_map.get(i, -1)], unit 

68 ) 

69 for i, text in enumerate(target) 

70 ] 

71 return [r[0] for r in results], max(r[1] for r in results)