From 21303b61c7a1d382f753cb852fbbefb02f87c742 Mon Sep 17 00:00:00 2001 From: allt0ld Date: Tue, 22 Aug 2023 12:54:33 -0400 Subject: [PATCH 1/6] Replace `license_file` with `license_files` --- setup.cfg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From ebac9aafcfb5bda795659bce0118c56f704a51df Mon Sep 17 00:00:00 2001 From: allt0ld Date: Thu, 24 Aug 2023 09:16:08 -0400 Subject: [PATCH 2/6] Add `asyncio.get_event_loop()` replacement to `utils` --- curvesim/utils.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/curvesim/utils.py b/curvesim/utils.py index 4abd1d5f2..68299ba52 100644 --- a/curvesim/utils.py +++ b/curvesim/utils.py @@ -1,6 +1,7 @@ """Utlity functions for general usage in Curvesim.""" import functools import inspect +import asyncio import os import re import sys @@ -156,3 +157,21 @@ def dataclass(*args, **kwargs): del kwargs["slots"] return _dataclass(*args, **kwargs) + + +def get_event_loop(): + """ + reasoning, stackoverflow post + default asyncio.get_event_loop() will simply emit a DeprecationWarning + if there is no event loop set or running This will become an error in future releases + + Generally, after calling this, you should run scheduled coroutines soon after + to avoid overwriting event loops that have unrun coroutines. + """ + try: + loop = asyncio.get_running_loop() + except RuntimeError: + loop = asyncio.new_event_loop() + asyncio.set_event_loop(loop) + + return loop \ No newline at end of file From 2c1fe33a5d9bf4892365fa44a9556d6faaac62f2 Mon Sep 17 00:00:00 2001 From: allt0ld Date: Thu, 24 Aug 2023 09:24:07 -0400 Subject: [PATCH 3/6] Import and use `curvesim.utils.get_event_loop` --- curvesim/network/nomics.py | 6 +++--- curvesim/network/utils.py | 4 +++- curvesim/pool_data/queries.py | 4 +++- 3 files changed, 9 insertions(+), 5 deletions(-) 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..d94117a36 100644 --- a/curvesim/pool_data/queries.py +++ b/curvesim/pool_data/queries.py @@ -1,5 +1,7 @@ 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 +24,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 From c027dd0df709e65ef93e8b32a11b55c18d2f352d Mon Sep 17 00:00:00 2001 From: allt0ld Date: Thu, 24 Aug 2023 17:19:18 -0400 Subject: [PATCH 4/6] Write docstring and lint --- curvesim/pool_data/queries.py | 2 -- curvesim/utils.py | 22 ++++++++++++++-------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/curvesim/pool_data/queries.py b/curvesim/pool_data/queries.py index d94117a36..e61bb7135 100644 --- a/curvesim/pool_data/queries.py +++ b/curvesim/pool_data/queries.py @@ -1,5 +1,3 @@ -import asyncio - from curvesim.utils import get_event_loop from ..network.subgraph import pool_snapshot_sync, symbol_address_sync diff --git a/curvesim/utils.py b/curvesim/utils.py index 68299ba52..27f65fe40 100644 --- a/curvesim/utils.py +++ b/curvesim/utils.py @@ -1,7 +1,7 @@ """Utlity functions for general usage in Curvesim.""" +import asyncio import functools import inspect -import asyncio import os import re import sys @@ -161,17 +161,23 @@ def dataclass(*args, **kwargs): def get_event_loop(): """ - reasoning, stackoverflow post - default asyncio.get_event_loop() will simply emit a DeprecationWarning - if there is no event loop set or running This will become an error in future releases + 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. - Generally, after calling this, you should run scheduled coroutines soon after - to avoid overwriting event loops that have unrun coroutines. + Implementation slightly modified from https://stackoverflow.com/a/73884759 + as supporting all Python 3.x isn't necessary. """ - try: + try: loop = asyncio.get_running_loop() except RuntimeError: loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) - return loop \ No newline at end of file + return loop From 3727b6605b5a527389e36615932036e5e2ce420e Mon Sep 17 00:00:00 2001 From: allt0ld Date: Thu, 24 Aug 2023 17:22:57 -0400 Subject: [PATCH 5/6] Edit docstring --- curvesim/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/curvesim/utils.py b/curvesim/utils.py index 27f65fe40..09b39deb6 100644 --- a/curvesim/utils.py +++ b/curvesim/utils.py @@ -172,7 +172,7 @@ def get_event_loop(): 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 supporting all Python 3.x isn't necessary. + as below works for all versions >= 3.7. """ try: loop = asyncio.get_running_loop() From bf6d1490a41b39dd6f5cd71e95ff7f504cf2c088 Mon Sep 17 00:00:00 2001 From: allt0ld Date: Thu, 24 Aug 2023 18:31:39 -0400 Subject: [PATCH 6/6] Add changelog entry --- ...824_175938_philiplu97_fix_deprecations.rst | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 changelog.d/20230824_175938_philiplu97_fix_deprecations.rst 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. +..