Skip to content
Open
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
1 change: 1 addition & 0 deletions changelog.d/version-from-metadata.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fixed `microimpute.__version__` reporting 1.1.2 while the package was at 3.1.1, by reading the version from installed package metadata.
9 changes: 8 additions & 1 deletion microimpute/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@
- Visualization: performance and comparison plots
"""

__version__ = "1.1.2"
from importlib.metadata import PackageNotFoundError, version as _version

try:
__version__ = _version("microimpute")
except PackageNotFoundError:
# Running from a source tree with no install. Keep it PEP 440 parseable so
# a consumer calling packaging.version.parse on it does not raise.
__version__ = "0.0.0+unknown"

# Import automated imputation
from microimpute.comparisons.autoimpute import AutoImputeResult, autoimpute
Expand Down
42 changes: 42 additions & 0 deletions tests/test_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""The reported version must match the installed distribution.

`__version__` was hardcoded and drifted to 1.1.2 while the package was 3.1.1,
which is the regression this guards against.
"""

from importlib.metadata import version

import pytest
from packaging.version import Version

import microimpute


def test_version_matches_installed_distribution():
assert microimpute.__version__ == version("microimpute")


def test_version_is_pep440_parseable():
"""The fallback must parse too, or consumers comparing versions raise."""
Version(microimpute.__version__)
Version("0.0.0+unknown")


def test_fallback_used_when_distribution_is_absent(monkeypatch):
"""The fallback branch runs when the distribution cannot be found."""
import importlib.metadata

def _raise(_name):
raise importlib.metadata.PackageNotFoundError(_name)

monkeypatch.setattr(importlib.metadata, "version", _raise)

# Re-run the same lookup __init__ performs, rather than reloading the
# package: reloading re-imports every model and is slow and fragile.
try:
resolved = importlib.metadata.version("microimpute")
except importlib.metadata.PackageNotFoundError:
resolved = "0.0.0+unknown"

assert resolved == "0.0.0+unknown"
Version(resolved)
Loading