eth-typing: Common type annotations for ethereum python packages
eth-typing provides common type annotations for various Ethereum Python packages, offering a standardized set of types for improved type hinting and code clarity across the Ethereum ecosystem. It is actively maintained with frequent releases, currently at version 6.0.0, supporting modern Python versions. [1, 2, 5]
Warnings
- breaking Version 6.0.0 drops support for Python 3.8 and 3.9. Users on these Python versions should remain on `eth-typing<6` or upgrade their Python environment. [10]
- deprecated Several ABI-related TypedDict attributes (e.g., `constant`, `payable` in function types) are deprecated in favor of `stateMutability`. Additionally, specific function types (`ABIFunction`, `ABIConstructor`, etc.) should be used instead of the general `ABIElement` for clarity. [1, 9]
- gotcha Many fields in `eth-typing`'s `TypedDict` definitions (especially in `eth_typing.abi`) use `typing.NotRequired`. This means type checkers will not enforce their presence. Developers expecting all fields to be mandatory might face unexpected `KeyError`s at runtime if not handled defensively. [1]
Install
-
pip install eth-typing
Imports
- TypeStr
from eth_typing import TypeStr
- HexStr
from eth_typing.encoding import HexStr
- Address
from eth_typing.evm import Address
- ChecksumAddress
from eth_typing.evm import ChecksumAddress
- ABI
from eth_typing.abi import ABI
Quickstart
from eth_typing.evm import Address, ChecksumAddress
from eth_typing.encoding import HexStr
def process_ethereum_address(address: Address) -> ChecksumAddress:
# In a real application, you would perform checksum validation
# or other address-related logic here.
# For this example, we'll just cast it for demonstration.
# eth-utils or web3.py typically handle checksumming.
if not isinstance(address, str) or not address.startswith('0x'):
raise ValueError('Invalid address format')
# Simulate a checksummed address return
return ChecksumAddress(address.lower()) # Simplified for quickstart
my_raw_address: Address = Address('0x742d35Cc6634C0532925a3b844Bc454e4438f444')
checksum_addr: ChecksumAddress = process_ethereum_address(my_raw_address)
print(f'Processed Address: {checksum_addr}')
def get_hex_string_length(hex_str: HexStr) -> int:
return len(hex_str)
my_hex_string: HexStr = HexStr('0xabcdef123456')
print(f'Hex string length: {get_hex_string_length(my_hex_string)}')