From 3819a783ab6a2ba746f88783eb9670159ccfb726 Mon Sep 17 00:00:00 2001 From: Jeremy Volkman Date: Sun, 18 Oct 2026 14:36:37 +0000 Subject: [PATCH] fix(inspect): ignore non-package top-level dirs in sdist site_paths --- pycross/private/tools/inspect_package.py | 7 +++++++ tests/unit/inspect_package_test.py | 25 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/pycross/private/tools/inspect_package.py b/pycross/private/tools/inspect_package.py index 3b7b4937..4ab3fc3f 100644 --- a/pycross/private/tools/inspect_package.py +++ b/pycross/private/tools/inspect_package.py @@ -425,6 +425,13 @@ def read_file(path: str) -> Optional[str]: declared.add(name) return sorted(declared) + def _has_python(d: str) -> bool: + prefix = d + "/" + return any(f.startswith(prefix) and _extract_module_name(f.rsplit("/", 1)[1]) for f in all_files) + + # An sdist's top-level dirs are only a guess at what the built wheel installs; a dir with + # no Python anywhere below it (.github/, requirements/, licenses/) can't be an import package. + top_level_dirs = {d for d in top_level_dirs if d.isidentifier() and _has_python(d)} pkgs = _resolve_top_level_paths(all_files, top_level_dirs, read_file) for f in root_files: name = _extract_module_name(f) diff --git a/tests/unit/inspect_package_test.py b/tests/unit/inspect_package_test.py index 2bee2747..8d9974ec 100644 --- a/tests/unit/inspect_package_test.py +++ b/tests/unit/inspect_package_test.py @@ -258,6 +258,31 @@ def test_sdist_no_init_py(self): result = inspect_sdist(sdist_path) self.assertEqual(result["site_paths"], []) + def test_sdist_ignores_non_package_top_level_dirs(self): + """Top-level sdist directories without Python modules (.github/, requirements/) are ignored.""" + sdist_path = self._create_tarball_with_dirs( + "packaging-23.2.tar.gz", + { + "packaging-23.2/.github/workflows/x.yml": "name: ci\n", + "packaging-23.2/requirements/base.txt": "pytest\n", + "packaging-23.2/packaging/__init__.py": "", + }, + ) + result = inspect_sdist(sdist_path) + self.assertEqual(result["site_paths"], ["packaging"]) + + def test_sdist_namespace_keeps_data_files_when_python_present(self): + """Data files inside a namespace directory that also contains Python modules are kept.""" + sdist_path = self._create_tarball_with_dirs( + "ns-pkg-1.0.tar.gz", + { + "ns-pkg-1.0/myns/helper.py": "# module\n", + "ns-pkg-1.0/myns/py.typed": "", + }, + ) + result = inspect_sdist(sdist_path) + self.assertEqual(result["site_paths"], ["myns/helper.py", "myns/py.typed"]) + def test_sdist_top_level_paths_pth(self): sdist_path = self._create_tarball_with_dirs( "rerun_sdk-1.0.tar.gz",