Skip to content

Commit 8756b1d

Browse files
authored
Merge pull request #4 from python-project-templates/tkp/hf2
Small fix for nested patterns
2 parents c8b48e6 + 80ec8c8 commit 8756b1d

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

check_dist/_core.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -495,12 +495,20 @@ def check_present(files: list[str], patterns: list[str], dist_type: str) -> list
495495
return errors
496496

497497

498-
def check_absent(files: list[str], patterns: list[str], dist_type: str) -> list[str]:
499-
"""Return error strings for any *patterns* found in *files*."""
498+
def check_absent(files: list[str], patterns: list[str], dist_type: str, *, present_patterns: list[str] | None = None) -> list[str]:
499+
"""Return error strings for any *patterns* found in *files*.
500+
501+
When *present_patterns* is given, files nested inside a directory
502+
that matches a present pattern are not flagged. This avoids false
503+
positives like ``lerna/tests/fake_package/pyproject.toml`` being
504+
flagged as unwanted when ``lerna`` is a required present pattern.
505+
"""
500506
errors: list[str] = []
501507
for pattern in patterns:
502508
translated = translate_extension(pattern)
503509
matching = [f for f in files if matches_pattern(f, pattern)]
510+
if matching and present_patterns:
511+
matching = [f for f in matching if not any(matches_pattern(f, pp) for pp in present_patterns)]
504512
if matching:
505513
msg = f"{dist_type}: unwanted pattern '{pattern}' matched: {', '.join(matching)}"
506514
if translated != pattern:
@@ -745,7 +753,7 @@ def check_dist(
745753
messages.append(f" Warning: could not compare against VCS: {exc}")
746754

747755
errors.extend(check_present(sdist_files, config["sdist"]["present"], "sdist"))
748-
errors.extend(check_absent(sdist_files, config["sdist"]["absent"], "sdist"))
756+
errors.extend(check_absent(sdist_files, config["sdist"]["absent"], "sdist", present_patterns=config["sdist"]["present"]))
749757
errors.extend(check_wrong_platform_extensions(sdist_files, "sdist"))
750758

751759
# ── wheel checks ─────────────────────────────────────────
@@ -757,7 +765,7 @@ def check_dist(
757765
messages.append(f" {f}")
758766

759767
errors.extend(check_present(wheel_files, config["wheel"]["present"], "wheel"))
760-
errors.extend(check_absent(wheel_files, config["wheel"]["absent"], "wheel"))
768+
errors.extend(check_absent(wheel_files, config["wheel"]["absent"], "wheel", present_patterns=config["wheel"]["present"]))
761769
errors.extend(check_wrong_platform_extensions(wheel_files, "wheel"))
762770
finally:
763771
if pre_built is None:

check_dist/tests/test_all.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,36 @@ def test_docs_directory(self):
205205
assert len(errors) == 1
206206
assert "docs" in errors[0]
207207

208+
def test_nested_in_present_pattern_skipped(self):
209+
"""Files nested inside a 'present' dir are not flagged as absent."""
210+
files = [
211+
"lerna/__init__.py",
212+
"lerna/test_utils/configs/missing_init_py/.gitignore",
213+
"lerna/tests/fake_package/pyproject.toml",
214+
"lerna/tests/fake_package2/pyproject.toml",
215+
"pyproject.toml", # top-level: should still be caught
216+
]
217+
errors = check_absent(
218+
files,
219+
[".gitignore", "pyproject.toml"],
220+
"wheel",
221+
present_patterns=["lerna"],
222+
)
223+
assert len(errors) == 1
224+
assert "pyproject.toml" in errors[0]
225+
# Only the top-level pyproject.toml, not the nested ones
226+
assert "lerna/" not in errors[0]
227+
228+
def test_present_patterns_none_flags_all(self):
229+
"""Without present_patterns, nested files are still flagged."""
230+
files = [
231+
"lerna/tests/fake_package/pyproject.toml",
232+
"pyproject.toml",
233+
]
234+
errors = check_absent(files, ["pyproject.toml"], "wheel")
235+
assert len(errors) == 1
236+
assert "lerna/tests/fake_package/pyproject.toml" in errors[0]
237+
208238

209239
# ── check_wrong_platform_extensions ──────────────────────────────────
210240

0 commit comments

Comments
 (0)