Skip to content

Commit

Permalink
Improve performance of URL.with_scheme (#1322)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Oct 18, 2024
1 parent bcd9936 commit 78c1616
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGES/1322.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improved performance of the :py:meth:`~yarl.URL.with_scheme` method -- by :user:`bdraco`.
6 changes: 4 additions & 2 deletions yarl/_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -1172,13 +1172,15 @@ def with_scheme(self, scheme: str) -> "URL":
if not isinstance(scheme, str):
raise TypeError("Invalid scheme type")
lower_scheme = scheme.lower()
if not self._val.netloc and lower_scheme in SCHEME_REQUIRES_HOST:
_, netloc, path, query, fragment = self._val
if not netloc and lower_scheme in SCHEME_REQUIRES_HOST:
msg = (
"scheme replacement is not allowed for "
f"relative URLs for the {lower_scheme} scheme"
)
raise ValueError(msg)
return self._from_val(self._val._replace(scheme=lower_scheme))
val = tuple.__new__(SplitResult, (lower_scheme, netloc, path, query, fragment))
return self._from_val(val)

def with_user(self, user: Union[str, None]) -> "URL":
"""Return a new URL with user replaced.
Expand Down

0 comments on commit 78c1616

Please sign in to comment.