Skip to content

Commit

Permalink
Improve performance of URL.parent (#1321)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Oct 18, 2024
1 parent 443ba5c commit 8e445f4
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGES/1321.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improved performance of :attr:`~yarl.URL.parent` -- by :user:`bdraco`.
9 changes: 5 additions & 4 deletions yarl/_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -904,13 +904,14 @@ def parent(self) -> "URL":
fragment.
"""
path = self.raw_path
scheme, netloc, path, query, fragment = self._val
if not path or path == "/":
if self._val.fragment or self._val.query:
return self._from_val(self._val._replace(query="", fragment=""))
if fragment or query:
val = tuple.__new__(SplitResult, (scheme, netloc, path, "", ""))
return self._from_val(val)
return self
parts = path.split("/")
val = self._val._replace(path="/".join(parts[:-1]), query="", fragment="")
val = tuple.__new__(SplitResult, (scheme, netloc, "/".join(parts[:-1]), "", ""))
return self._from_val(val)

@cached_property
Expand Down

0 comments on commit 8e445f4

Please sign in to comment.