From 80ec8c894c4928f0d2753b1693cffed69179b2da Mon Sep 17 00:00:00 2001 From: Tim Paine <3105306+timkpaine@users.noreply.github.com> Date: Sat, 21 Feb 2026 17:49:41 -0500 Subject: [PATCH] Small fix for nested patterns --- check_dist/_core.py | 16 ++++++++++++---- check_dist/tests/test_all.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/check_dist/_core.py b/check_dist/_core.py index a8bbe4c..04c000c 100644 --- a/check_dist/_core.py +++ b/check_dist/_core.py @@ -495,12 +495,20 @@ def check_present(files: list[str], patterns: list[str], dist_type: str) -> list return errors -def check_absent(files: list[str], patterns: list[str], dist_type: str) -> list[str]: - """Return error strings for any *patterns* found in *files*.""" +def check_absent(files: list[str], patterns: list[str], dist_type: str, *, present_patterns: list[str] | None = None) -> list[str]: + """Return error strings for any *patterns* found in *files*. + + When *present_patterns* is given, files nested inside a directory + that matches a present pattern are not flagged. This avoids false + positives like ``lerna/tests/fake_package/pyproject.toml`` being + flagged as unwanted when ``lerna`` is a required present pattern. + """ errors: list[str] = [] for pattern in patterns: translated = translate_extension(pattern) matching = [f for f in files if matches_pattern(f, pattern)] + if matching and present_patterns: + matching = [f for f in matching if not any(matches_pattern(f, pp) for pp in present_patterns)] if matching: msg = f"{dist_type}: unwanted pattern '{pattern}' matched: {', '.join(matching)}" if translated != pattern: @@ -745,7 +753,7 @@ def check_dist( messages.append(f" Warning: could not compare against VCS: {exc}") errors.extend(check_present(sdist_files, config["sdist"]["present"], "sdist")) - errors.extend(check_absent(sdist_files, config["sdist"]["absent"], "sdist")) + errors.extend(check_absent(sdist_files, config["sdist"]["absent"], "sdist", present_patterns=config["sdist"]["present"])) errors.extend(check_wrong_platform_extensions(sdist_files, "sdist")) # ── wheel checks ───────────────────────────────────────── @@ -757,7 +765,7 @@ def check_dist( messages.append(f" {f}") errors.extend(check_present(wheel_files, config["wheel"]["present"], "wheel")) - errors.extend(check_absent(wheel_files, config["wheel"]["absent"], "wheel")) + errors.extend(check_absent(wheel_files, config["wheel"]["absent"], "wheel", present_patterns=config["wheel"]["present"])) errors.extend(check_wrong_platform_extensions(wheel_files, "wheel")) finally: if pre_built is None: diff --git a/check_dist/tests/test_all.py b/check_dist/tests/test_all.py index b9ac68a..29c360e 100644 --- a/check_dist/tests/test_all.py +++ b/check_dist/tests/test_all.py @@ -205,6 +205,36 @@ def test_docs_directory(self): assert len(errors) == 1 assert "docs" in errors[0] + def test_nested_in_present_pattern_skipped(self): + """Files nested inside a 'present' dir are not flagged as absent.""" + files = [ + "lerna/__init__.py", + "lerna/test_utils/configs/missing_init_py/.gitignore", + "lerna/tests/fake_package/pyproject.toml", + "lerna/tests/fake_package2/pyproject.toml", + "pyproject.toml", # top-level: should still be caught + ] + errors = check_absent( + files, + [".gitignore", "pyproject.toml"], + "wheel", + present_patterns=["lerna"], + ) + assert len(errors) == 1 + assert "pyproject.toml" in errors[0] + # Only the top-level pyproject.toml, not the nested ones + assert "lerna/" not in errors[0] + + def test_present_patterns_none_flags_all(self): + """Without present_patterns, nested files are still flagged.""" + files = [ + "lerna/tests/fake_package/pyproject.toml", + "pyproject.toml", + ] + errors = check_absent(files, ["pyproject.toml"], "wheel") + assert len(errors) == 1 + assert "lerna/tests/fake_package/pyproject.toml" in errors[0] + # ── check_wrong_platform_extensions ──────────────────────────────────