Skip to content

Reject invalid replacement filenames in with_name - #266

Open
00200200 wants to merge 1 commit into
jaraco:mainfrom
00200200:codex/fix-with-name-validation
Open

00200200 wants to merge 1 commit into
jaraco:mainfrom
00200200:codex/fix-with-name-validation

Conversation

@00200200

@00200200 00200200 commented Oct 8, 2026

Copy link
Copy Markdown

Path('data/report.txt').with_name('../other.txt') currently returns
data/../other.txt, although replacing a filename should reject directory
components. Empty names, '.', and Windows drive components are also accepted.

Validate the replacement name through the configured path module before
constructing the result. This preserves POSIX backslashes as literal filename
characters and rejects both Windows separator forms and drive-qualified names.
with_stem inherits the validation through its existing call to with_name.

Refs #214. This change only covers replacement-name validation; it does not
claim complete pathlib compatibility or close the compatibility checklist.

Validation on macOS, Python 3.11.5:

  • New tests on unchanged main: 13 failed (missing ValueError), 3 passed.
  • Same focused tests after the fix: 16 passed.
  • Tests and module doctests: 151 passed, 8 skipped; one existing pytest fixture
    deprecation warning. Plugin autoload was disabled and filterwarnings cleared
    to run the functional suite without optional plugin dependencies.
  • Ruff format check and git diff --check passed.
  • Ruff check of the two changed Python files reports the same 21 diagnostics
    as unchanged main, with no new diagnostics. This is not a clean lint pass.

Focused command:
PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 python3 -m pytest tests/test_path.py -k 'with_name or with_stem' -q -o addopts= -o filterwarnings= --tb=short

Full functional command:
PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 python3 -m pytest tests/ path/ -q --doctest-modules -o addopts= -o filterwarnings= --tb=short

POSIX and Windows path parsing are covered via posixpath and ntpath. Native
Windows execution and other Python versions were not run.

AI disclosure: Implementation, tests, and this description were prepared with
OpenAI Codex assistance. The checks above were executed locally.

@Wkhanex Wkhanex left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replacement names with directory/drive components are rejected under both POSIX and Windows path modules, including Windows drive-relative names. Valid sibling filenames still keep the original directory.

Checked 3db027d99702 with standalone Python 3.12.14 checks (13 passing assertions). I haven't run the full upstream suite.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants