Skip to content

Commit

Permalink
Improve with_path performance
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco committed Oct 17, 2024
1 parent fa83191 commit d243f4e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGES/1307.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improved performance of the :py:meth:`~yarl.URL.with_path` method -- by :user:`bdraco`.
7 changes: 5 additions & 2 deletions yarl/_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -1264,13 +1264,16 @@ def with_port(self, port: Union[int, None]) -> "URL":

def with_path(self, path: str, *, encoded: bool = False) -> "URL":
"""Return a new URL with path replaced."""
scheme, netloc, _, _, _ = self._val
if not encoded:
path = self._PATH_QUOTER(path)
if self._val.netloc:
if netloc:
path = self._normalize_path(path) if "." in path else path
if path and path[0] != "/":
path = f"/{path}"
return self._from_val(self._val._replace(path=path, query="", fragment=""))
return self._from_val(
tuple.__new__(SplitResult, (scheme, netloc, path, "", ""))
)

@classmethod
def _get_str_query_from_sequence_iterable(
Expand Down

0 comments on commit d243f4e

Please sign in to comment.