Repository: pypa/setuptools_scm
Pull request #815 (2023-03-13 13:09:09)

refactor/breaking: move file finders to private module


Patch:
diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index 155cb30..71e528a 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -7,14 +7,18 @@ breaking
 * remove legacy version parser api - config arg always required
 * turn Configuration into a dataclass
 * require configuration to always pass into helpers
+* hide file-finders implementation in private module
+* define own build backend to have setuptools work
 
 features
 --------
 
+* created a directory for the vcs-versioning package and added it to pypi
 * git: expect main as possible default branch
 * drop version_from_scm helper
 * trim down exposed public api
 * no longer self-call twice in setuptools
+* add support for version schemes by import
 * chores
 
   * migrate own metadata to pyproject.toml
diff --git a/pyproject.toml b/pyproject.toml
index f8cb696..c8a4baa 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -61,18 +61,18 @@ repository = "https://github.com/pypa/setuptools_scm/"
 use_scm_version = "setuptools_scm.integration:version_keyword"
 
 [project.entry-points."setuptools.file_finders"]
-setuptools_scm = "setuptools_scm.integration:find_files"
+setuptools_scm = "setuptools_scm._file_finders:find_files"
 
 [project.entry-points."setuptools.finalize_distribution_options"]
 setuptools_scm = "setuptools_scm.integration:infer_version"
 
 [project.entry-points."setuptools_scm.files_command"]
-".git" = "setuptools_scm.file_finder_git:git_find_files"
-".hg" = "setuptools_scm.file_finder_hg:hg_find_files"
+".git" = "setuptools_scm._file_finders.git:git_find_files"
+".hg" = "setuptools_scm._file_finders.hg:hg_find_files"
 
 [project.entry-points."setuptools_scm.files_command_fallback"]
-".git_archival.txt" = "setuptools_scm.file_finder_git:git_archive_find_files"
-".hg_archival.txt" = "setuptools_scm.file_finder_hg:hg_archive_find_files"
+".git_archival.txt" = "setuptools_scm._file_finders.git:git_archive_find_files"
+".hg_archival.txt" = "setuptools_scm._file_finders.hg:hg_archive_find_files"
 
 [project.entry-points."setuptools_scm.local_scheme"]
 dirty-tag = "setuptools_scm.version:get_local_dirty_tag"
diff --git a/src/setuptools_scm/_cli.py b/src/setuptools_scm/_cli.py
index 953c471..37bbc9a 100644
--- a/src/setuptools_scm/_cli.py
+++ b/src/setuptools_scm/_cli.py
@@ -6,8 +6,8 @@ import sys
 
 from setuptools_scm import _get_version
 from setuptools_scm import Configuration
+from setuptools_scm._file_finders import find_files
 from setuptools_scm.discover import walk_potential_roots
-from setuptools_scm.integration import find_files
 
 
 def main(args: list[str] | None = None) -> None:
diff --git a/src/setuptools_scm/file_finder.py b/src/setuptools_scm/_file_finders/__init__.py
similarity index 84%
rename from src/setuptools_scm/file_finder.py
rename to src/setuptools_scm/_file_finders/__init__.py
index 27a50d7..5a7a1c0 100644
--- a/src/setuptools_scm/file_finder.py
+++ b/src/setuptools_scm/_file_finders/__init__.py
@@ -1,11 +1,14 @@
 from __future__ import annotations
 
+import itertools
 import os
+from typing import Callable
 
 from typing_extensions import TypeGuard
 
-from . import _types as _t
-from ._trace import trace
+from setuptools_scm import _types as _t
+from setuptools_scm._entrypoints import iter_entry_points
+from setuptools_scm._trace import trace
 
 
 def scm_find_files(
@@ -84,3 +87,15 @@ def is_toplevel_acceptable(toplevel: str | None) -> TypeGuard[str]:
     trace(toplevel, ignored)
 
     return toplevel not in ignored
+
+
+def find_files(path: _t.PathT = "") -> list[str]:
+    for ep in itertools.chain(
+        iter_entry_points("setuptools_scm.files_command"),
+        iter_entry_points("setuptools_scm.files_command_fallback"),
+    ):
+        command: Callable[[_t.PathT], list[str]] = ep.load()
+        res: list[str] = command(path)
+        if res:
+            return res
+    return []
diff --git a/src/setuptools_scm/file_finder_git.py b/src/setuptools_scm/_file_finders/git.py
similarity index 94%
rename from src/setuptools_scm/file_finder_git.py
rename to src/setuptools_scm/_file_finders/git.py
index 20c04a1..f007d5b 100644
--- a/src/setuptools_scm/file_finder_git.py
+++ b/src/setuptools_scm/_file_finders/git.py
@@ -6,12 +6,12 @@ import subprocess
 import tarfile
 from typing import IO
 
-from . import _types as _t
-from ._run_cmd import run as _run
-from ._trace import trace
-from .file_finder import is_toplevel_acceptable
-from .file_finder import scm_find_files
-from .utils import data_from_mime
+from . import is_toplevel_acceptable
+from . import scm_find_files
+from .. import _types as _t
+from .._run_cmd import run as _run
+from .._trace import trace
+from ..utils import data_from_mime
 
 log = logging.getLogger(__name__)
 
diff --git a/src/setuptools_scm/file_finder_hg.py b/src/setuptools_scm/_file_finders/hg.py
similarity index 84%
rename from src/setuptools_scm/file_finder_hg.py
rename to src/setuptools_scm/_file_finders/hg.py
index a02bcce..8f777ea 100644
--- a/src/setuptools_scm/file_finder_hg.py
+++ b/src/setuptools_scm/_file_finders/hg.py
@@ -3,13 +3,12 @@ from __future__ import annotations
 import os
 import subprocess
 
-from . import _types as _t
-from ._run_cmd import run as _run
-from ._trace import trace
-from .file_finder import is_toplevel_acceptable
-from .file_finder import scm_find_files
-from .utils import data_from_mime
-from .utils import do_ex
+from .. import _types as _t
+from .._file_finders import is_toplevel_acceptable
+from .._file_finders import scm_find_files
+from .._run_cmd import run as _run
+from .._trace import trace
+from ..utils import data_from_mime
 
 
 def _hg_toplevel(path: str) -> str | None:
@@ -31,10 +30,10 @@ def _hg_toplevel(path: str) -> str | None:
 def _hg_ls_files_and_dirs(toplevel: str) -> tuple[set[str], set[str]]:
     hg_files: set[str] = set()
     hg_dirs = {toplevel}
-    out, err, ret = do_ex(["hg", "files"], cwd=toplevel)
-    if ret:
+    res = _run(["hg", "files"], cwd=toplevel)
+    if res.returncode:
         (), ()
-    for name in out.splitlines():
+    for name in res.stdout.splitlines():
         name = os.path.normcase(name).replace("/", os.path.sep)
         fullname = os.path.join(toplevel, name)
         hg_files.add(fullname)
diff --git a/src/setuptools_scm/integration.py b/src/setuptools_scm/integration.py
index e2a033e..1d600e2 100644
--- a/src/setuptools_scm/integration.py
+++ b/src/setuptools_scm/integration.py
@@ -1,6 +1,5 @@
 from __future__ import annotations
 
-import itertools
 import os
 import warnings
 from typing import Any
@@ -12,7 +11,6 @@ import setuptools
 from . import _get_version
 from . import _version_missing
 from . import Configuration
-from ._entrypoints import iter_entry_points
 from ._integration.setuptools import (
     read_dist_name_from_setup_cfg as _read_dist_name_from_setup_cfg,
 )
@@ -20,7 +18,7 @@ from ._trace import trace
 from ._version_cls import _validate_version_cls
 
 if TYPE_CHECKING:
-    from . import _types as _t
+    pass
 
 
 def _warn_on_old_setuptools(_version: str = setuptools.__version__) -> None:
@@ -99,18 +97,6 @@ def version_keyword(
     _assign_version(dist, config)
 
 
-def find_files(path: _t.PathT = "") -> list[str]:
-    for ep in itertools.chain(
-        iter_entry_points("setuptools_scm.files_command"),
-        iter_entry_points("setuptools_scm.files_command_fallback"),
-    ):
-        command = ep.load()
-        res: list[str] = command(path)
-        if res:
-            return res
-    return []
-
-
 def infer_version(dist: setuptools.Distribution) -> None:
     trace(
         "finalize hook",


Test patch:
diff --git a/testing/test_file_finder.py b/testing/test_file_finder.py
index 6c7f2d8..3825500 100644
--- a/testing/test_file_finder.py
+++ b/testing/test_file_finder.py
@@ -8,7 +8,7 @@ from typing import Iterable
 import pytest
 
 from .wd_wrapper import WorkDir
-from setuptools_scm.integration import find_files
+from setuptools_scm._file_finders import find_files
 
 
 @pytest.fixture(params=["git", "hg"])
diff --git a/testing/test_git.py b/testing/test_git.py
index 13a8fec..3e1ee67 100644
--- a/testing/test_git.py
+++ b/testing/test_git.py
@@ -15,14 +15,14 @@ from unittest.mock import patch
 
 import pytest
 
+import setuptools_scm._file_finders
 from .conftest import DebugMode
 from .wd_wrapper import WorkDir
 from setuptools_scm import Configuration
 from setuptools_scm import git
-from setuptools_scm import integration
 from setuptools_scm import NonNormalizedVersion
+from setuptools_scm._file_finders.git import git_find_files
 from setuptools_scm._run_cmd import run
-from setuptools_scm.file_finder_git import git_find_files
 from setuptools_scm.git import archival_to_version
 from setuptools_scm.utils import has_command
 from setuptools_scm.version import format_version
@@ -327,7 +327,7 @@ def test_find_files_stop_at_root_git(wd: WorkDir) -> None:
     project = wd.cwd / "project"
     project.mkdir()
     project.joinpath("setup.cfg").touch()
-    assert integration.find_files(str(project)) == []
+    assert setuptools_scm._file_finders.find_files(str(project)) == []
 
 
 @pytest.mark.issue(128)
@@ -356,7 +356,7 @@ def test_git_archive_export_ignore(
     wd("git add test1.txt test2.txt")
     wd.commit()
     monkeypatch.chdir(wd.cwd)
-    assert integration.find_files(".") == [opj(".", "test1.txt")]
+    assert setuptools_scm._file_finders.find_files(".") == [opj(".", "test1.txt")]
 
 
 @pytest.mark.issue(228)
@@ -366,7 +366,9 @@ def test_git_archive_subdirectory(wd: WorkDir, monkeypatch: pytest.MonkeyPatch)
     wd("git add foobar")
     wd.commit()
     monkeypatch.chdir(wd.cwd)
-    assert integration.find_files(".") == [opj(".", "foobar", "test1.txt")]
+    assert setuptools_scm._file_finders.find_files(".") == [
+        opj(".", "foobar", "test1.txt")
+    ]
 
 
 @pytest.mark.issue(251)
@@ -378,7 +380,7 @@ def test_git_archive_run_from_subdirectory(
     wd("git add foobar")
     wd.commit()
     monkeypatch.chdir(wd.cwd / "foobar")
-    assert integration.find_files(".") == [opj(".", "test1.txt")]
+    assert setuptools_scm._file_finders.find_files(".") == [opj(".", "test1.txt")]
 
 
 @pytest.mark.issue("https://github.com/pypa/setuptools_scm/issues/728")
diff --git a/testing/test_mercurial.py b/testing/test_mercurial.py
index 144e423..7de7ca2 100644
--- a/testing/test_mercurial.py
+++ b/testing/test_mercurial.py
@@ -5,8 +5,8 @@ from pathlib import Path
 
 import pytest
 
+import setuptools_scm._file_finders
 from setuptools_scm import Configuration
-from setuptools_scm import integration
 from setuptools_scm.hg import archival_to_version
 from setuptools_scm.hg import parse
 from setuptools_scm.utils import has_command
@@ -67,11 +67,11 @@ def test_find_files_stop_at_root_hg(
     project.mkdir()
     project.joinpath("setup.cfg").touch()
     # setup.cfg has not been committed
-    assert integration.find_files(str(project)) == []
+    assert setuptools_scm._file_finders.find_files(str(project)) == []
     # issue 251
     wd.add_and_commit()
     monkeypatch.chdir(project)
-    assert integration.find_files() == ["setup.cfg"]
+    assert setuptools_scm._file_finders.find_files() == ["setup.cfg"]
 
 
 # XXX: better tests for tag prefixes