Skip to content

Commit

Permalink
fix: use importlib_metadata for Python < 3.10.2 (#693)
Browse files Browse the repository at this point in the history
* Use importlib_metadata for Python < 3.10.2

* Update tests

* Apply suggestions

* Trick mypy

* Fix import order to work with mypy
  • Loading branch information
GianlucaFicarelli committed Oct 27, 2023
1 parent 8b3155d commit e3081b6
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Expand Up @@ -36,7 +36,7 @@ dependencies = [
"pyproject_hooks",
# not actually a runtime dependency, only supplied as there is not "recommended dependency" support
'colorama; os_name == "nt"',
'importlib-metadata >= 4.6; python_version < "3.10"', # Not required for 3.8+, but fixes a stdlib bug
'importlib-metadata >= 4.6; python_full_version < "3.10.2"', # Not required for 3.8+, but fixes a stdlib bug
'tomli >= 1.1.0; python_version < "3.11"',
]

Expand Down
7 changes: 4 additions & 3 deletions src/build/_importlib.py
Expand Up @@ -3,12 +3,13 @@

if sys.version_info < (3, 8):
import importlib_metadata as metadata
elif sys.version_info < (3, 9, 10) or (3, 10, 0) <= sys.version_info < (3, 10, 2):
elif sys.version_info >= (3, 10, 2):
from importlib import metadata
else:
try:
import importlib_metadata as metadata
except ModuleNotFoundError:
# helps bootstrapping when dependencies aren't installed
from importlib import metadata
else:
from importlib import metadata

__all__ = ['metadata']
3 changes: 3 additions & 0 deletions tests/test_util.py
Expand Up @@ -14,6 +14,7 @@ def test_wheel_metadata(package_test_setuptools, isolated):

assert metadata['name'] == 'test-setuptools'
assert metadata['version'] == '1.0.0'
assert isinstance(metadata.json, dict)


@pytest.mark.network
Expand All @@ -26,6 +27,7 @@ def test_wheel_metadata_isolation(package_test_flit):

assert metadata['name'] == 'test_flit'
assert metadata['version'] == '1.0.0'
assert isinstance(metadata.json, dict)

with pytest.raises(
build.BuildBackendException,
Expand All @@ -42,3 +44,4 @@ def test_with_get_requires(package_test_metadata):
assert metadata['name'] == 'test-metadata'
assert str(metadata['version']) == '1.0.0'
assert metadata['summary'] == 'hello!'
assert isinstance(metadata.json, dict)

0 comments on commit e3081b6

Please sign in to comment.