Merge pull request 'fix type checking in tests' (#2012) from type-checking into main
This commit is contained in:
6
flake.lock
generated
6
flake.lock
generated
@@ -131,11 +131,11 @@
|
|||||||
]
|
]
|
||||||
},
|
},
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1724833132,
|
"lastModified": 1725271338,
|
||||||
"narHash": "sha256-F4djBvyNRAXGusJiNYInqR6zIMI3rvlp6WiKwsRISos=",
|
"narHash": "sha256-FuP8Im+wj4xxaUp1bffhgvJvXtUT6TapkMfRcjaIAK4=",
|
||||||
"owner": "numtide",
|
"owner": "numtide",
|
||||||
"repo": "treefmt-nix",
|
"repo": "treefmt-nix",
|
||||||
"rev": "3ffd842a5f50f435d3e603312eefa4790db46af5",
|
"rev": "23c2b0d953710939487ec878d70495d73b2c9bb3",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
|
|||||||
@@ -46,15 +46,11 @@
|
|||||||
};
|
};
|
||||||
treefmt.programs.mypy.directories =
|
treefmt.programs.mypy.directories =
|
||||||
{
|
{
|
||||||
"pkgs/clan-cli" = {
|
"pkgs/clan-cli".extraPythonPackages = self'.packages.clan-cli.testDependencies;
|
||||||
extraPythonPackages = self'.packages.clan-cli.testDependencies;
|
|
||||||
modules = [ "clan_cli" ];
|
|
||||||
};
|
|
||||||
"pkgs/clan-app" = {
|
"pkgs/clan-app" = {
|
||||||
extraPythonPackages =
|
extraPythonPackages =
|
||||||
(self'.packages.clan-app.externalTestDeps or [ ]) ++ self'.packages.clan-cli.testDependencies;
|
(self'.packages.clan-app.externalTestDeps or [ ]) ++ self'.packages.clan-cli.testDependencies;
|
||||||
extraPythonPaths = [ "../clan-cli" ];
|
extraPythonPaths = [ "../clan-cli" ];
|
||||||
modules = [ "clan_app" ];
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
// (
|
// (
|
||||||
@@ -64,7 +60,6 @@
|
|||||||
extraPythonPackages =
|
extraPythonPackages =
|
||||||
self'.packages.clan-vm-manager.externalTestDeps ++ self'.packages.clan-cli.testDependencies;
|
self'.packages.clan-vm-manager.externalTestDeps ++ self'.packages.clan-cli.testDependencies;
|
||||||
extraPythonPaths = [ "../clan-cli" ];
|
extraPythonPaths = [ "../clan-cli" ];
|
||||||
modules = [ "clan_vm_manager" ];
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -72,6 +67,5 @@
|
|||||||
);
|
);
|
||||||
treefmt.programs.ruff.check = true;
|
treefmt.programs.ruff.check = true;
|
||||||
treefmt.programs.ruff.format = true;
|
treefmt.programs.ruff.format = true;
|
||||||
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -86,7 +86,6 @@ def dataclass_to_dict(obj: Any, *, use_alias: bool = True) -> Any:
|
|||||||
|
|
||||||
|
|
||||||
T = TypeVar("T", bound=dataclass) # type: ignore
|
T = TypeVar("T", bound=dataclass) # type: ignore
|
||||||
G = TypeVar("G") # type: ignore
|
|
||||||
|
|
||||||
|
|
||||||
def is_union_type(type_hint: type | UnionType) -> bool:
|
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
|
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.
|
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
|
# If the field is another dataclass
|
||||||
# Field_value must be a dictionary
|
# Field_value must be a dictionary
|
||||||
if is_dataclass(t) and isinstance(field_value, dict):
|
if is_dataclass(t) and isinstance(field_value, dict):
|
||||||
|
assert isinstance(t, type)
|
||||||
return construct_dataclass(t, field_value)
|
return construct_dataclass(t, field_value)
|
||||||
|
|
||||||
# If the field expects a path
|
# 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
|
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 is_dataclass(t):
|
||||||
if not isinstance(data, dict):
|
if not isinstance(data, dict):
|
||||||
raise ClanError(f"{data} is not a dict. Expected {t}")
|
raise ClanError(f"{data} is not a dict. Expected {t}")
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ class CaptureOutput:
|
|||||||
self.capsys.readouterr()
|
self.capsys.readouterr()
|
||||||
return self
|
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()
|
res = self.capsys.readouterr()
|
||||||
self.out = res.out
|
self.out = res.out
|
||||||
self.err = res.err
|
self.err = res.err
|
||||||
@@ -23,7 +23,6 @@ class CaptureOutput:
|
|||||||
# Disable capsys again
|
# Disable capsys again
|
||||||
self.capsys_disabled = self.capsys.disabled()
|
self.capsys_disabled = self.capsys.disabled()
|
||||||
self.capsys_disabled.__enter__()
|
self.capsys_disabled.__enter__()
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
|
|||||||
@@ -255,13 +255,13 @@ def test_none_or_string() -> None:
|
|||||||
class Person:
|
class Person:
|
||||||
name: Path
|
name: Path
|
||||||
|
|
||||||
checked = from_dict(str | None, data)
|
checked: str | None = from_dict(str | None, data)
|
||||||
assert checked is None
|
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
|
assert checked2 is None
|
||||||
|
|
||||||
checked3 = from_dict(Person | None, data)
|
checked3: Person | None = from_dict(Person | None, data)
|
||||||
assert checked3 is None
|
assert checked3 is None
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -163,9 +163,7 @@ def test_generate_secret_var_password_store(
|
|||||||
temporary_home: Path,
|
temporary_home: Path,
|
||||||
) -> None:
|
) -> None:
|
||||||
config = nested_dict()
|
config = nested_dict()
|
||||||
my_generator = config["clan"]["core"]["vars"]["settings"]["secretStore"] = (
|
config["clan"]["core"]["vars"]["settings"]["secretStore"] = "password-store"
|
||||||
"password-store"
|
|
||||||
)
|
|
||||||
my_generator = config["clan"]["core"]["vars"]["generators"]["my_generator"]
|
my_generator = config["clan"]["core"]["vars"]["generators"]["my_generator"]
|
||||||
my_generator["files"]["my_secret"]["secret"] = True
|
my_generator["files"]["my_secret"]["secret"] = True
|
||||||
my_generator["script"] = "echo hello > $out/my_secret"
|
my_generator["script"] = "echo hello > $out/my_secret"
|
||||||
|
|||||||
Reference in New Issue
Block a user