Skip to content

Commit 5c56ba2

Browse files
authored
Check val.netloc instead of self.absolute internally (#1252)
1 parent 91c4d92 commit 5c56ba2

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

yarl/_url.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ def __init_subclass__(cls):
418418
def __str__(self) -> str:
419419
val = self._val
420420
scheme, netloc, path, query, fragment = val
421-
if not val.path and self.absolute and (val.query or val.fragment):
421+
if not val.path and val.netloc and (val.query or val.fragment):
422422
path = "/"
423423
if (port := self.explicit_port) is not None and port == self._default_port:
424424
# port normalization - using None for default ports to remove from rendering
@@ -454,11 +454,11 @@ def __eq__(self, other: object) -> bool:
454454
return NotImplemented
455455

456456
val1 = self._val
457-
if not val1.path and self.absolute:
457+
if not val1.path and val1.netloc:
458458
val1 = val1._replace(path="/")
459459

460460
val2 = other._val
461-
if not val2.path and other.absolute:
461+
if not val2.path and val2.netloc:
462462
val2 = val2._replace(path="/")
463463

464464
return val1 == val2
@@ -467,7 +467,7 @@ def __hash__(self) -> int:
467467
ret = self._cache.get("hash")
468468
if ret is None:
469469
val = self._val
470-
if not val.path and self.absolute:
470+
if not val.path and val.netloc:
471471
val = val._replace(path="/")
472472
ret = self._cache["hash"] = hash(val)
473473
return ret
@@ -501,9 +501,8 @@ def __mod__(self, query: Query) -> "URL":
501501
return self.update_query(query)
502502

503503
def __bool__(self) -> bool:
504-
return bool(
505-
self._val.netloc or self._val.path or self._val.query or self._val.fragment
506-
)
504+
val = self._val
505+
return bool(val.netloc or val.path or val.query or val.fragment)
507506

508507
def __getstate__(self) -> Tuple[SplitResult]:
509508
return (self._val,)
@@ -551,7 +550,7 @@ def is_default_port(self) -> bool:
551550
# If the explicit port is None, then the URL must be
552551
# using the default port unless its a relative URL
553552
# which does not have an implicit port / default port
554-
return self.absolute
553+
return self._val.netloc != ""
555554
return explicit == self._default_port
556555

557556
def origin(self) -> "URL":
@@ -588,7 +587,7 @@ def relative(self) -> "URL":
588587
scheme, user, password, host and port are removed.
589588
590589
"""
591-
if not self.absolute:
590+
if not self._val.netloc:
592591
raise ValueError("URL should be absolute")
593592
val = self._val._replace(scheme="", netloc="")
594593
return self._from_val(val)
@@ -762,7 +761,7 @@ def raw_path(self) -> str:
762761
763762
"""
764763
ret = self._val.path
765-
if not ret and self.absolute:
764+
if not ret and self._val.netloc:
766765
ret = "/"
767766
return ret
768767

@@ -859,7 +858,7 @@ def raw_parts(self) -> Tuple[str, ...]:
859858
860859
"""
861860
path = self._val.path
862-
if self.absolute:
861+
if self._val.netloc:
863862
return ("/", *path[1:].split("/")) if path else ("/",)
864863
if path and path[0] == "/":
865864
return ("/", *path[1:].split("/"))
@@ -893,7 +892,7 @@ def parent(self) -> "URL":
893892
def raw_name(self) -> str:
894893
"""The last part of raw_parts."""
895894
parts = self.raw_parts
896-
if self.absolute:
895+
if self._val.netloc:
897896
parts = parts[1:]
898897
if not parts:
899898
return ""
@@ -970,7 +969,7 @@ def _make_child(self, paths: "Sequence[str]", encoded: bool = False) -> "URL":
970969
old_path_cutoff = -1 if old_path_segments[-1] == "" else None
971970
parsed = [*old_path_segments[:old_path_cutoff], *parsed]
972971

973-
if self.absolute:
972+
if self._val.netloc:
974973
parsed = _normalize_path_segments(parsed) if needs_normalize else parsed
975974
if parsed and parsed[0] != "":
976975
# inject a leading slash when adding a path to an absolute URL
@@ -1148,7 +1147,7 @@ def with_scheme(self, scheme: str) -> "URL":
11481147
if not isinstance(scheme, str):
11491148
raise TypeError("Invalid scheme type")
11501149
lower_scheme = scheme.lower()
1151-
if not self.absolute and lower_scheme in SCHEME_REQUIRES_HOST:
1150+
if not self._val.netloc and lower_scheme in SCHEME_REQUIRES_HOST:
11521151
msg = (
11531152
"scheme replacement is not allowed for "
11541153
f"relative URLs for the {lower_scheme} scheme"
@@ -1173,7 +1172,7 @@ def with_user(self, user: Union[str, None]) -> "URL":
11731172
password = self.raw_password
11741173
else:
11751174
raise TypeError("Invalid user type")
1176-
if not self.absolute:
1175+
if not val.netloc:
11771176
raise ValueError("user replacement is not allowed for relative URLs")
11781177
encoded_host = self.host_subcomponent or ""
11791178
netloc = self._make_netloc(user, password, encoded_host, self.explicit_port)
@@ -1194,7 +1193,7 @@ def with_password(self, password: Union[str, None]) -> "URL":
11941193
password = self._QUOTER(password)
11951194
else:
11961195
raise TypeError("Invalid password type")
1197-
if not self.absolute:
1196+
if not self._val.netloc:
11981197
raise ValueError("password replacement is not allowed for relative URLs")
11991198
encoded_host = self.host_subcomponent or ""
12001199
port = self.explicit_port
@@ -1213,14 +1212,15 @@ def with_host(self, host: str) -> "URL":
12131212
# N.B. doesn't cleanup query/fragment
12141213
if not isinstance(host, str):
12151214
raise TypeError("Invalid host type")
1216-
if not self.absolute:
1215+
val = self._val
1216+
if not val.netloc:
12171217
raise ValueError("host replacement is not allowed for relative URLs")
12181218
if not host:
12191219
raise ValueError("host removing is not allowed")
12201220
encoded_host = self._encode_host(host) if host else ""
12211221
port = self.explicit_port
12221222
netloc = self._make_netloc(self.raw_user, self.raw_password, encoded_host, port)
1223-
return self._from_val(self._val._replace(netloc=netloc))
1223+
return self._from_val(val._replace(netloc=netloc))
12241224

12251225
def with_port(self, port: Union[int, None]) -> "URL":
12261226
"""Return a new URL with port replaced.
@@ -1234,9 +1234,9 @@ def with_port(self, port: Union[int, None]) -> "URL":
12341234
raise TypeError(f"port should be int or None, got {type(port)}")
12351235
if not (0 <= port <= 65535):
12361236
raise ValueError(f"port must be between 0 and 65535, got {port}")
1237-
if not self.absolute:
1238-
raise ValueError("port replacement is not allowed for relative URLs")
12391237
val = self._val
1238+
if not val.netloc:
1239+
raise ValueError("port replacement is not allowed for relative URLs")
12401240
encoded_host = self.host_subcomponent or ""
12411241
netloc = self._make_netloc(self.raw_user, self.raw_password, encoded_host, port)
12421242
return self._from_val(val._replace(netloc=netloc))
@@ -1245,7 +1245,7 @@ def with_path(self, path: str, *, encoded: bool = False) -> "URL":
12451245
"""Return a new URL with path replaced."""
12461246
if not encoded:
12471247
path = self._PATH_QUOTER(path)
1248-
if self.absolute:
1248+
if self._val.netloc:
12491249
path = self._normalize_path(path) if "." in path else path
12501250
if len(path) > 0 and path[0] != "/":
12511251
path = "/" + path
@@ -1470,7 +1470,7 @@ def with_name(self, name: str) -> "URL":
14701470
if name in (".", ".."):
14711471
raise ValueError(". and .. values are forbidden")
14721472
parts = list(self.raw_parts)
1473-
if self.absolute:
1473+
if self._val.netloc:
14741474
if len(parts) == 1:
14751475
parts.append(name)
14761476
else:

0 commit comments

Comments
 (0)