Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delay calling _package_version until __getattr__ time. #226

Merged
merged 1 commit into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions censusdis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,20 @@

Both Python and CLI interfaces are available.
"""
from typing import Any
from .impl.exceptions import CensusApiException
import importlib.metadata
from pathlib import Path


def _package_version() -> str:
"""Find the version of the package."""
"""Find the version of this package."""
package_version = "unknown"

try:
# Try to get the version of the current package if
# it is running from a distribution.
package = Path(__file__).parent.name
metadata = importlib.metadata.metadata(package)

package_version = metadata["Version"]
package_version = importlib.metadata.version("censusdis")
except importlib.metadata.PackageNotFoundError:
# Fall back on getting it from a local pyproject.toml.
# This works in a development environment where the
Expand All @@ -39,6 +37,12 @@ def _package_version() -> str:
return package_version


version = _package_version()
def __getattr__(name: str) -> Any:
"""Get package attributes."""
if name in ("version", "__version__"):
return _package_version()
else:
raise AttributeError(f"No attribute {name} in module {__name__}.")


__all__ = ("CensusApiException", "version")
__all__ = ("CensusApiException",)
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 = "censusdis"
version = "0.99.11"
version = "0.99.12"
description = "US Census utilities for a variety of data loading and mapping purposes."
license = "HL3-CL-ECO-EXTR-FFD-LAW-MIL-SV"
authors = ["Darren Vengroff"]
Expand Down