Skip to content

Commit

Permalink
Improve performance of URL.with_name (#1320)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Oct 18, 2024
1 parent 78c1616 commit 0608fdd
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGES/1320.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improved performance of the :py:meth:`~yarl.URL.with_name` method -- by :user:`bdraco`.
8 changes: 4 additions & 4 deletions yarl/_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -1528,7 +1528,8 @@ def with_name(self, name: str) -> "URL":
if name in (".", ".."):
raise ValueError(". and .. values are forbidden")
parts = list(self.raw_parts)
if self._val.netloc:
scheme, netloc, _, _, _ = self._val
if netloc:
if len(parts) == 1:
parts.append(name)
else:
Expand All @@ -1538,9 +1539,8 @@ def with_name(self, name: str) -> "URL":
parts[-1] = name
if parts[0] == "/":
parts[0] = "" # replace leading '/'
return self._from_val(
self._val._replace(path="/".join(parts), query="", fragment="")
)
val = tuple.__new__(SplitResult, (scheme, netloc, "/".join(parts), "", ""))
return self._from_val(val)

def with_suffix(self, suffix: str) -> "URL":
"""Return a new URL with suffix (file extension of name) replaced.
Expand Down

0 comments on commit 0608fdd

Please sign in to comment.