Skip to content

Commit

Permalink
Consolidate importlib metadata import
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Jun 27, 2023
1 parent 0ab7b05 commit 7d7bee9
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 30 deletions.
10 changes: 10 additions & 0 deletions src/build/_importlib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import sys


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


__all__ = ['metadata']
10 changes: 3 additions & 7 deletions src/build/_util.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import annotations

import re
import sys

from collections.abc import Iterator, Set

Expand All @@ -25,10 +24,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 @@ -49,8 +45,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
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 7d7bee9

Please sign in to comment.