with_suffix: reject empty suffix and fix the operator-precedence trap in the guard#1779
Open
HrachShah wants to merge 3 commits into
Open
with_suffix: reject empty suffix and fix the operator-precedence trap in the guard#1779HrachShah wants to merge 3 commits into
HrachShah wants to merge 3 commits into
Conversation
added 2 commits
July 1, 2026 03:50
…the guard The previous guard read ``if suffix and not suffix[0] == '.' or suffix == '.' or '/' in suffix`` wh
for more information, see https://pre-commit.ci
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1779 +/- ##
=======================================
Coverage 99.86% 99.86%
=======================================
Files 21 21
Lines 4310 4310
Branches 252 252
=======================================
Hits 4304 4304
Misses 4 4
Partials 2 2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Coverage Report for CI Build 28492218069Coverage remained the same at 98.74%Details
Uncovered ChangesNo uncovered changes found. Coverage RegressionsNo coverage regressions found. Coverage Stats
💛 - Coveralls |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What do these changes do?
Rewrite the guard in :meth:
yarl.URL.with_suffixso the operatorprecedence is no longer the load-bearing part of the check, and so
an empty suffix is rejected.
The current expression reads as
which Python parses as
so the implicit short-circuit and the duplicate-
.check bothdisappear into separate
orarms and an empty suffix is acceptedquietly.
The replacement is a single, parenthesised guard that means what
it says: reject empty, reject bare
., reject non-leading.,reject any
/. Equivalent for every input the existing testsuite covers, plus three new cases:
ValueErrorNone,123) rejected withTypeError.is still required for the suffix to be acceptedAre there changes in behavior for the user?
Yes — a previously-silent acceptance is now an error. Three
concrete behaviour changes:
URL("http://x/a").with_suffix("")now raisesValueErrorinstead of returning
URL("http://x/a").URL("http://x/a").with_suffix(None)andwith_suffix(123)now raise
TypeError(the old expression's truthiness checkaccepted anything that was not
"").test suite already covers (leading
., bare.,/insuffix); they are all still rejected for the same reason.
Is it a substantial burden for the maintainers to support this?
No. The change is confined to one method on
URL, the newbehaviour is documented in the test additions, and the fix is
the kind of input validation the API clearly intended from the
start. No API surface is added or removed; the only effect is
on inputs that were already misbehaving.
Related issue number
Fixes #1735.
Checklist
narrative doc change is needed; the public docstring already
says the suffix must be non-empty and must start with
.)CONTRIBUTORS.txt(N/A — there is noCONTRIBUTORS.txtinthis repo)
CHANGES/folder1735.bugfix.rstbugfixDrafted with Zo Bot (Claude Opus 4.7); reviewed by @HrachShah.