Skip to content

Commit

Permalink
FIX: restore importlib version detection for pypi
Browse files Browse the repository at this point in the history
  • Loading branch information
erichare committed Feb 1, 2024
1 parent 2fbaedd commit 1c859cd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
39 changes: 23 additions & 16 deletions astrapy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,35 @@

import toml
import os
import importlib.metadata

from typing import Any


def get_version() -> Any:
# Get the path to the pyproject.toml file
dir_path = os.path.dirname(os.path.realpath(__file__))
pyproject_path = os.path.join(dir_path, "..", "pyproject.toml")

# Read the pyproject.toml file and get the version from the poetry section
try:
with open(pyproject_path, encoding="utf-8") as pyproject:
# Load the pyproject.toml file as a dictionary
file_contents = pyproject.read()
pyproject_data = toml.loads(file_contents)

# Return the version from the poetry section
return pyproject_data["tool"]["poetry"]["version"]

# If the pyproject.toml file does not exist or the version is not found, return unknown
except (FileNotFoundError, KeyError):
return "unknown"
# Poetry will create a __version__ attribute in the package's __init__.py file
return importlib.metadata.version(__package__)

# If the package is not installed, we can still get the version from the pyproject.toml file
except importlib.metadata.PackageNotFoundError:
# Get the path to the pyproject.toml file
dir_path = os.path.dirname(os.path.realpath(__file__))
pyproject_path = os.path.join(dir_path, "..", "pyproject.toml")

# Read the pyproject.toml file and get the version from the poetry section
try:
with open(pyproject_path, encoding="utf-8") as pyproject:
# Load the pyproject.toml file as a dictionary
file_contents = pyproject.read()
pyproject_data = toml.loads(file_contents)

# Return the version from the poetry section
return pyproject_data["tool"]["poetry"]["version"]

# If the pyproject.toml file does not exist or the version is not found, return unknown
except (FileNotFoundError, KeyError):
return "unknown"


__version__ = get_version()
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "astrapy"
version = "0.7.2"
version = "0.7.2.post1"
description = "AstraPy is a Pythonic SDK for DataStax Astra"
authors = [
"Kirsten Hunter <kirsten.hunter@datastax.com>"
Expand Down

0 comments on commit 1c859cd

Please sign in to comment.