Skip to content

Commit

Permalink
feat: add gevent to dependencies.py, raise ImportError when it's not …
Browse files Browse the repository at this point in the history
…available
  • Loading branch information
Object905 committed Sep 6, 2023
1 parent 6bdf51f commit f2e7ea2
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 1 deletion.
5 changes: 5 additions & 0 deletions py-polars/polars/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
_PYARROW_AVAILABLE = True
_PYDANTIC_AVAILABLE = True
_ZONEINFO_AVAILABLE = True
_GEVENT_AVAILABLE = True


class _LazyModule(ModuleType):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -232,6 +235,7 @@ def _check_for_pydantic(obj: Any) -> bool:
"pydantic",
"pyarrow",
"zoneinfo",
"gevent",
# lazy utilities
"_check_for_numpy",
"_check_for_pandas",
Expand All @@ -246,4 +250,5 @@ def _check_for_pydantic(obj: Any) -> bool:
"_PANDAS_AVAILABLE",
"_PYARROW_AVAILABLE",
"_ZONEINFO_AVAILABLE",
"_GEVENT_AVAILABLE",
]
7 changes: 7 additions & 0 deletions py-polars/polars/utils/_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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]

Expand Down
1 change: 1 addition & 0 deletions py-polars/polars/utils/show_versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down
1 change: 1 addition & 0 deletions py-polars/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ module = [
"xlsx2csv",
"xlsxwriter.*",
"zoneinfo",
"gevent",
]
ignore_missing_imports = true

Expand Down
2 changes: 1 addition & 1 deletion py-polars/tests/unit/test_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit f2e7ea2

Please sign in to comment.