Skip to content

Commit 00c7ab7

Browse files
Update type stubs and fix test type annotations to satisfy mypy linting
1 parent dc1c180 commit 00c7ab7

File tree

2 files changed

+51
-7
lines changed

2 files changed

+51
-7
lines changed

src/rustfluent.pyi

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,55 @@ from pathlib import Path
33

44
Variable = str | int | date
55

6+
class ParseErrorDetail:
7+
"""Parse time: syntax errors in .ftl files (invalid FTL syntax, malformed messages)"""
8+
9+
message: str
10+
line: int
11+
column: int
12+
byte_start: int
13+
byte_end: int
14+
filename: str | None
15+
16+
class ValidationError:
17+
"""Load time: semantic errors during Bundle() init (unknown refs, cycles, duplicate IDs)"""
18+
19+
error_type: (
20+
str # DuplicateMessageId, UnknownMessage, UnknownTerm, UnknownAttribute, CyclicReference
21+
)
22+
message: str
23+
message_id: str | None
24+
reference: str | None
25+
26+
class FormatError:
27+
"""Runtime: errors during get_translation() (missing variables, invalid variable types)"""
28+
29+
error_type: str # MissingVariable, InvalidVariableType
30+
message: str
31+
message_id: str | None
32+
variable_name: str | None
33+
expected_type: str | None
34+
actual_type: str | None
35+
636
class Bundle:
737
def __init__(
8-
self, language: str, ftl_filenames: list[str | Path], strict: bool = False
38+
self,
39+
language: str,
40+
ftl_filenames: list[str | Path],
41+
strict: bool = False,
42+
validate_references: bool = True,
943
) -> None: ...
1044
def get_translation(
1145
self,
1246
identifier: str,
1347
variables: dict[str, Variable] | None = None,
1448
use_isolating: bool = True,
49+
errors: list[FormatError] | None = None,
1550
) -> str: ...
51+
def get_parse_errors(self) -> list[ParseErrorDetail]: ...
52+
def get_validation_errors(self) -> list[ValidationError]: ...
53+
def get_all_compile_errors(
54+
self,
55+
) -> list[tuple[str, ParseErrorDetail | ValidationError]]: ...
56+
def get_compile_errors(self) -> list[ValidationError]: ...
57+
def get_required_variables(self, identifier: str) -> list[str]: ...

tests/test_python_interface.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,7 @@ def test_validate_references_false_with_duplicates():
844844
def test_get_translation_errors_missing_variable():
845845
"""Test that missing variables are reported via errors parameter."""
846846
bundle = fluent.Bundle("en", [data_dir / "variables.ftl"])
847-
errors = []
847+
errors: list[fluent.FormatError] = []
848848

849849
# Call get_translation without providing required variable
850850
result = bundle.get_translation("greeting", errors=errors)
@@ -864,11 +864,13 @@ def test_get_translation_errors_missing_variable():
864864
def test_get_translation_errors_invalid_variable_type():
865865
"""Test that invalid variable types are reported via errors parameter."""
866866
bundle = fluent.Bundle("en", [data_dir / "variables.ftl"])
867-
errors = []
867+
errors: list[fluent.FormatError] = []
868868

869869
# Pass a list instead of a string
870870
result = bundle.get_translation(
871-
"greeting", variables={"name": ["not", "a", "string"]}, errors=errors
871+
"greeting",
872+
variables={"name": ["not", "a", "string"]}, # type: ignore[dict-item]
873+
errors=errors,
872874
)
873875

874876
# Result should use fallback (the variable key itself)
@@ -887,7 +889,7 @@ def test_get_translation_errors_invalid_variable_type():
887889
def test_get_translation_errors_multiple_missing_variables():
888890
"""Test that multiple missing variables are all reported."""
889891
bundle = fluent.Bundle("en", [data_dir / "variables.ftl"])
890-
errors = []
892+
errors: list[fluent.FormatError] = []
891893

892894
# user-info needs both $username and $count
893895
result = bundle.get_translation("user-info", errors=errors)
@@ -906,7 +908,7 @@ def test_get_translation_errors_multiple_missing_variables():
906908
def test_get_translation_errors_partial_variables():
907909
"""Test errors when only some variables are provided."""
908910
bundle = fluent.Bundle("en", [data_dir / "variables.ftl"])
909-
errors = []
911+
errors: list[fluent.FormatError] = []
910912

911913
# Provide only username, not count
912914
result = bundle.get_translation("user-info", variables={"username": "Alice"}, errors=errors)
@@ -932,7 +934,7 @@ def test_get_translation_errors_none_parameter():
932934
def test_get_translation_errors_with_attributes():
933935
"""Test error collection with message attributes."""
934936
bundle = fluent.Bundle("en", [data_dir / "variables.ftl"])
935-
errors = []
937+
errors: list[fluent.FormatError] = []
936938

937939
# email-template.subject needs $recipient
938940
result = bundle.get_translation("email-template.subject", errors=errors)

0 commit comments

Comments
 (0)