Skip to content

Commit

Permalink
MNT: Update version detection
Browse files Browse the repository at this point in the history
Try to avoid pulling in setuptools_scm for better import time by
directly checking for a pip file that indicates whether the install is
editable and only using the git-based version in that case; otherwise,
depend on the distribution metadata.
  • Loading branch information
dopplershift committed Jan 18, 2024
1 parent 27a6701 commit 0082df8
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions src/metpy/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,29 @@ def get_version():
"""Get MetPy's version.
Either get it from package metadata, or get it using version control information if
a development install.
an editable installation.
"""
from importlib.metadata import distribution, PackageNotFoundError

try:
from setuptools_scm import get_version
return get_version(root='../..', relative_to=__file__,
version_scheme='post-release')
except (ImportError, LookupError):
from importlib.metadata import PackageNotFoundError, version

try:
return version(__package__)
except PackageNotFoundError:
return 'Unknown'
dist = distribution(__package__)

# First see if we can find this file from pip to check for an editable install
if direct := dist.read_text('direct_url.json'):
import json

# Parse file and look for editable key
info = json.loads(direct)
if info.get('dir_info', {}).get('editable'):
import contextlib

Check warning on line 25 in src/metpy/_version.py

View check run for this annotation

Codecov / codecov/patch

src/metpy/_version.py#L25

Added line #L25 was not covered by tests

# If editable try to get version using setuptools_scm
with contextlib.suppress(ImportError, LookupError):
from setuptools_scm import get_version
return get_version(root='../..', relative_to=__file__,

Check warning on line 30 in src/metpy/_version.py

View check run for this annotation

Codecov / codecov/patch

src/metpy/_version.py#L28-L30

Added lines #L28 - L30 were not covered by tests
version_scheme='post-release')

# With any error or not an editable install, we use the version from the metadata
return dist.version
except PackageNotFoundError:
return 'Unknown'

Check warning on line 36 in src/metpy/_version.py

View check run for this annotation

Codecov / codecov/patch

src/metpy/_version.py#L35-L36

Added lines #L35 - L36 were not covered by tests

0 comments on commit 0082df8

Please sign in to comment.