diff --git a/tests/__init__.py b/tests/__init__.py index 2ff0de60bf..a49fb35c9e 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -13,7 +13,7 @@ LANGUAGES = [p.name for p in SENTENCES_DIR.iterdir() if p.is_dir()] -def load_sentences(language: str): +def load_sentences(language: str) -> dict[str, Any]: """Load sentences from sentences/ for a language""" lang_dir = SENTENCES_DIR / language files: Dict[str, Any] = {} @@ -30,6 +30,6 @@ def load_sentences(language: str): return files -def load_test(language: str, test_name: str): +def load_test(language: str, test_name: str) -> dict[str, Any]: """Load test sentences from tests/ for a language""" return yaml.safe_load((TESTS_DIR / language / f"{test_name}.yaml").read_text()) diff --git a/tests/conftest.py b/tests/conftest.py index a03777902d..fa40253f21 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -2,6 +2,7 @@ from __future__ import annotations import dataclasses +from typing import Any import pytest import yaml @@ -27,27 +28,33 @@ def pytest_generate_tests(metafunc): @pytest.fixture(scope="session") -def intent_schemas(): +def intent_schemas() -> dict[str, Any]: """Loads the base intents file""" with open(INTENTS_FILE, "r", encoding="utf-8") as schema_file: return yaml.safe_load(schema_file) @pytest.fixture(name="language_sentences_yaml", scope="session") -def language_sentences_yaml_fixture(language: str): +def language_sentences_yaml_fixture(language: str) -> dict[str, Any]: """Loads the language sentences.""" return load_sentences(language) @pytest.fixture(name="language_sentences_common", scope="session") -def language_sentences_common_fixture(language, language_sentences_yaml): +def language_sentences_common_fixture( + language: str, + language_sentences_yaml: dict[str, Any], +) -> Intents: """Loads the common language intents.""" language_sentences_yaml["_common.yaml"].setdefault("intents", {}) return Intents.from_dict(language_sentences_yaml["_common.yaml"]) @pytest.fixture(scope="session") -def language_sentences(language_sentences_yaml: dict, language_sentences_common): +def language_sentences( + language_sentences_yaml: dict[str, Any], + language_sentences_common: Intents, +) -> Intents: """Parse language sentences.""" merged: dict = {} for file_name, intents_dict in language_sentences_yaml.items(): diff --git a/tests/test_language_intents.py b/tests/test_language_intents.py index 8f4c8c1a23..dafc8fab0e 100644 --- a/tests/test_language_intents.py +++ b/tests/test_language_intents.py @@ -1,7 +1,10 @@ """Test language intents.""" +from __future__ import annotations + import dataclasses import sys -from typing import Any, Dict, Iterable, Set +from pathlib import Path +from typing import Any, Iterable from hassil import Intents from hassil.expression import Expression, ListReference, RuleReference, Sequence @@ -11,9 +14,9 @@ def test_language_common( - language, - language_sentences_yaml: Dict[str, Any], -): + language: str, + language_sentences_yaml: dict[str, Any], +) -> None: """Test the language common file.""" common_files = [key for key in language_sentences_yaml if key.startswith("_")] @@ -39,10 +42,10 @@ def test_language_common( def do_test_language_sentences( file_name: str, - intent_schemas: Dict[str, Any], - language_sentences_yaml: Dict[str, Any], + intent_schemas: dict[str, Any], + language_sentences_yaml: dict[str, Any], language_sentences_common: Intents, -): +) -> None: """Ensure all language sentences contain valid slots, lists, rules, etc.""" parsed_sentences_without_common = Intents.from_dict( language_sentences_yaml[file_name] @@ -65,7 +68,7 @@ def do_test_language_sentences( for data in intent.data: for sentence in data.sentences: - found_slots: Set[str] = set() + found_slots: set[str] = set() for expression in _flatten(sentence): _verify( expression, @@ -100,10 +103,10 @@ def _verify( expression: Expression, intents: Intents, intent_name: str, - slot_schema: Dict[str, Any], - visited_rules: Set[str], - found_slots: Set[str], -): + slot_schema: dict[str, Any], + visited_rules: set[str], + found_slots: set[str], +) -> None: if isinstance(expression, ListReference): list_ref: ListReference = expression @@ -153,8 +156,12 @@ def _flatten(expression: Expression) -> Iterable[Expression]: yield expression -def gen_test(test_file): - def test_func(intent_schemas, language_sentences_yaml, language_sentences_common): +def gen_test(test_file: Path) -> None: + def test_func( + intent_schemas: dict[str, Any], + language_sentences_yaml: dict[str, Any], + language_sentences_common: Intents, + ) -> None: do_test_language_sentences( test_file.name, intent_schemas, @@ -166,7 +173,7 @@ def test_func(intent_schemas, language_sentences_yaml, language_sentences_common setattr(sys.modules[__name__], test_func.__name__, test_func) -def gen_tests(): +def gen_tests() -> None: lang_dir = SENTENCES_DIR / "en" for test_file in lang_dir.glob("*.yaml"): diff --git a/tests/test_language_sentences.py b/tests/test_language_sentences.py index e8033ba954..4cdd13fa53 100644 --- a/tests/test_language_sentences.py +++ b/tests/test_language_sentences.py @@ -1,15 +1,18 @@ """Test language sentences.""" +from __future__ import annotations + import sys +from pathlib import Path import pytest -from hassil import recognize -from hassil.intents import TextSlotList +from hassil import Intents, recognize +from hassil.intents import SlotList, TextSlotList from . import TESTS_DIR, load_test @pytest.fixture(name="slot_lists", scope="session") -def slot_lists_fixture(language): +def slot_lists_fixture(language: str) -> dict[str, SlotList]: """Loads the slot lists for the language.""" fixtures = load_test(language, "_fixtures") return { @@ -23,8 +26,11 @@ def slot_lists_fixture(language): def do_test_language_sentences_file( - language, test_file, slot_lists, language_sentences -): + language: str, + test_file: str, + slot_lists: dict[str, SlotList], + language_sentences: Intents, +) -> None: """Tests recognition all of the test sentences for a language""" _testing_domain, testing_intent = test_file.split("_", 1) @@ -56,8 +62,12 @@ def do_test_language_sentences_file( assert result.entities[slot_name].value == slot_dict["value"] -def gen_test(test_file): - def test_func(language, slot_lists, language_sentences): +def gen_test(test_file: Path) -> None: + def test_func( + language: str, + slot_lists: dict[str, SlotList], + language_sentences: Intents, + ) -> None: do_test_language_sentences_file( language, test_file.stem, slot_lists, language_sentences ) @@ -66,7 +76,7 @@ def test_func(language, slot_lists, language_sentences): setattr(sys.modules[__name__], test_func.__name__, test_func) -def gen_tests(): +def gen_tests() -> None: lang_dir = TESTS_DIR / "en" for test_file in lang_dir.glob("*.yaml"):