Skip to content

Commit

Permalink
Bump importlib metadata dependency (#631)
Browse files Browse the repository at this point in the history
* Consolidate importlib metadata import

* Rely on importlib_metadata for Python 3.9 and earlier. Fixes #630.

* Bump minimum importlib-metadata to 4.6

* Apply suggestions from code review

* Update _importlib.py

---------

Co-authored-by: Henry Schreiner <[email protected]>
  • Loading branch information
jaraco and henryiii authored Jul 7, 2023
1 parent 6b884ae commit 870a9ff
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 31 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
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 >= 0.22; python_version < "3.8"',
'importlib-metadata >= 4.6; python_version < "3.10"', # Not required for 3.8+, but fixes a stdlib bug
'tomli >= 1.1.0; python_version < "3.11"',
]

Expand Down
14 changes: 14 additions & 0 deletions src/build/_importlib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import sys


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):
try:
import importlib_metadata as metadata
except ModuleNotFoundError:
from importlib import metadata
else:
from importlib import metadata

__all__ = ['metadata']
9 changes: 3 additions & 6 deletions src/build/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ def check_dependency(
"""
import packaging.requirements

if sys.version_info >= (3, 8):
import importlib.metadata as importlib_metadata
else:
import importlib_metadata
from ._importlib import metadata

req = packaging.requirements.Requirement(req_string)
normalised_req_string = str(req)
Expand All @@ -51,8 +48,8 @@ def check_dependency(
return

try:
dist = importlib_metadata.distribution(req.name)
except importlib_metadata.PackageNotFoundError:
dist = metadata.distribution(req.name)
except metadata.PackageNotFoundError:
# dependency is not installed in the environment.
yield (*ancestral_req_strings, normalised_req_string)
else:
Expand Down
14 changes: 4 additions & 10 deletions src/build/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,27 @@
from __future__ import annotations

import pathlib
import sys
import tempfile

import pyproject_hooks

from . import PathType, ProjectBuilder, RunnerType
from ._importlib import metadata
from .env import DefaultIsolatedEnv


if sys.version_info >= (3, 8):
import importlib.metadata as importlib_metadata
else:
import importlib_metadata


def _project_wheel_metadata(builder: ProjectBuilder) -> importlib_metadata.PackageMetadata:
def _project_wheel_metadata(builder: ProjectBuilder) -> metadata.PackageMetadata:
with tempfile.TemporaryDirectory() as tmpdir:
path = pathlib.Path(builder.metadata_path(tmpdir))
return importlib_metadata.PathDistribution(path).metadata
return metadata.PathDistribution(path).metadata


def project_wheel_metadata(
source_dir: PathType,
isolated: bool = True,
*,
runner: RunnerType = pyproject_hooks.quiet_subprocess_runner,
) -> importlib_metadata.PackageMetadata:
) -> metadata.PackageMetadata:
"""
Return the wheel metadata for a project.
Expand Down
2 changes: 1 addition & 1 deletion tests/constraints.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
importlib-metadata==0.22
importlib-metadata==4.6
packaging==19.0
pyproject_hooks==1.0
setuptools==42.0.0; python_version < "3.10"
Expand Down
21 changes: 8 additions & 13 deletions tests/test_projectbuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import copy
import logging
import os
import pathlib
import sys
import textwrap

Expand All @@ -12,13 +13,7 @@

import build


if sys.version_info >= (3, 8): # pragma: no cover
from importlib import metadata as importlib_metadata
else: # pragma: no cover
import importlib_metadata

import pathlib
from build import _importlib


build_open_owner = 'builtins'
Expand All @@ -30,7 +25,7 @@
}


class MockDistribution(importlib_metadata.Distribution):
class MockDistribution(_importlib.metadata.Distribution):
def locate_file(self, path): # pragma: no cover
return ''

Expand All @@ -48,7 +43,7 @@ def from_name(cls, name):
return CircularMockDistribution()
elif name == 'nested_circular_dep':
return NestedCircularMockDistribution()
raise importlib_metadata.PackageNotFoundError
raise _importlib.metadata.PackageNotFoundError


class ExtraMockDistribution(MockDistribution):
Expand Down Expand Up @@ -167,7 +162,7 @@ def read_text(self, filename):
],
)
def test_check_dependency(monkeypatch, requirement_string, expected):
monkeypatch.setattr(importlib_metadata, 'Distribution', MockDistribution)
monkeypatch.setattr(_importlib.metadata, 'Distribution', MockDistribution)
assert next(build.check_dependency(requirement_string), None) == expected


Expand Down Expand Up @@ -502,7 +497,7 @@ def dummy_runner(cmd, cwd=None, extra_environ=None):
def test_metadata_path_no_prepare(tmp_dir, package_test_no_prepare):
builder = build.ProjectBuilder(package_test_no_prepare)

metadata = importlib_metadata.PathDistribution(
metadata = _importlib.metadata.PathDistribution(
pathlib.Path(builder.metadata_path(tmp_dir)),
).metadata

Expand All @@ -513,7 +508,7 @@ def test_metadata_path_no_prepare(tmp_dir, package_test_no_prepare):
def test_metadata_path_with_prepare(tmp_dir, package_test_setuptools):
builder = build.ProjectBuilder(package_test_setuptools)

metadata = importlib_metadata.PathDistribution(
metadata = _importlib.metadata.PathDistribution(
pathlib.Path(builder.metadata_path(tmp_dir)),
).metadata

Expand All @@ -524,7 +519,7 @@ def test_metadata_path_with_prepare(tmp_dir, package_test_setuptools):
def test_metadata_path_legacy(tmp_dir, package_legacy):
builder = build.ProjectBuilder(package_legacy)

metadata = importlib_metadata.PathDistribution(
metadata = _importlib.metadata.PathDistribution(
pathlib.Path(builder.metadata_path(tmp_dir)),
).metadata

Expand Down
2 changes: 2 additions & 0 deletions tests/test_self_packaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
'src/build/__init__.py',
'src/build/__main__.py',
'src/build/_exceptions.py',
'src/build/_importlib.py',
'src/build/_util.py',
'src/build/env.py',
'src/build/py.typed',
Expand All @@ -31,6 +32,7 @@
'build/__init__.py',
'build/__main__.py',
'build/_exceptions.py',
'build/_importlib.py',
'build/_util.py',
'build/env.py',
'build/py.typed',
Expand Down

0 comments on commit 870a9ff

Please sign in to comment.