Use `functools.cache` instead of memoize and update tests accordingly

pull/3569/head
James Campbell 2024-12-12 13:58:02 +01:00
parent 4f9d9fba22
commit 8668583ba0
No known key found for this signature in database
3 changed files with 6 additions and 12 deletions

View File

@ -1,8 +1,7 @@
from enum import Enum
from functools import cache
from typing import Any, Dict, NamedTuple
from cytoolz.functoolz import memoize
class UnrecognizedTacoDomain(Exception):
"""Raised when a domain is not recognized."""
@ -90,7 +89,7 @@ SUPPORTED_DOMAINS: Dict[str, TACoDomain] = {
}
@memoize
@cache
def get_domain(d: Any) -> TACoDomain:
if not isinstance(d, str):
raise TypeError(f"domain must be a string, not {type(d)}")

View File

@ -1,9 +1,9 @@
import time
from decimal import Decimal
from functools import cache
from typing import Dict, List, Union
import requests
from cytoolz import memoize
from eth_typing import ChecksumAddress
from requests import RequestException
from web3 import Web3
@ -135,7 +135,7 @@ def rpc_endpoint_health_check(endpoint: str, max_drift_seconds: int = 60) -> boo
return True # finally!
@memoize
@cache
def get_default_rpc_endpoints(domain: TACoDomain) -> Dict[int, List[str]]:
"""
Fetches the default RPC endpoints for various chains

View File

@ -50,21 +50,16 @@ def test_get_default_rpc_endpoints(mocker):
polygon_chain=PolygonChain.AMOY,
)
bad_domain = TACoDomain(
name="bad_domain",
eth_chain=EthChain.SEPOLIA,
polygon_chain=PolygonChain.AMOY,
)
expected_result = {
1: ["http://endpoint1", "http://endpoint2"],
2: ["http://endpoint3", "http://endpoint4"],
}
assert get_default_rpc_endpoints(test_domain) == expected_result
get_default_rpc_endpoints.cache_clear()
# Mock a failed response
mock_get.return_value.status_code = 500
assert get_default_rpc_endpoints(bad_domain) == {}
assert get_default_rpc_endpoints(test_domain) == {}
def test_get_healthy_default_rpc_endpoints(mocker):