Skip to content

Commit 14c3759

Browse files
authored
Small cleanups to URL.join (#1416)
1 parent ee4611c commit 14c3759

File tree

1 file changed

+18
-28
lines changed

1 file changed

+18
-28
lines changed

yarl/_url.py

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,6 @@ def pre_encoded_url(url_str: str) -> "URL":
216216

217217
@rewrite_module
218218
class URL:
219-
220219
# Don't derive from str
221220
# follow pathlib.Path design
222221
# probably URL will not suffer from pathlib problems:
@@ -1338,31 +1337,18 @@ def join(self, url: "URL") -> "URL":
13381337
if type(url) is not URL:
13391338
raise TypeError("url should be URL")
13401339

1341-
orig_scheme = self._scheme
1342-
orig_path = self._path
1343-
orig_query = self._query
1344-
orig_fragment = self._fragment
1345-
join_netloc = url._netloc
1346-
join_path = url._path
1347-
join_query = url._query
1348-
join_fragment = url._fragment
1349-
scheme = url._scheme or orig_scheme
1350-
1351-
if scheme != orig_scheme or scheme not in USES_RELATIVE:
1340+
scheme = url._scheme or self._scheme
1341+
if scheme != self._scheme or scheme not in USES_RELATIVE:
13521342
return url
13531343

13541344
# scheme is in uses_authority as uses_authority is a superset of uses_relative
1355-
if join_netloc and scheme in USES_AUTHORITY:
1345+
if (join_netloc := url._netloc) and scheme in USES_AUTHORITY:
13561346
return self._from_parts(
1357-
scheme, join_netloc, join_path, join_query, join_fragment
1347+
scheme, join_netloc, url._path, url._query, url._fragment
13581348
)
13591349

1360-
fragment = join_fragment if join_path or join_fragment else orig_fragment
1361-
query = join_query if join_path or join_query else orig_query
1362-
1363-
if not join_path:
1364-
path = orig_path
1365-
else:
1350+
orig_path = self._path
1351+
if join_path := url._path:
13661352
if join_path[0] == "/":
13671353
path = join_path
13681354
elif not orig_path:
@@ -1379,15 +1365,19 @@ def join(self, url: "URL") -> "URL":
13791365
if orig_path[0] == "/":
13801366
path = path[1:]
13811367
path = normalize_path(path) if "." in path else path
1368+
else:
1369+
path = orig_path
13821370

1383-
url = object.__new__(URL)
1384-
url._scheme = scheme
1385-
url._netloc = self._netloc
1386-
url._path = path
1387-
url._query = query
1388-
url._fragment = fragment
1389-
url._cache = {}
1390-
return url
1371+
new_url = object.__new__(URL)
1372+
new_url._scheme = scheme
1373+
new_url._netloc = self._netloc
1374+
new_url._path = path
1375+
new_url._query = url._query if join_path or url._query else self._query
1376+
new_url._fragment = (
1377+
url._fragment if join_path or url._fragment else self._fragment
1378+
)
1379+
new_url._cache = {}
1380+
return new_url
13911381

13921382
def joinpath(self, *other: str, encoded: bool = False) -> "URL":
13931383
"""Return a new URL with the elements in other appended to the path."""

0 commit comments

Comments
 (0)