From f2e7ea2ff26265533e2aef294c6f3a362ed3e946 Mon Sep 17 00:00:00 2001 From: Object905 Date: Wed, 6 Sep 2023 10:59:27 +0500 Subject: [PATCH] feat: add gevent to dependencies.py, raise ImportError when it's not available --- py-polars/polars/dependencies.py | 5 +++++ py-polars/polars/utils/_async.py | 7 +++++++ py-polars/polars/utils/show_versions.py | 1 + py-polars/pyproject.toml | 1 + py-polars/tests/unit/test_async.py | 2 +- 5 files changed, 15 insertions(+), 1 deletion(-) diff --git a/py-polars/polars/dependencies.py b/py-polars/polars/dependencies.py index b042e0c913823..9ba3335b11a38 100644 --- a/py-polars/polars/dependencies.py +++ b/py-polars/polars/dependencies.py @@ -17,6 +17,7 @@ _PYARROW_AVAILABLE = True _PYDANTIC_AVAILABLE = True _ZONEINFO_AVAILABLE = True +_GEVENT_AVAILABLE = True class _LazyModule(ModuleType): @@ -154,6 +155,7 @@ def _lazy_import(module_name: str) -> tuple[ModuleType, bool]: import dataframe_api_compat import deltalake import fsspec + import gevent import hypothesis import numpy import pandas @@ -188,6 +190,7 @@ def _lazy_import(module_name: str) -> tuple[ModuleType, bool]: if sys.version_info >= (3, 9) else _lazy_import("backports.zoneinfo") ) + gevent, _GEVENT_AVAILABLE = _lazy_import("gevent") @lru_cache(maxsize=None) @@ -232,6 +235,7 @@ def _check_for_pydantic(obj: Any) -> bool: "pydantic", "pyarrow", "zoneinfo", + "gevent", # lazy utilities "_check_for_numpy", "_check_for_pandas", @@ -246,4 +250,5 @@ def _check_for_pydantic(obj: Any) -> bool: "_PANDAS_AVAILABLE", "_PYARROW_AVAILABLE", "_ZONEINFO_AVAILABLE", + "_GEVENT_AVAILABLE", ] diff --git a/py-polars/polars/utils/_async.py b/py-polars/polars/utils/_async.py index edac5d750ace2..9b401758423da 100644 --- a/py-polars/polars/utils/_async.py +++ b/py-polars/polars/utils/_async.py @@ -2,6 +2,7 @@ from typing import TYPE_CHECKING, Any, Awaitable, Generator, Generic, TypeVar +from polars.dependencies import _GEVENT_AVAILABLE from polars.utils._wrap import wrap_df if TYPE_CHECKING: @@ -17,6 +18,12 @@ class _GeventDataFrameResult(Generic[T]): __slots__ = ("_watcher", "_value", "_result") def __init__(self) -> None: + if not _GEVENT_AVAILABLE: + raise ImportError( + "gevent is required for using LazyFrame.collect_async(gevent=True) or" + "polars.collect_all_async(gevent=True)" + ) + from gevent.event import AsyncResult # type: ignore[import] from gevent.hub import get_hub # type: ignore[import] diff --git a/py-polars/polars/utils/show_versions.py b/py-polars/polars/utils/show_versions.py index c92b46f2e875b..998ff6cdd760d 100644 --- a/py-polars/polars/utils/show_versions.py +++ b/py-polars/polars/utils/show_versions.py @@ -71,6 +71,7 @@ def _get_dependency_info() -> dict[str, str]: "sqlalchemy", "xlsx2csv", "xlsxwriter", + "gevent", ] return {f"{name}:": _get_dependency_version(name) for name in opt_deps} diff --git a/py-polars/pyproject.toml b/py-polars/pyproject.toml index 4b2737340bc27..9a5c4447ea55c 100644 --- a/py-polars/pyproject.toml +++ b/py-polars/pyproject.toml @@ -86,6 +86,7 @@ module = [ "xlsx2csv", "xlsxwriter.*", "zoneinfo", + "gevent", ] ignore_missing_imports = true diff --git a/py-polars/tests/unit/test_async.py b/py-polars/tests/unit/test_async.py index 9e874c96ad5b1..85ac2e7ced3cf 100644 --- a/py-polars/tests/unit/test_async.py +++ b/py-polars/tests/unit/test_async.py @@ -5,10 +5,10 @@ from functools import partial from typing import Any, Callable -import gevent # type: ignore[import] import pytest import polars as pl +from polars.dependencies import gevent async def _aio_collect_async(raises: bool = False) -> pl.DataFrame: