|
| 1 | +"""Test file to verify type annotations work correctly for zntrack fields. |
| 2 | +
|
| 3 | +This test should pass type checking with pyright/mypy. |
| 4 | +The main goal is to ensure that patterns like: |
| 5 | + field: int = zntrack.params() |
| 6 | + field: str = zntrack.outs() |
| 7 | +do not raise type errors when no explicit value is provided. |
| 8 | +""" |
| 9 | + |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | +import pandas as pd |
| 13 | + |
| 14 | +import zntrack |
| 15 | + |
| 16 | + |
| 17 | +class TestFieldAnnotations(zntrack.Node): |
| 18 | + """Test basic field type annotations without explicit values.""" |
| 19 | + |
| 20 | + # These should NOT raise type errors (the main fix) |
| 21 | + param_int: int = zntrack.params() |
| 22 | + param_str: str = zntrack.params() |
| 23 | + param_dict: dict = zntrack.params() |
| 24 | + |
| 25 | + out_int: int = zntrack.outs() |
| 26 | + out_str: str = zntrack.outs() |
| 27 | + out_dict: dict = zntrack.outs() |
| 28 | + |
| 29 | + metric_dict: dict = zntrack.metrics() |
| 30 | + |
| 31 | + dep_int: int = zntrack.deps() |
| 32 | + dep_str: str = zntrack.deps() |
| 33 | + |
| 34 | + plots_data: pd.DataFrame = ( |
| 35 | + zntrack.plots() |
| 36 | + ) # pandas.DataFrame, but we don't enforce the type |
| 37 | + |
| 38 | + # Path fields |
| 39 | + params_path_str: str = zntrack.params_path() |
| 40 | + params_path_path: Path = zntrack.params_path() |
| 41 | + outs_path_str: str = zntrack.outs_path() |
| 42 | + outs_path_path: Path = zntrack.outs_path() |
| 43 | + deps_path_str: str = zntrack.deps_path() |
| 44 | + deps_path_path: Path = zntrack.deps_path() |
| 45 | + plots_path_path: Path = zntrack.plots_path() |
| 46 | + metrics_path_path: Path = zntrack.metrics_path() |
| 47 | + |
| 48 | + def run(self): |
| 49 | + pass |
| 50 | + |
| 51 | + |
| 52 | +class TestFieldAnnotationsWithDefaults(zntrack.Node): |
| 53 | + """Test that type safety is maintained when providing explicit values.""" |
| 54 | + |
| 55 | + # These should work fine (correct type matching) |
| 56 | + param_good_int: int = zntrack.params(42) |
| 57 | + param_good_str: str = zntrack.params("hello") |
| 58 | + param_good_tuple: str = zntrack.params(("word",)) |
| 59 | + |
| 60 | + # Using default_factory |
| 61 | + param_good_dict: dict = zntrack.params(default_factory=dict) |
| 62 | + param_factory: list = zntrack.params(default_factory=list) |
| 63 | + |
| 64 | + # Path fields with correct types |
| 65 | + params_path_str_with_val: str = zntrack.params_path("config.yaml") |
| 66 | + params_path_path_with_val: Path = zntrack.params_path(Path("config.yaml")) |
| 67 | + |
| 68 | + # Path fields using zntrack.nwd |
| 69 | + outs_path_in_nwd: Path = zntrack.outs_path(zntrack.nwd / "output.txt") |
| 70 | + metrics_path_in_nwd: Path = zntrack.metrics_path(zntrack.nwd / "metrics.json") |
| 71 | + plots_path_in_nwd: Path = zntrack.plots_path(zntrack.nwd / "plot.png") |
| 72 | + |
| 73 | + # plots_path_list |
| 74 | + plots_path_list: list = zntrack.plots_path( |
| 75 | + default_factory=lambda: ["plot1.png", "plot2.png"] |
| 76 | + ) |
| 77 | + |
| 78 | + # path using tuples |
| 79 | + params_path_tuple: tuple[Path, ...] = zntrack.params_path( |
| 80 | + (zntrack.nwd / "config1.yaml", zntrack.nwd / "config2.yaml") |
| 81 | + ) |
| 82 | + outs_path_tuple: tuple[Path, ...] = zntrack.outs_path( |
| 83 | + (zntrack.nwd / "output1.txt", zntrack.nwd / "output2.txt") |
| 84 | + ) |
| 85 | + |
| 86 | + def run(self): |
| 87 | + pass |
| 88 | + |
| 89 | + |
| 90 | +class TestTypeSafetyErrors(zntrack.Node): |
| 91 | + """These should cause type errors to ensure type safety is maintained.""" |
| 92 | + |
| 93 | + # # Type mismatches with explicit values (should error when uncommented) |
| 94 | + param_bad_int: int = zntrack.params( |
| 95 | + "string" |
| 96 | + ) # Should error: str not assignable to int |
| 97 | + param_bad_str: str = zntrack.params(42) # Should error: int not assignable to str |
| 98 | + param_bad_dict: dict = zntrack.params( |
| 99 | + "not_a_dict" |
| 100 | + ) # Should error: str not assignable to dict |
| 101 | + |
| 102 | + # # # Path field type mismatches (should error when uncommented) |
| 103 | + params_path_bad: Path = zntrack.params_path( |
| 104 | + 1234 |
| 105 | + ) # Should error: int not assignable to path types |
| 106 | + outs_path_bad: str = zntrack.outs_path( |
| 107 | + 42 |
| 108 | + ) # Should error: int not assignable to path types |
| 109 | + deps_path_bad: Path = zntrack.deps_path( |
| 110 | + 3.14 |
| 111 | + ) # Should error: float not assignable to path types |
| 112 | + |
| 113 | + # List vs single type mismatches (should error when uncommented) |
| 114 | + outs_path_list_mismatch: str = zntrack.outs_path( |
| 115 | + ("file1.txt", "file2.txt") |
| 116 | + ) # Should error: list not assignable to str |
| 117 | + params_path_list_mismatch: Path = zntrack.params_path( |
| 118 | + ("config1.yaml", "config2.yaml") |
| 119 | + ) # Should error: list not assignable to Path |
| 120 | + # using default factory with list |
| 121 | + params_path_list_mismatch_lambda: Path = zntrack.params_path( |
| 122 | + default_factory=lambda: ["config1.yaml", "config2.yaml"] |
| 123 | + ) # Should error: list not assignable to Path |
| 124 | + |
| 125 | + # Factory function returning wrong type (should error when uncommented) |
| 126 | + param_factory_bad: int = zntrack.params( |
| 127 | + default_factory=str |
| 128 | + ) # Should error: str() returns str, not int |
| 129 | + param_factory_bad2: dict = zntrack.params( |
| 130 | + default_factory=list |
| 131 | + ) # Should error: list() returns list, not dict |
| 132 | + |
| 133 | + # zntrack.nwd as string |
| 134 | + nwd_as_str: str = zntrack.outs_path( |
| 135 | + zntrack.nwd / "output.txt" |
| 136 | + ) # Should error: str not assignable to Path |
0 commit comments