Skip to content

Commit

Permalink
use import lib
Browse files Browse the repository at this point in the history
  • Loading branch information
bh2smith committed Apr 21, 2024
1 parent fe4cc78 commit 42a90fd
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions dune_client/util.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Utility methods for package."""

from datetime import datetime, timezone
import importlib
from typing import Optional

DUNE_DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
Expand All @@ -13,17 +14,12 @@ def postgres_date(date_str: str) -> datetime:

def get_package_version(package_name: str) -> Optional[str]:
"""
Returns the package version by `package_name`
Returns the package version by `package_name` using the importlib.metadata module
which is available in Python 3.8 and later.
"""
try:
# pylint: disable=import-outside-toplevel
import pkg_resources

return pkg_resources.get_distribution(package_name).version
except ModuleNotFoundError:
# Python 3.12 does not have pkg_resources!
return None
except pkg_resources.DistributionNotFound:
return importlib.metadata.version(package_name)
except importlib.metadata.PackageNotFoundError:
return None


Expand Down

0 comments on commit 42a90fd

Please sign in to comment.