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

perf: use package metadata before scm #3363

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/metpy/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,30 @@
# Distributed under the terms of the BSD 3-Clause License.
# SPDX-License-Identifier: BSD-3-Clause
"""Tools for versioning."""
import os
from importlib.metadata import PackageNotFoundError, version

Check failure on line 6 in src/metpy/_version.py

View workflow job for this annotation

GitHub Actions / Flake8

[flake8] reported by reviewdog 🐶 I001 isort found an import in the wrong position Raw Output: ./src/metpy/_version.py:6:1: I001 isort found an import in the wrong position


def get_version():
"""Get MetPy's version.

Either get it from package metadata, or get it using version control information if
a development install.
If the package is installed (read: metpy is in site-packages), use package
metadata. If not, use what is provided by setuptools_scm and default to
package data if that also fails.
"""
# Inspect where this file's parent directory is located
moddirname = os.path.dirname(os.path.dirname(__file__))
# If we're in site-packages, try using package metadata
if moddirname.endswith('site-packages'):
try:
return version(__package__)
except PackageNotFoundError:
pass
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:
Expand Down
27 changes: 27 additions & 0 deletions tests/test_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright (c) 2024 MetPy Developers.
# Distributed under the terms of the BSD 3-Clause License.
# SPDX-License-Identifier: BSD-3-Clause
"""Test package version."""
import importlib
from importlib.metadata import PackageNotFoundError
from unittest.mock import patch

import metpy

Check failure on line 10 in tests/test_version.py

View workflow job for this annotation

GitHub Actions / Flake8

[flake8] reported by reviewdog 🐶 I003 isort expected 1 blank line in imports, found 0 Raw Output: ./tests/test_version.py:10:1: I003 isort expected 1 blank line in imports, found 0
def test_version():

Check failure on line 11 in tests/test_version.py

View workflow job for this annotation

GitHub Actions / Flake8

[flake8] reported by reviewdog 🐶 E302 expected 2 blank lines, found 1 Raw Output: ./tests/test_version.py:11:1: E302 expected 2 blank lines, found 1
"""Test that MetPy version is not None."""
assert metpy.__version__ is not None


def test_version_installed_with_importlib_metadata_failure():
"""Test that version works when importlib_metadata is not available."""
with patch('importlib.metadata.version', side_effect=PackageNotFoundError):
importlib.reload(metpy)
assert metpy.__version__ != 'Unknown'


def test_version_notinstalled():
"""Test that version works when not installed."""
with patch('os.path.dirname', return_value='/bogus/bogus/bogus'):
importlib.reload(metpy)
assert metpy.__version__ != 'Unknown'