Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix deprecations #212

Merged
merged 6 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions changelog.d/20230824_175938_philiplu97_fix_deprecations.rst
Original file line number Diff line number Diff line change
@@ -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.
..
6 changes: 3 additions & 3 deletions curvesim/network/nomics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 []
Expand Down
4 changes: 3 additions & 1 deletion curvesim/network/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions curvesim/pool_data/queries.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
25 changes: 25 additions & 0 deletions curvesim/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Utlity functions for general usage in Curvesim."""
import asyncio
import functools
import inspect
import os
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ url = https://github.com/curveresearch/curvesim
author = Curve Research
author_email = [email protected]
license = MIT
license_file = LICENSE.md
license_files = LICENSE.md
classifiers =
Development Status :: 4 - Beta
Environment :: Console
Expand Down
Loading