From f4922bce66431fba2b39ebdd72b42169ad62ac02 Mon Sep 17 00:00:00 2001 From: Gabriel Levcovitz Date: Wed, 30 Jul 2025 19:03:58 -0300 Subject: [PATCH] tests(nano): fix usage of `raises` with match --- tests/nanocontracts/test_contract_upgrade.py | 10 +++--- tests/nanocontracts/test_rng.py | 33 ++++++++++---------- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/tests/nanocontracts/test_contract_upgrade.py b/tests/nanocontracts/test_contract_upgrade.py index 440feb9cd..909947aef 100644 --- a/tests/nanocontracts/test_contract_upgrade.py +++ b/tests/nanocontracts/test_contract_upgrade.py @@ -1,3 +1,5 @@ +import pytest + from hathor.nanocontracts import Blueprint, Context, fallback, public from hathor.nanocontracts.exception import BlueprintDoesNotExist, NCFail, NCInvalidSyscall, NCMethodNotFound from hathor.nanocontracts.types import BlueprintId, ContractId, NCAction, NCArgs @@ -127,7 +129,7 @@ def test_basic(self) -> None: assert isinstance(proxy_contract, ProxyBlueprint) self.runner.call_public_method(proxy_id, 'set_contract', ctx, proxy_id) - with self.assertRaises(NCInvalidSyscall, match='cannot call the same blueprint'): + with pytest.raises(NCInvalidSyscall, match='cannot call the same blueprint'): self.runner.call_public_method(proxy_id, 'inc', ctx) self.runner.call_public_method(proxy_id, 'set_contract', ctx, code1_id) @@ -154,7 +156,7 @@ def test_basic(self) -> None: assert code2_contract.counter == 0 assert proxy_contract.counter == 1 - with self.assertRaises(NCFail): + with pytest.raises(NCFail): self.runner.call_public_method(proxy_id, 'upgrade', ctx, self.code3_bp_id, 'on_upgrade_fail') assert proxy_storage.get_blueprint_id() == self.proxy_bp_id assert proxy_contract.counter == 1 @@ -168,7 +170,7 @@ def test_basic(self) -> None: assert proxy_contract.counter == 3 # it should invoke the fallback method which will fail calling `dec()` from code2's blueprint. - with self.assertRaises(NCMethodNotFound, match='method `dec` not found and no fallback is provided'): + with pytest.raises(NCMethodNotFound, match='method `dec` not found and no fallback is provided'): self.runner.call_public_method(proxy_id, 'dec', ctx) assert proxy_storage.get_blueprint_id() == self.proxy_bp_id assert proxy_contract.contract == code2_id @@ -177,7 +179,7 @@ def test_basic(self) -> None: assert proxy_contract.counter == 3 unknown_bp_id = self.gen_random_blueprint_id() - with self.assertRaises(BlueprintDoesNotExist): + with pytest.raises(BlueprintDoesNotExist): self.runner.call_public_method(proxy_id, 'upgrade_no_cb', ctx, unknown_bp_id) self.runner.call_public_method(proxy_id, 'upgrade', ctx, self.code3_bp_id, 'on_upgrade_inc') diff --git a/tests/nanocontracts/test_rng.py b/tests/nanocontracts/test_rng.py index 074eab22f..b467a5212 100644 --- a/tests/nanocontracts/test_rng.py +++ b/tests/nanocontracts/test_rng.py @@ -1,5 +1,7 @@ from math import floor, sqrt +import pytest + from hathor.conf import HathorSettings from hathor.nanocontracts import Blueprint, Context, public from hathor.nanocontracts.catalog import NCBlueprintCatalog @@ -71,41 +73,38 @@ def test_rng_override(self) -> None: seed = b'0' * 32 rng = NanoRNG(seed=seed) - with self.assertRaises(AttributeError, match='Cannot assign methods to this object.'): + with pytest.raises(AttributeError, match='Cannot assign methods to this object.'): rng.random = lambda self: 2 # type: ignore[method-assign, misc, assignment] - with self.assertRaises(AttributeError, match='Cannot assign methods to this object.'): + with pytest.raises(AttributeError, match='Cannot assign methods to this object.'): setattr(rng, 'random', lambda self: 2) - with self.assertRaises(AttributeError, match='Cannot assign methods to this object.'): + with pytest.raises(AttributeError, match='Cannot assign methods to this object.'): from types import MethodType rng.random = MethodType(lambda self: 2, rng) # type: ignore[method-assign] - with self.assertRaises(AttributeError, match='\'NanoRNG\' object attribute \'random\' is read-only'): + with pytest.raises(AttributeError, match='\'NanoRNG\' object attribute \'random\' is read-only'): object.__setattr__(rng, 'random', lambda self: 2) - with self.assertRaises(AttributeError, match='AttributeError: Cannot override method `random`'): + with pytest.raises(AttributeError, match='Cannot override method `random`'): NanoRNG.random = lambda self: 2 # type: ignore[method-assign] - with self.assertRaises(AttributeError, match='AttributeError: Cannot override method `random`'): + with pytest.raises(AttributeError, match='Cannot override method `random`'): setattr(NanoRNG, 'random', lambda self: 2) - with self.assertRaises(TypeError, match='can\'t apply this __setattr__ to NoMethodOverrideMeta object'): + with pytest.raises(TypeError, match='can\'t apply this __setattr__ to NoMethodOverrideMeta object'): object.__setattr__(NanoRNG, 'random', lambda self: 2) - with self.assertRaises(AttributeError, match='AttributeError: Cannot override method `random`'): + with pytest.raises(AttributeError, match='Cannot override method `random`'): rng.__class__.random = lambda self: 2 # type: ignore[method-assign] - with self.assertRaises(AttributeError, match='AttributeError: Cannot override method `random`'): + with pytest.raises(AttributeError, match='Cannot override method `random`'): setattr(rng.__class__, 'random', lambda self: 2) - with self.assertRaises(TypeError, match='can\'t apply this __setattr__ to NoMethodOverrideMeta object'): + with pytest.raises(TypeError, match='can\'t apply this __setattr__ to NoMethodOverrideMeta object'): object.__setattr__(rng.__class__, 'random', lambda self: 2) - # mypy incorrectly infers the type of `rng.random` as `Never` (leading to "Never not callable [misc]") - # due to the override attempts above, which are expected to fail at runtime but confuse static analysis. - # This is a false positive in the test context; use `reveal_type(rng.random)` to inspect the inferred type. - assert rng.random() < 1 # type: ignore[misc] + assert rng.random() < 1 def test_rng_shell_class(self) -> None: seed = b'0' * 32 @@ -114,13 +113,13 @@ def test_rng_shell_class(self) -> None: assert rng1.__class__ != rng2.__class__ - with self.assertRaises(AttributeError, match='AttributeError: Cannot override method `random`'): + with pytest.raises(AttributeError, match='Cannot override method `random`'): rng1.__class__.random = lambda self: 2 # type: ignore[method-assign] - with self.assertRaises(AttributeError, match='AttributeError: Cannot override method `random`'): + with pytest.raises(AttributeError, match='Cannot override method `random`'): setattr(rng1.__class__, 'random', lambda self: 2) - with self.assertRaises(TypeError, match='can\'t apply this __setattr__ to NoMethodOverrideMeta object'): + with pytest.raises(TypeError, match='can\'t apply this __setattr__ to NoMethodOverrideMeta object'): object.__setattr__(rng1.__class__, 'random', lambda self: 2) def assertGoodnessOfFitTest(self, observed: list[int], expected: list[int]) -> None: