diff --git a/changelog.d/20230824_175938_philiplu97_fix_deprecations.rst b/changelog.d/20230824_175938_philiplu97_fix_deprecations.rst new file mode 100644 index 000000000..42ad1bd3a --- /dev/null +++ b/changelog.d/20230824_175938_philiplu97_fix_deprecations.rst @@ -0,0 +1,37 @@ +.. A new scriv changelog fragment. +.. +.. Uncomment the header that is right (remove the leading dots). +.. +.. Removed +.. ------- +.. +.. - A bullet item for the Removed category. +.. +Added +----- + +- Utils now has get_event_loop to access the event loop, which deprecates asyncio.get_event_loop. + +Changed +------- + +- In curvesim.network.nomics, curvesim.network.utils, and curvesim.pool_data.queries, all instances of + asyncio.get_event_loop were replaced with get_event_loop imported from curvesim.utils. + +- Updated deprecated license_file parameter to license_files in setup.cfg. + +.. Deprecated +.. ---------- +.. +.. - A bullet item for the Deprecated category. +.. +.. Fixed +.. ----- +.. +.. - A bullet item for the Fixed category. +.. +.. Security +.. -------- +.. +.. - A bullet item for the Security category. +.. diff --git a/curvesim/network/nomics.py b/curvesim/network/nomics.py index c74a65ea5..4ee9a3d20 100644 --- a/curvesim/network/nomics.py +++ b/curvesim/network/nomics.py @@ -10,7 +10,7 @@ from numpy import NaN from curvesim.logging import get_logger -from curvesim.utils import get_env_var +from curvesim.utils import get_env_var, get_event_loop from .http import HTTP from .utils import sync @@ -178,7 +178,7 @@ def update( t_start_orig = t_start t_end = t_end.replace(tzinfo=timezone.utc) - loop = asyncio.get_event_loop() + loop = get_event_loop() coins = coin_ids_from_addresses_sync(coins, event_loop=loop) # Coins priced against one another @@ -279,7 +279,7 @@ def pool_prices( # noqa: C901 pzero : pandas.Series Proportion of timestamps with zero volume. """ - loop = asyncio.get_event_loop() + loop = get_event_loop() coins = coins or [] pairs = pairs or [] diff --git a/curvesim/network/utils.py b/curvesim/network/utils.py index b8ad2ed34..3b63ac6d9 100644 --- a/curvesim/network/utils.py +++ b/curvesim/network/utils.py @@ -5,6 +5,8 @@ from gmpy2 import mpz +from curvesim.utils import get_event_loop + def compute_D(xp, A): """Standalone `D` calc neede for some data processing.""" @@ -59,7 +61,7 @@ def sync(func): @functools.wraps(func) def inner(*args, event_loop=None, **kwargs): - loop = event_loop or asyncio.get_event_loop() + loop = event_loop or get_event_loop() coro = func(*args, **kwargs) if loop.is_running(): # If for some reason, we are trying to make async code diff --git a/curvesim/pool_data/queries.py b/curvesim/pool_data/queries.py index 2731bf016..e61bb7135 100644 --- a/curvesim/pool_data/queries.py +++ b/curvesim/pool_data/queries.py @@ -1,4 +1,4 @@ -import asyncio +from curvesim.utils import get_event_loop from ..network.subgraph import pool_snapshot_sync, symbol_address_sync from ..network.web3 import underlying_coin_info_sync @@ -22,7 +22,7 @@ def from_address(address, chain, env="prod", end_ts=None): Pool snapshot dictionary in the format returned by :func:`curvesim.network.subgraph.pool_snapshot`. """ - loop = asyncio.get_event_loop() + loop = get_event_loop() data = pool_snapshot_sync(address, chain, env=env, end_ts=end_ts, event_loop=loop) # Get underlying token addresses diff --git a/curvesim/utils.py b/curvesim/utils.py index 4abd1d5f2..09b39deb6 100644 --- a/curvesim/utils.py +++ b/curvesim/utils.py @@ -1,4 +1,5 @@ """Utlity functions for general usage in Curvesim.""" +import asyncio import functools import inspect import os @@ -156,3 +157,27 @@ def dataclass(*args, **kwargs): del kwargs["slots"] return _dataclass(*args, **kwargs) + + +def get_event_loop(): + """ + Access the event loop without using asyncio.get_event_loop(). + + Generally, you should run scheduled coroutines soon after calling + this to avoid overwriting event loops that have unrun coroutines. + + Calling asyncio.get_event_loop() when an event loop isn't running and/or + set will cause a DeprecationWarning in various versions of Python 3.10-3.12, + and a future release will start raising an error instead: + https://docs.python.org/3.11/library/asyncio-eventloop.html?highlight=selectoreventloop#asyncio.get_event_loop. + + Implementation slightly modified from https://stackoverflow.com/a/73884759 + as below works for all versions >= 3.7. + """ + try: + loop = asyncio.get_running_loop() + except RuntimeError: + loop = asyncio.new_event_loop() + asyncio.set_event_loop(loop) + + return loop diff --git a/setup.cfg b/setup.cfg index a7bad4ded..1c8c90786 100644 --- a/setup.cfg +++ b/setup.cfg @@ -51,7 +51,7 @@ url = https://github.com/curveresearch/curvesim author = Curve Research author_email = help@curveresearch.org license = MIT -license_file = LICENSE.md +license_files = LICENSE.md classifiers = Development Status :: 4 - Beta Environment :: Console