Skip to content

Commit

Permalink
Merge pull request Unidata#3364 from dopplershift/version-finding
Browse files Browse the repository at this point in the history
Update version detection
  • Loading branch information
dopplershift authored Feb 21, 2024
2 parents 742e50d + 0082df8 commit 8ccabb7
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

# 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__,
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'

0 comments on commit 8ccabb7

Please sign in to comment.