Skip to content

Commit 07798bf

Browse files
authored
Avoid creation of tuples to create new URL objects (#1398)
1 parent 3f2c26f commit 07798bf

File tree

2 files changed

+38
-36
lines changed

2 files changed

+38
-36
lines changed

CHANGES/1398.misc.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1396.misc.rst

yarl/_url.py

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -434,15 +434,16 @@ def _val(self) -> SplitURL:
434434
return (self._scheme, self._netloc, self._path, self._query, self._fragment)
435435

436436
@classmethod
437-
def _from_tup(cls, val: SplitURL) -> "URL":
438-
"""Create a new URL from a tuple.
439-
440-
The tuple should be in the form of a SplitURL.
441-
442-
(scheme, netloc, path, query, fragment)
443-
"""
437+
def _from_parts(
438+
cls, scheme: str, netloc: str, path: str, query: str, fragment: str
439+
) -> "URL":
440+
"""Create a new URL from parts."""
444441
self = object.__new__(cls)
445-
self._scheme, self._netloc, self._path, self._query, self._fragment = val
442+
self._scheme = scheme
443+
self._netloc = netloc
444+
self._path = path
445+
self._query = query
446+
self._fragment = fragment
446447
self._cache = {}
447448
return self
448449

@@ -592,7 +593,7 @@ def _origin(self) -> "URL":
592593
netloc = make_netloc(None, None, encoded_host, self.explicit_port)
593594
elif not self._path and not self._query and not self._fragment:
594595
return self
595-
return self._from_tup((scheme, netloc, "", "", ""))
596+
return self._from_parts(scheme, netloc, "", "", "")
596597

597598
def relative(self) -> "URL":
598599
"""Return a relative part of the URL.
@@ -602,7 +603,7 @@ def relative(self) -> "URL":
602603
"""
603604
if not self._netloc:
604605
raise ValueError("URL should be absolute")
605-
return self._from_tup(("", "", self._path, self._query, self._fragment))
606+
return self._from_parts("", "", self._path, self._query, self._fragment)
606607

607608
@cached_property
608609
def absolute(self) -> bool:
@@ -924,11 +925,11 @@ def parent(self) -> "URL":
924925
path = self._path
925926
if not path or path == "/":
926927
if self._fragment or self._query:
927-
return self._from_tup((self._scheme, self._netloc, path, "", ""))
928+
return self._from_parts(self._scheme, self._netloc, path, "", "")
928929
return self
929930
parts = path.split("/")
930-
return self._from_tup(
931-
(self._scheme, self._netloc, "/".join(parts[:-1]), "", "")
931+
return self._from_parts(
932+
self._scheme, self._netloc, "/".join(parts[:-1]), "", ""
932933
)
933934

934935
@cached_property
@@ -1012,7 +1013,7 @@ def _make_child(self, paths: "Sequence[str]", encoded: bool = False) -> "URL":
10121013

10131014
new_path = "/".join(parsed)
10141015

1015-
return self._from_tup((self._scheme, netloc, new_path, "", ""))
1016+
return self._from_parts(self._scheme, netloc, new_path, "", "")
10161017

10171018
def with_scheme(self, scheme: str) -> "URL":
10181019
"""Return a new URL with scheme replaced."""
@@ -1027,8 +1028,8 @@ def with_scheme(self, scheme: str) -> "URL":
10271028
f"relative URLs for the {lower_scheme} scheme"
10281029
)
10291030
raise ValueError(msg)
1030-
return self._from_tup(
1031-
(lower_scheme, netloc, self._path, self._query, self._fragment)
1031+
return self._from_parts(
1032+
lower_scheme, netloc, self._path, self._query, self._fragment
10321033
)
10331034

10341035
def with_user(self, user: Union[str, None]) -> "URL":
@@ -1051,8 +1052,8 @@ def with_user(self, user: Union[str, None]) -> "URL":
10511052
raise ValueError("user replacement is not allowed for relative URLs")
10521053
encoded_host = self.host_subcomponent or ""
10531054
netloc = make_netloc(user, password, encoded_host, self.explicit_port)
1054-
return self._from_tup(
1055-
(self._scheme, netloc, self._path, self._query, self._fragment)
1055+
return self._from_parts(
1056+
self._scheme, netloc, self._path, self._query, self._fragment
10561057
)
10571058

10581059
def with_password(self, password: Union[str, None]) -> "URL":
@@ -1075,8 +1076,8 @@ def with_password(self, password: Union[str, None]) -> "URL":
10751076
encoded_host = self.host_subcomponent or ""
10761077
port = self.explicit_port
10771078
netloc = make_netloc(self.raw_user, password, encoded_host, port)
1078-
return self._from_tup(
1079-
(self._scheme, netloc, self._path, self._query, self._fragment)
1079+
return self._from_parts(
1080+
self._scheme, netloc, self._path, self._query, self._fragment
10801081
)
10811082

10821083
def with_host(self, host: str) -> "URL":
@@ -1098,8 +1099,8 @@ def with_host(self, host: str) -> "URL":
10981099
encoded_host = _encode_host(host, validate_host=True) if host else ""
10991100
port = self.explicit_port
11001101
netloc = make_netloc(self.raw_user, self.raw_password, encoded_host, port)
1101-
return self._from_tup(
1102-
(self._scheme, netloc, self._path, self._query, self._fragment)
1102+
return self._from_parts(
1103+
self._scheme, netloc, self._path, self._query, self._fragment
11031104
)
11041105

11051106
def with_port(self, port: Union[int, None]) -> "URL":
@@ -1118,8 +1119,8 @@ def with_port(self, port: Union[int, None]) -> "URL":
11181119
raise ValueError("port replacement is not allowed for relative URLs")
11191120
encoded_host = self.host_subcomponent or ""
11201121
netloc = make_netloc(self.raw_user, self.raw_password, encoded_host, port)
1121-
return self._from_tup(
1122-
(self._scheme, netloc, self._path, self._query, self._fragment)
1122+
return self._from_parts(
1123+
self._scheme, netloc, self._path, self._query, self._fragment
11231124
)
11241125

11251126
def with_path(self, path: str, *, encoded: bool = False) -> "URL":
@@ -1131,7 +1132,7 @@ def with_path(self, path: str, *, encoded: bool = False) -> "URL":
11311132
path = normalize_path(path) if "." in path else path
11321133
if path and path[0] != "/":
11331134
path = f"/{path}"
1134-
return self._from_tup((self._scheme, netloc, path, "", ""))
1135+
return self._from_parts(self._scheme, netloc, path, "", "")
11351136

11361137
@overload
11371138
def with_query(self, query: Query) -> "URL": ...
@@ -1154,8 +1155,8 @@ def with_query(self, *args: Any, **kwargs: Any) -> "URL":
11541155
"""
11551156
# N.B. doesn't cleanup query/fragment
11561157
query = get_str_query(*args, **kwargs) or ""
1157-
return self._from_tup(
1158-
(self._scheme, self._netloc, self._path, query, self._fragment)
1158+
return self._from_parts(
1159+
self._scheme, self._netloc, self._path, query, self._fragment
11591160
)
11601161

11611162
@overload
@@ -1182,8 +1183,8 @@ def extend_query(self, *args: Any, **kwargs: Any) -> "URL":
11821183
query += new_query if query[-1] == "&" else f"&{new_query}"
11831184
else:
11841185
query = new_query
1185-
return self._from_tup(
1186-
(self._scheme, self._netloc, self._path, query, self._fragment)
1186+
return self._from_parts(
1187+
self._scheme, self._netloc, self._path, query, self._fragment
11871188
)
11881189

11891190
@overload
@@ -1241,8 +1242,8 @@ def update_query(self, *args: Any, **kwargs: Any) -> "URL":
12411242
"Invalid query type: only str, mapping or "
12421243
"sequence of (key, value) pairs is allowed"
12431244
)
1244-
return self._from_tup(
1245-
(self._scheme, self._netloc, self._path, query, self._fragment)
1245+
return self._from_parts(
1246+
self._scheme, self._netloc, self._path, query, self._fragment
12461247
)
12471248

12481249
def without_query_params(self, *query_params: str) -> "URL":
@@ -1275,8 +1276,8 @@ def with_fragment(self, fragment: Union[str, None]) -> "URL":
12751276
raw_fragment = FRAGMENT_QUOTER(fragment)
12761277
if self._fragment == raw_fragment:
12771278
return self
1278-
return self._from_tup(
1279-
(self._scheme, self._netloc, self._path, self._query, raw_fragment)
1279+
return self._from_parts(
1280+
self._scheme, self._netloc, self._path, self._query, raw_fragment
12801281
)
12811282

12821283
def with_name(self, name: str) -> "URL":
@@ -1307,7 +1308,7 @@ def with_name(self, name: str) -> "URL":
13071308
parts[-1] = name
13081309
if parts[0] == "/":
13091310
parts[0] = "" # replace leading '/'
1310-
return self._from_tup((scheme, netloc, "/".join(parts), "", ""))
1311+
return self._from_parts(scheme, netloc, "/".join(parts), "", "")
13111312

13121313
def with_suffix(self, suffix: str) -> "URL":
13131314
"""Return a new URL with suffix (file extension of name) replaced.
@@ -1357,8 +1358,8 @@ def join(self, url: "URL") -> "URL":
13571358

13581359
# scheme is in uses_authority as uses_authority is a superset of uses_relative
13591360
if join_netloc and scheme in USES_AUTHORITY:
1360-
return self._from_tup(
1361-
(scheme, join_netloc, join_path, join_query, join_fragment)
1361+
return self._from_parts(
1362+
scheme, join_netloc, join_path, join_query, join_fragment
13621363
)
13631364

13641365
fragment = join_fragment if join_path or join_fragment else orig_fragment

0 commit comments

Comments
 (0)