diff --git a/faster_web3/exceptions.py b/faster_web3/exceptions.py index 054ec98324..4a875a5245 100644 --- a/faster_web3/exceptions.py +++ b/faster_web3/exceptions.py @@ -4,8 +4,14 @@ TYPE_CHECKING, Any, Dict, + Final, Optional, Union, + final, +) + +from mypy_extensions import ( + mypyc_attr, ) from faster_web3.types import ( @@ -17,6 +23,7 @@ import asyncio +@mypyc_attr(native_class=False) class Web3Exception(Exception): """ Exception mixin inherited by all exceptions of web3.py @@ -31,7 +38,7 @@ class Web3Exception(Exception): # deal with other exceptions """ - user_message: Optional[str] = None + user_message: Final[Optional[str]] def __init__( self, @@ -44,6 +51,8 @@ def __init__( self.user_message = user_message +@final +@mypyc_attr(native_class=False) class Web3AssertionError(Web3Exception, AssertionError): """ A web3.py exception wrapper for `AssertionError`, for better control over @@ -51,6 +60,7 @@ class Web3AssertionError(Web3Exception, AssertionError): """ +@mypyc_attr(native_class=False) class Web3ValueError(Web3Exception, ValueError): """ A web3.py exception wrapper for `ValueError`, for better control over @@ -58,6 +68,8 @@ class Web3ValueError(Web3Exception, ValueError): """ +@final +@mypyc_attr(native_class=False) class Web3AttributeError(Web3Exception, AttributeError): """ A web3.py exception wrapper for `AttributeError`, for better control over @@ -65,6 +77,8 @@ class Web3AttributeError(Web3Exception, AttributeError): """ +@final +@mypyc_attr(native_class=False) class Web3TypeError(Web3Exception, TypeError): """ A web3.py exception wrapper for `TypeError`, for better control over @@ -72,12 +86,14 @@ class Web3TypeError(Web3Exception, TypeError): """ +@final class MethodNotSupported(Web3Exception): """ Raised when a method is not supported by the provider. """ +@final class BadFunctionCallOutput(Web3Exception): """ We failed to decode ABI output. @@ -86,18 +102,21 @@ class BadFunctionCallOutput(Web3Exception): """ +@final class BlockNumberOutOfRange(Web3Exception): """ block_identifier passed does not match known block. """ +@final class ProviderConnectionError(Web3Exception): """ Raised when unable to connect to a provider """ +@final class CannotHandleRequest(Web3Exception): """ Raised by a provider to signal that it cannot handle an RPC request and @@ -105,12 +124,14 @@ class CannotHandleRequest(Web3Exception): """ +@final class TooManyRequests(Web3Exception): """ Raised by a provider to signal that too many requests have been made consecutively. """ +@final class MultipleFailedRequests(Web3Exception): """ Raised by a provider to signal that multiple requests to retrieve the same @@ -118,12 +139,14 @@ class MultipleFailedRequests(Web3Exception): """ +@final class InvalidAddress(Web3Exception): """ The supplied address does not have a valid checksum, as defined in EIP-55 """ +@final class NameNotFound(Web3Exception): """ Raised when a caller provides an Ethereum Name Service name that @@ -131,6 +154,7 @@ class NameNotFound(Web3Exception): """ +@final class StaleBlockchain(Web3Exception): """ Raised by the stalecheck_middleware when the latest block is too old. @@ -153,6 +177,8 @@ def __str__(self) -> str: return self.args[0] +@final +@mypyc_attr(native_class=False) class MismatchedABI(Web3Exception): """ Raised when an ABI does not match with supplied parameters, or when an @@ -160,6 +186,8 @@ class MismatchedABI(Web3Exception): """ +@final +@mypyc_attr(native_class=False) class ABIEventNotFound(AttributeError, MismatchedABI): """ Raised when an attempt is made to access an event @@ -167,6 +195,8 @@ class ABIEventNotFound(AttributeError, MismatchedABI): """ +@final +@mypyc_attr(native_class=False) class ABIFunctionNotFound(AttributeError, MismatchedABI): """ Raised when an attempt is made to access a function @@ -174,18 +204,21 @@ class ABIFunctionNotFound(AttributeError, MismatchedABI): """ +@final class ABIConstructorNotFound(Web3Exception): """ Raised when a constructor function doesn't exist in contract. """ +@final class ABIFallbackNotFound(Web3Exception): """ Raised when a fallback function doesn't exist in contract. """ +@final class ABIReceiveNotFound(Web3Exception): """ Raised when a receive function doesn't exist in contract. @@ -198,30 +231,35 @@ class Web3ValidationError(Web3Exception): """ +@final class ExtraDataLengthError(Web3ValidationError): """ Raised when an RPC call returns >32 bytes of extraData. """ +@final class NoABIFunctionsFound(Web3Exception): """ Raised when an ABI is present, but doesn't contain any functions. """ +@final class NoABIFound(Web3Exception): """ Raised when no ABI is present. """ +@final class NoABIEventsFound(Web3Exception): """ Raised when an ABI doesn't contain any events. """ +@final class InsufficientData(Web3Exception): """ Raised when there are insufficient data points to @@ -229,6 +267,7 @@ class InsufficientData(Web3Exception): """ +@final class TimeExhausted(Web3Exception): """ Raised when a method has not retrieved the desired @@ -236,18 +275,21 @@ class TimeExhausted(Web3Exception): """ +@final class InfuraProjectIdNotFound(Web3Exception): """ Raised when there is no Infura Project Id set. """ +@final class LogTopicError(Web3Exception): """ Raised when the number of log topics is mismatched. """ +@final class InvalidEventABI(Web3Exception): """ Raised when the event ABI is invalid. @@ -265,30 +307,33 @@ def __init__( data: Optional[Union[str, Dict[str, str]]] = None, ): super().__init__(message, data) - self.message = message - self.data = data + self.message: Final = message + self.data: Final = data +@final class ContractCustomError(ContractLogicError): """ Raised on a contract revert custom error """ +@final class ContractPanicError(ContractLogicError): """ Raised when a contract reverts with Panic, as of Solidity 0.8.0 """ +@final class OffchainLookup(ContractLogicError): """ Raised when a contract reverts with OffchainLookup as described in EIP-3668 """ def __init__(self, payload: Dict[str, Any], data: Optional[str] = None) -> None: - self.payload = payload - self.data = data + self.payload: Final = payload + self.data: Final = data super().__init__(data=data) @@ -301,6 +346,7 @@ def __init__(self, message: str) -> None: super().__init__(message) +@final class TransactionTypeMismatch(InvalidTransaction): """ Raised when legacy transaction values are used alongside dynamic @@ -312,6 +358,7 @@ def __init__(self) -> None: super().__init__(message) +@final class BadResponseFormat(Web3Exception): """ Raised when a JSON-RPC response comes back in an unexpected format @@ -327,19 +374,22 @@ class TaskNotRunning(Web3Exception): def __init__( self, task: "asyncio.Task[Any]", message: Optional[str] = None ) -> None: - self.task = task + self.task: Final = task if message is None: message = f"Task {task} is not running." - self.message = message + self.message: Final = message super().__init__(message) +@mypyc_attr(native_class=False) class PersistentConnectionError(Web3Exception): """ Raised when a persistent connection encounters an error. """ +@final +@mypyc_attr(native_class=False) class ReadBufferLimitReached(PersistentConnectionError, Web3ValueError): """ Raised when the read buffer limit is reached while reading data from a persistent @@ -347,12 +397,16 @@ class ReadBufferLimitReached(PersistentConnectionError, Web3ValueError): """ +@final +@mypyc_attr(native_class=False) class PersistentConnectionClosedOK(PersistentConnectionError): """ Raised when a persistent connection is closed gracefully by the server. """ +@final +@mypyc_attr(native_class=False) class SubscriptionProcessingFinished(PersistentConnectionError): """ Raised to alert the subscription manager that the processing of subscriptions @@ -360,6 +414,7 @@ class SubscriptionProcessingFinished(PersistentConnectionError): """ +@final class SubscriptionHandlerTaskException(TaskNotRunning): """ Raised to alert the subscription manager that an exception occurred in the @@ -388,28 +443,32 @@ def __init__( message, user_message=user_message, ) - self.message = message - self.rpc_response = rpc_response + self.message: Final = message + self.rpc_response: Final = rpc_response +@final class MethodUnavailable(Web3RPCError): """ Raised when the method is not available on the node """ +@final class RequestTimedOut(Web3RPCError): """ Raised when a request to the node times out. """ +@final class TransactionNotFound(Web3RPCError): """ Raised when a tx hash used to look up a tx in a jsonrpc call cannot be found. """ +@final class TransactionIndexingInProgress(Web3RPCError): """ Raised when a transaction receipt is not yet available due to transaction indexing @@ -417,6 +476,7 @@ class TransactionIndexingInProgress(Web3RPCError): """ +@final class BlockNotFound(Web3RPCError): """ Raised when the block id used to look up a block in a jsonrpc call cannot be found. diff --git a/setup.py b/setup.py index 32021b698e..ae56e16c79 100644 --- a/setup.py +++ b/setup.py @@ -97,6 +97,7 @@ "faster_web3/beacon", "faster_web3/constants.py", "faster_web3/contract/utils.py", + "faster_web3/exceptions.py", "faster_web3/gas_strategies", "faster_web3/providers/eth_tester", "faster_web3/tools/benchmark/node.py",