fix all typing error in tests

This commit is contained in:
Jörg Thalheim
2024-08-21 18:58:16 +02:00
parent a482829fef
commit f3a2b4d80e
4 changed files with 12 additions and 11 deletions

View File

@@ -86,7 +86,6 @@ def dataclass_to_dict(obj: Any, *, use_alias: bool = True) -> Any:
T = TypeVar("T", bound=dataclass) # type: ignore
G = TypeVar("G") # type: ignore
def is_union_type(type_hint: type | UnionType) -> bool:
@@ -121,7 +120,9 @@ def unwrap_none_type(type_hint: type | UnionType) -> type:
JsonValue = str | float | dict[str, Any] | list[Any] | None
def construct_value(t: type, field_value: JsonValue, loc: list[str] = []) -> Any:
def construct_value(
t: type | UnionType, field_value: JsonValue, loc: list[str] = []
) -> Any:
"""
Construct a field value from a type hint and a field value.
"""
@@ -135,6 +136,7 @@ def construct_value(t: type, field_value: JsonValue, loc: list[str] = []) -> Any
# If the field is another dataclass
# Field_value must be a dictionary
if is_dataclass(t) and isinstance(field_value, dict):
assert isinstance(t, type)
return construct_dataclass(t, field_value)
# If the field expects a path
@@ -250,7 +252,9 @@ def construct_dataclass(t: type[T], data: dict[str, Any], path: list[str] = [])
return t(**field_values) # type: ignore
def from_dict(t: type[G], data: dict[str, Any] | Any, path: list[str] = []) -> G:
def from_dict(
t: type | UnionType, data: dict[str, Any] | Any, path: list[str] = []
) -> Any:
if is_dataclass(t):
if not isinstance(data, dict):
raise ClanError(f"{data} is not a dict. Expected {t}")

View File

@@ -15,7 +15,7 @@ class CaptureOutput:
self.capsys.readouterr()
return self
def __exit__(self, exc_type: Any, exc_value: Any, exc_traceback: Any) -> bool:
def __exit__(self, exc_type: Any, exc_value: Any, exc_traceback: Any) -> None:
res = self.capsys.readouterr()
self.out = res.out
self.err = res.err
@@ -23,7 +23,6 @@ class CaptureOutput:
# Disable capsys again
self.capsys_disabled = self.capsys.disabled()
self.capsys_disabled.__enter__()
return False
@pytest.fixture

View File

@@ -255,13 +255,13 @@ def test_none_or_string() -> None:
class Person:
name: Path
checked = from_dict(str | None, data)
checked: str | None = from_dict(str | None, data)
assert checked is None
checked2 = from_dict(dict[str, str] | None, data)
checked2: dict[str, str] | None = from_dict(dict[str, str] | None, data)
assert checked2 is None
checked3 = from_dict(Person | None, data)
checked3: Person | None = from_dict(Person | None, data)
assert checked3 is None

View File

@@ -163,9 +163,7 @@ def test_generate_secret_var_password_store(
temporary_home: Path,
) -> None:
config = nested_dict()
my_generator = config["clan"]["core"]["vars"]["settings"]["secretStore"] = (
"password-store"
)
config["clan"]["core"]["vars"]["settings"]["secretStore"] = "password-store"
my_generator = config["clan"]["core"]["vars"]["generators"]["my_generator"]
my_generator["files"]["my_secret"]["secret"] = True
my_generator["script"] = "echo hello > $out/my_secret"