diff --git a/python/cdp/evm_local_account.py b/python/cdp/evm_local_account.py index 2c1cb0d0d..8bee2905f 100644 --- a/python/cdp/evm_local_account.py +++ b/python/cdp/evm_local_account.py @@ -1,7 +1,6 @@ import asyncio from typing import Any -import nest_asyncio from eth_account.datastructures import SignedMessage, SignedTransaction from eth_account.messages import SignableMessage, _hash_eip191_message, encode_typed_data from eth_account.signers.base import BaseAccount @@ -11,17 +10,16 @@ from cdp.errors import UserInputValidationError from cdp.evm_server_account import EvmServerAccount -# Apply nest-asyncio to allow nested event loops, but only if compatible +# Apply nest-asyncio to allow nested event loops (e.g., Jupyter notebooks). +# Catch ImportError if nest_asyncio is not installed, TypeError if it conflicts +# with uvicorn's loop_factory on Python 3.12+, and ValueError if the loop +# cannot be patched (e.g., uvloop). try: + import nest_asyncio + nest_asyncio.apply() -except ValueError as e: - # If nest_asyncio can't patch the loop (e.g., uvloop), silently continue - # This commonly happens when uvloop is installed and running - if "Can't patch loop" in str(e): - pass - else: - # Re-raise other ValueError exceptions - raise +except (ImportError, TypeError, ValueError): + pass def _run_async(coroutine):