Merge pull request 'fix type checking in tests' (#2012) from type-checking into main

This commit is contained in:
clan-bot
2024-09-02 10:31:27 +00:00
6 changed files with 16 additions and 21 deletions

6
flake.lock generated
View File

@@ -131,11 +131,11 @@
]
},
"locked": {
"lastModified": 1724833132,
"narHash": "sha256-F4djBvyNRAXGusJiNYInqR6zIMI3rvlp6WiKwsRISos=",
"lastModified": 1725271338,
"narHash": "sha256-FuP8Im+wj4xxaUp1bffhgvJvXtUT6TapkMfRcjaIAK4=",
"owner": "numtide",
"repo": "treefmt-nix",
"rev": "3ffd842a5f50f435d3e603312eefa4790db46af5",
"rev": "23c2b0d953710939487ec878d70495d73b2c9bb3",
"type": "github"
},
"original": {

View File

@@ -46,15 +46,11 @@
};
treefmt.programs.mypy.directories =
{
"pkgs/clan-cli" = {
extraPythonPackages = self'.packages.clan-cli.testDependencies;
modules = [ "clan_cli" ];
};
"pkgs/clan-cli".extraPythonPackages = self'.packages.clan-cli.testDependencies;
"pkgs/clan-app" = {
extraPythonPackages =
(self'.packages.clan-app.externalTestDeps or [ ]) ++ self'.packages.clan-cli.testDependencies;
extraPythonPaths = [ "../clan-cli" ];
modules = [ "clan_app" ];
};
}
// (
@@ -64,7 +60,6 @@
extraPythonPackages =
self'.packages.clan-vm-manager.externalTestDeps ++ self'.packages.clan-cli.testDependencies;
extraPythonPaths = [ "../clan-cli" ];
modules = [ "clan_vm_manager" ];
};
}
else
@@ -72,6 +67,5 @@
);
treefmt.programs.ruff.check = true;
treefmt.programs.ruff.format = true;
};
}

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"