Skip to content

Commit

Permalink
Small cleanups to URL.join
Browse files Browse the repository at this point in the history
#1397 made this a less concise, clean it up a bit
  • Loading branch information
bdraco committed Nov 17, 2024
1 parent e910178 commit 5c571ae
Showing 1 changed file with 22 additions and 28 deletions.
50 changes: 22 additions & 28 deletions yarl/_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,6 @@ def pre_encoded_url(url_str: str) -> "URL":

@rewrite_module
class URL:

# Don't derive from str
# follow pathlib.Path design
# probably URL will not suffer from pathlib problems:
Expand Down Expand Up @@ -1131,10 +1130,12 @@ def with_path(self, path: str, *, encoded: bool = False) -> "URL":
return self._from_parts(self._scheme, netloc, path, "", "")

@overload
def with_query(self, query: Query) -> "URL": ...
def with_query(self, query: Query) -> "URL":
...

@overload
def with_query(self, **kwargs: QueryVariable) -> "URL": ...
def with_query(self, **kwargs: QueryVariable) -> "URL":
...

def with_query(self, *args: Any, **kwargs: Any) -> "URL":
"""Return a new URL with query part replaced.
Expand All @@ -1156,10 +1157,12 @@ def with_query(self, *args: Any, **kwargs: Any) -> "URL":
)

@overload
def extend_query(self, query: Query) -> "URL": ...
def extend_query(self, query: Query) -> "URL":
...

@overload
def extend_query(self, **kwargs: QueryVariable) -> "URL": ...
def extend_query(self, **kwargs: QueryVariable) -> "URL":
...

def extend_query(self, *args: Any, **kwargs: Any) -> "URL":
"""Return a new URL with query part combined with the existing.
Expand All @@ -1184,10 +1187,12 @@ def extend_query(self, *args: Any, **kwargs: Any) -> "URL":
)

@overload
def update_query(self, query: Query) -> "URL": ...
def update_query(self, query: Query) -> "URL":
...

@overload
def update_query(self, **kwargs: QueryVariable) -> "URL": ...
def update_query(self, **kwargs: QueryVariable) -> "URL":
...

def update_query(self, *args: Any, **kwargs: Any) -> "URL":
"""Return a new URL with query part updated.
Expand Down Expand Up @@ -1338,31 +1343,18 @@ def join(self, url: "URL") -> "URL":
if type(url) is not URL:
raise TypeError("url should be URL")

orig_scheme = self._scheme
orig_path = self._path
orig_query = self._query
orig_fragment = self._fragment
join_netloc = url._netloc
join_path = url._path
join_query = url._query
join_fragment = url._fragment
scheme = url._scheme or orig_scheme

if scheme != orig_scheme or scheme not in USES_RELATIVE:
scheme = url._scheme or self._scheme
if scheme != self._scheme or scheme not in USES_RELATIVE:
return url

# scheme is in uses_authority as uses_authority is a superset of uses_relative
if join_netloc and scheme in USES_AUTHORITY:
if (join_netloc := url._netloc) and scheme in USES_AUTHORITY:
return self._from_parts(
scheme, join_netloc, join_path, join_query, join_fragment
scheme, join_netloc, url._path, url._query, url._fragment
)

fragment = join_fragment if join_path or join_fragment else orig_fragment
query = join_query if join_path or join_query else orig_query

if not join_path:
path = orig_path
else:
orig_path = self._path
if join_path := url._path:
if join_path[0] == "/":
path = join_path
elif not orig_path:
Expand All @@ -1379,13 +1371,15 @@ def join(self, url: "URL") -> "URL":
if orig_path[0] == "/":
path = path[1:]
path = normalize_path(path) if "." in path else path
else:
path = orig_path

url = object.__new__(URL)
url._scheme = scheme
url._netloc = self._netloc
url._path = path
url._query = query
url._fragment = fragment
url._query = url._query if join_path or url._query else self._query
url._fragment = url._fragment if join_path or url._fragment else self._fragment
url._cache = {}
return url

Expand Down

0 comments on commit 5c571ae

Please sign in to comment.