Skip to content

Commit

Permalink
MAINT: Use importlib.metadata and packaging instead of deprecated pkg…
Browse files Browse the repository at this point in the history
…_resources (#3133)

* MAINT: Use importlib.metadata and packaging instead of deprecated pkg_resources

* Consolidate importlib.metadata imports
  • Loading branch information
binste authored Aug 1, 2023
1 parent fa610fc commit 157c4e3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
32 changes: 16 additions & 16 deletions altair/utils/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import random
import hashlib
import warnings
from importlib.metadata import version as importlib_version, PackageNotFoundError
from typing import Union, MutableMapping, Optional, Dict, Sequence, TYPE_CHECKING, List
from types import ModuleType

import pandas as pd
from toolz import curried
from typing import TypeVar
from packaging.version import Version

from .core import sanitize_dataframe, sanitize_arrow_table, _DataFrameLike
from .core import sanitize_geo_interface
Expand Down Expand Up @@ -349,25 +351,23 @@ def curry(*args, **kwargs):


def import_pyarrow_interchange() -> ModuleType:
import pkg_resources

try:
pkg_resources.require("pyarrow>=11.0.0")
# The package is installed and meets the minimum version requirement
import pyarrow.interchange as pi

return pi
except pkg_resources.DistributionNotFound as err:
# The package is not installed
raise ImportError(
"Usage of the DataFrame Interchange Protocol requires the package 'pyarrow', but it is not installed."
) from err
except pkg_resources.VersionConflict as err:
# The package is installed but does not meet the minimum version requirement
pyarrow_version_str = importlib_version("pyarrow")
except PackageNotFoundError as err:
raise ImportError(
"The installed version of 'pyarrow' does not meet the minimum requirement of version 11.0.0. "
"Please update 'pyarrow' to use the DataFrame Interchange Protocol."
"Usage of the DataFrame Interchange Protocol requires the package"
+ " 'pyarrow', but it is not installed."
) from err
else:
if Version(pyarrow_version_str) < Version("11.0.0"):
raise ImportError(
"The installed version of 'pyarrow' does not meet the minimum requirement of version 11.0.0. "
"Please update 'pyarrow' to use the DataFrame Interchange Protocol."
)
else:
import pyarrow.interchange as pi

return pi


def pyarrow_available() -> bool:
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ dependencies = [
"numpy",
# If you update the minimum required pandas version, also update it in build.yml
"pandas>=0.25",
"toolz"
"toolz",
"packaging"
]
description = "Vega-Altair: A declarative statistical visualization library for Python."
readme = "README.md"
Expand Down

0 comments on commit 157c4e3

Please sign in to comment.