Repository: pypa/build
Pull request #401 (2021-11-02 18:07:36)

Only import importlib_metadata when needed
Similar to #395, this helps with bootstrap issues with python < 3.8.

layday: The `importlib_metadata` import was removed in #361 which is awaiting feedback.
jmroot: Ah, OK. Feel free to close then, unless the other PR is going to take a while (would be nice to get this in a release).
layday: I don't have an estimate, sorry.
layday: `test_pip_needs_upgrade_mac_os_11` needs fixing if you'd still like for this to be merged. 

Patch:
diff --git a/.github/dependabot.yml b/.github/dependabot.yml
new file mode 100644
index 0000000..1230149
--- /dev/null
+++ b/.github/dependabot.yml
@@ -0,0 +1,6 @@
+version: 2
+updates:
+  - package-ecosystem: "github-actions"
+    directory: "/"
+    schedule:
+      interval: "daily"
diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml
index fea1cfc..c6e9bfb 100644
--- a/.github/workflows/check.yml
+++ b/.github/workflows/check.yml
@@ -19,7 +19,7 @@ jobs:
       - uses: actions/checkout@v3
 
       - name: Setup Python 3.9
-        uses: actions/setup-python@v3
+        uses: actions/setup-python@v4
         with:
           python-version: 3.9
 
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index c9d0048..289e3d7 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -4,7 +4,7 @@ ci:
 
 repos:
   - repo: https://github.com/pre-commit/pre-commit-hooks
-    rev: v4.2.0
+    rev: v4.3.0
     hooks:
       - id: check-ast
       - id: check-builtin-literals
@@ -18,12 +18,12 @@ repos:
       - id: trailing-whitespace
       - id: double-quote-string-fixer
   - repo: https://github.com/asottile/pyupgrade
-    rev: v2.32.1
+    rev: v2.37.2
     hooks:
       - id: pyupgrade
         args: ["--py36-plus"]
   - repo: https://github.com/psf/black
-    rev: 22.3.0
+    rev: 22.6.0
     hooks:
       - id: black
   - repo: https://github.com/asottile/blacken-docs
@@ -32,7 +32,7 @@ repos:
       - id: blacken-docs
         additional_dependencies: [black==22.3.0]
   - repo: https://github.com/pre-commit/mirrors-prettier
-    rev: "v2.6.2"
+    rev: "v2.7.1"
     hooks:
       - id: prettier
   - repo: https://github.com/PyCQA/isort
@@ -40,7 +40,7 @@ repos:
     hooks:
       - id: isort
   - repo: https://github.com/asottile/setup-cfg-fmt
-    rev: v1.20.1
+    rev: v1.20.2
     hooks:
       - id: setup-cfg-fmt
   - repo: https://github.com/PyCQA/flake8
diff --git a/src/build/env.py b/src/build/env.py
index b9e8f62..101aadd 100644
--- a/src/build/env.py
+++ b/src/build/env.py
@@ -18,11 +18,6 @@ from typing import Callable, Collection, List, Optional, Tuple, Type
 import build
 
 
-if sys.version_info < (3, 8):
-    import importlib_metadata as metadata
-else:
-    from importlib import metadata
-
 try:
     import virtualenv
 except ModuleNotFoundError:
@@ -260,6 +255,11 @@ def _create_isolated_env_venv(path: str) -> Tuple[str, str]:
 
     import packaging.version
 
+    if sys.version_info < (3, 8):
+        import importlib_metadata as metadata
+    else:
+        from importlib import metadata
+
     symlinks = _fs_supports_symlink()
     try:
         venv.EnvBuilder(with_pip=True, symlinks=symlinks).create(path)


Test patch:
diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml
index cc293b8..91cdd82 100644
--- a/.github/workflows/test.yml
+++ b/.github/workflows/test.yml
@@ -53,7 +53,7 @@ jobs:
       - uses: actions/checkout@v3
 
       - name: Setup python for test ${{ matrix.py }}
-        uses: actions/setup-python@v3
+        uses: actions/setup-python@v4
         with:
           python-version: ${{ matrix.py }}
 
@@ -74,7 +74,7 @@ jobs:
         shell: python
 
       - name: Setup python for tox
-        uses: actions/setup-python@v3
+        uses: actions/setup-python@v4
         with:
           python-version: 3.9
 
@@ -96,7 +96,7 @@ jobs:
         run: mv ".tox/coverage.${BASE}.xml" .tox/coverage.xml
         shell: bash
 
-      - uses: codecov/codecov-action@v1
+      - uses: codecov/codecov-action@v3
         if: always()
         env:
           PYTHON: ${{ matrix.python }}
@@ -119,7 +119,7 @@ jobs:
       - uses: actions/checkout@v3
 
       - name: Setup Python 3.9
-        uses: actions/setup-python@v3
+        uses: actions/setup-python@v4
         with:
           python-version: 3.9
 
diff --git a/tests/test_env.py b/tests/test_env.py
index a1ee0cb..7f683c4 100644
--- a/tests/test_env.py
+++ b/tests/test_env.py
@@ -117,7 +117,11 @@ def test_isolated_env_log(mocker, caplog, package_test_flit):
         ('INFO', 'Installing packages in isolated environment... (something)'),
     ]
     if sys.version_info >= (3, 8):  # stacklevel
-        assert [(record.lineno) for record in caplog.records] == [frameinfo.lineno + 1, 107, 198]
+        assert [(record.lineno) for record in caplog.records] == [
+            frameinfo.lineno + 1,
+            frameinfo.lineno - 7,
+            frameinfo.lineno + 84,
+        ]
 
 
 @pytest.mark.isolated
@@ -139,7 +143,8 @@ def test_pip_needs_upgrade_mac_os_11(mocker, pip_version, arch):
     mocker.patch('platform.system', return_value='Darwin')
     mocker.patch('platform.machine', return_value=arch)
     mocker.patch('platform.mac_ver', return_value=('11.0', ('', '', ''), ''))
-    mocker.patch('build.env.metadata.distributions', return_value=(SimpleNamespace(version=pip_version),))
+    metadata_name = 'importlib_metadata' if sys.version_info < (3, 8) else 'importlib.metadata'
+    mocker.patch(metadata_name + '.distributions', return_value=(SimpleNamespace(version=pip_version),))
 
     min_version = Version('20.3' if arch == 'x86_64' else '21.0.1')
     with build.env.IsolatedEnvBuilder():