Skip to content

Commit 9c106ad

Browse files
committed
feat: add type annotations to FixtureCache
- Added type annotations to FixtureCache class and its usage. - Updated cache_fixture method to use generic type FixtureCache[T]. - Improved type safety by replacing tp.Any with specific types. - Modified test files to handle the updated FixtureCache type.
1 parent 4c1bf80 commit 9c106ad

File tree

11 files changed

+46
-32
lines changed

11 files changed

+46
-32
lines changed

cardano_node_tests/cluster_management/cluster_management.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
# flake8: noqa
44
from cardano_node_tests.cluster_management.common import CLUSTER_LOCK
55
from cardano_node_tests.cluster_management.manager import ClusterManager
6+
from cardano_node_tests.cluster_management.manager import FixtureCache
67
from cardano_node_tests.cluster_management.resources import Resources

cardano_node_tests/cluster_management/manager.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
LOGGER = logging.getLogger(__name__)
3131

32+
T = tp.TypeVar("T")
33+
3234
if configuration.CLUSTERS_COUNT > 1 and configuration.DEV_CLUSTER_RUNNING:
3335
msg = "Cannot run multiple cluster instances when 'DEV_CLUSTER_RUNNING' is set."
3436
raise RuntimeError(msg)
@@ -43,10 +45,10 @@ def _get_manager_fixture_line_str() -> str:
4345

4446

4547
@dataclasses.dataclass
46-
class FixtureCache:
48+
class FixtureCache(tp.Generic[T]):
4749
"""Cache for a fixture."""
4850

49-
value: tp.Any
51+
value: T | None
5052

5153

5254
class ClusterManager:
@@ -219,12 +221,12 @@ def respin_on_failure(self) -> tp.Iterator[None]:
219221
raise
220222

221223
@contextlib.contextmanager
222-
def cache_fixture(self, key: str = "") -> tp.Iterator[FixtureCache]:
224+
def cache_fixture(self, key: str = "") -> tp.Iterator[FixtureCache[tp.Any]]:
223225
"""Cache fixture value - context manager."""
224226
key_str = key or _get_manager_fixture_line_str()
225227
key_hash = int(hashlib.sha1(key_str.encode("utf-8")).hexdigest(), 16)
226228
cached_value = self.cache.test_data.get(key_hash)
227-
container = FixtureCache(value=cached_value)
229+
container: FixtureCache[tp.Any] = FixtureCache(value=cached_value)
228230

229231
yield container
230232

cardano_node_tests/tests/common.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -347,9 +347,10 @@ def _create_addrs() -> list[clusterlib.AddressRecord]:
347347
return addrs
348348

349349
if caching_key:
350+
fixture_cache: cluster_management.FixtureCache[list[clusterlib.AddressRecord] | None]
350351
with cluster_manager.cache_fixture(key=caching_key) as fixture_cache:
351-
if fixture_cache.value:
352-
return fixture_cache.value # type: ignore
352+
if fixture_cache.value is not None:
353+
return fixture_cache.value
353354

354355
addrs = _create_addrs()
355356
fixture_cache.value = addrs
@@ -410,9 +411,10 @@ def _create_pool_users() -> list[clusterlib.PoolUser]:
410411
return users
411412

412413
if caching_key:
414+
fixture_cache: cluster_management.FixtureCache[list[clusterlib.PoolUser] | None]
413415
with cluster_manager.cache_fixture(key=caching_key) as fixture_cache:
414-
if fixture_cache.value:
415-
return fixture_cache.value # type: ignore
416+
if fixture_cache.value is not None:
417+
return fixture_cache.value
416418

417419
users = _create_pool_users()
418420
fixture_cache.value = users

cardano_node_tests/tests/test_mir_certs.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,10 @@ def registered_users(
8282
"""Register pool user's stake address."""
8383
registered = pool_users[1:3]
8484

85+
fixture_cache: cluster_management.FixtureCache[list[clusterlib.PoolUser] | None]
8586
with cluster_manager.cache_fixture() as fixture_cache:
86-
if fixture_cache.value:
87-
return fixture_cache.value # type: ignore
87+
if fixture_cache.value is not None:
88+
return fixture_cache.value
8889
fixture_cache.value = registered
8990

9091
for i, pool_user in enumerate(registered):

cardano_node_tests/tests/test_native_tokens.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1665,9 +1665,10 @@ def new_token(
16651665
cluster: clusterlib.ClusterLib,
16661666
payment_addrs: list[clusterlib.AddressRecord],
16671667
) -> clusterlib_utils.TokenRecord:
1668+
fixture_cache: cluster_management.FixtureCache[clusterlib_utils.TokenRecord | None]
16681669
with cluster_manager.cache_fixture() as fixture_cache:
1669-
if fixture_cache.value:
1670-
return fixture_cache.value # type: ignore
1670+
if fixture_cache.value is not None:
1671+
return fixture_cache.value
16711672

16721673
rand = clusterlib.get_rand_str(4)
16731674
temp_template = f"test_tx_new_token_ci{cluster_manager.cluster_instance_num}_{rand}"

cardano_node_tests/tests/test_tx_basic.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,10 @@ def byron_addrs(
5353
cluster: clusterlib.ClusterLib,
5454
) -> list[clusterlib.AddressRecord]:
5555
"""Create 2 new Byron payment addresses."""
56+
fixture_cache: cluster_management.FixtureCache[list[clusterlib.AddressRecord] | None]
5657
with cluster_manager.cache_fixture() as fixture_cache:
57-
if fixture_cache.value:
58-
return fixture_cache.value # type: ignore
58+
if fixture_cache.value is not None:
59+
return fixture_cache.value
5960

6061
new_byron_addrs = [
6162
clusterlib_utils.gen_byron_addr(

cardano_node_tests/tests/tests_conway/conway_common.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,10 @@ def _create_user() -> clusterlib.PoolUser:
129129
return pool_user
130130

131131
if caching_key:
132+
fixture_cache: cluster_management.FixtureCache[clusterlib.PoolUser | None]
132133
with cluster_manager.cache_fixture(key=caching_key) as fixture_cache:
133-
if fixture_cache.value:
134-
return fixture_cache.value # type: ignore
134+
if fixture_cache.value is not None:
135+
return fixture_cache.value
135136

136137
pool_user = _create_user()
137138
fixture_cache.value = pool_user

cardano_node_tests/tests/tests_conway/test_drep.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,10 @@ def get_custom_drep(
9898
if cluster_nodes.get_cluster_type().type != cluster_nodes.ClusterType.LOCAL:
9999
pytest.skip("runs only on local cluster")
100100

101+
fixture_cache: cluster_management.FixtureCache[governance_utils.DRepRegistration | None]
101102
with cluster_manager.cache_fixture(key=caching_key) as fixture_cache:
102-
if fixture_cache.value:
103-
return fixture_cache.value # type: ignore
103+
if fixture_cache.value is not None:
104+
return fixture_cache.value
104105

105106
reg_drep = create_drep(
106107
name_template=name_template,

cardano_node_tests/tests/tests_plutus/test_mint_build.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
pytest.mark.plutus,
3333
]
3434

35+
PHorizonFundsT = tuple[list[clusterlib.UTXOData], list[clusterlib.UTXOData], clusterlib.TxRawOutput]
36+
3537

3638
@pytest.fixture
3739
def payment_addrs(
@@ -59,11 +61,12 @@ def past_horizon_funds(
5961
cluster_manager: cluster_management.ClusterManager,
6062
cluster: clusterlib.ClusterLib,
6163
payment_addrs: list[clusterlib.AddressRecord],
62-
) -> tuple[list[clusterlib.UTXOData], list[clusterlib.UTXOData], clusterlib.TxRawOutput]:
64+
) -> PHorizonFundsT:
6365
"""Create UTxOs for `test_ttl_horizon`."""
66+
fixture_cache: cluster_management.FixtureCache[PHorizonFundsT | None]
6467
with cluster_manager.cache_fixture() as fixture_cache:
65-
if fixture_cache.value:
66-
return fixture_cache.value # type: ignore
68+
if fixture_cache.value is not None:
69+
return fixture_cache.value
6770

6871
temp_template = common.get_test_id(cluster)
6972
payment_addr = payment_addrs[0]
@@ -1140,9 +1143,7 @@ def test_ttl_horizon(
11401143
self,
11411144
cluster: clusterlib.ClusterLib,
11421145
payment_addrs: list[clusterlib.AddressRecord],
1143-
past_horizon_funds: tuple[
1144-
list[clusterlib.UTXOData], list[clusterlib.UTXOData], clusterlib.TxRawOutput
1145-
],
1146+
past_horizon_funds: PHorizonFundsT,
11461147
plutus_version: str,
11471148
ttl_offset: int,
11481149
):

cardano_node_tests/tests/tests_plutus/test_spend_negative_raw.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -734,9 +734,10 @@ def fund_script_guessing_game_v1(
734734
cluster_manager: cluster_management.ClusterManager,
735735
cluster: clusterlib.ClusterLib,
736736
) -> FundTupleT:
737+
fixture_cache: cluster_management.FixtureCache[FundTupleT | None]
737738
with cluster_manager.cache_fixture() as fixture_cache:
738-
if fixture_cache.value:
739-
return fixture_cache.value # type: ignore
739+
if fixture_cache.value is not None:
740+
return fixture_cache.value
740741

741742
temp_template = common.get_test_id(cluster)
742743

@@ -756,9 +757,10 @@ def fund_script_guessing_game_v2(
756757
cluster_manager: cluster_management.ClusterManager,
757758
cluster: clusterlib.ClusterLib,
758759
) -> FundTupleT:
760+
fixture_cache: cluster_management.FixtureCache[FundTupleT | None]
759761
with cluster_manager.cache_fixture() as fixture_cache:
760-
if fixture_cache.value:
761-
return fixture_cache.value # type: ignore
762+
if fixture_cache.value is not None:
763+
return fixture_cache.value
762764

763765
temp_template = common.get_test_id(cluster)
764766

0 commit comments

Comments
 (0)