Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9c61453

Browse files
bdracowebknjaz
andauthored
Replace all Optional with Union (#1095)
Co-authored-by: Sviatoslav Sydorenko (Святослав Сидоренко) <[email protected]>
1 parent f8ba967 commit 9c61453

File tree

2 files changed

+29
-29
lines changed

2 files changed

+29
-29
lines changed

CHANGES/1095.contrib.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Replaced all ``Optional`` with ``Union`` -- by :user:`bdraco`.

yarl/_url.py

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
Iterable,
1313
Iterator,
1414
List,
15-
Optional,
1615
Tuple,
1716
TypedDict,
1817
TypeVar,
@@ -194,7 +193,7 @@ def __new__(
194193
val: Union[str, SplitResult, "URL"] = "",
195194
*,
196195
encoded: bool = False,
197-
strict: Optional[bool] = None,
196+
strict: Union[bool, None] = None,
198197
) -> Self:
199198
if strict is not None: # pragma: no cover
200199
warnings.warn("strict parameter is ignored")
@@ -211,7 +210,7 @@ def __new__(
211210
raise TypeError("Constructor parameter should be str")
212211

213212
if not encoded:
214-
host: Optional[str]
213+
host: Union[str, None]
215214
if not val[1]: # netloc
216215
netloc = ""
217216
host = ""
@@ -250,12 +249,12 @@ def build(
250249
*,
251250
scheme: str = "",
252251
authority: str = "",
253-
user: Optional[str] = None,
254-
password: Optional[str] = None,
252+
user: Union[str, None] = None,
253+
password: Union[str, None] = None,
255254
host: str = "",
256-
port: Optional[int] = None,
255+
port: Union[int, None] = None,
257256
path: str = "",
258-
query: Optional[_Query] = None,
257+
query: Union[_Query, None] = None,
259258
query_string: str = "",
260259
fragment: str = "",
261260
encoded: bool = False,
@@ -529,7 +528,7 @@ def authority(self) -> str:
529528
)
530529

531530
@property
532-
def raw_user(self) -> Optional[str]:
531+
def raw_user(self) -> Union[str, None]:
533532
"""Encoded user part of URL.
534533
535534
None if user is missing.
@@ -539,7 +538,7 @@ def raw_user(self) -> Optional[str]:
539538
return self._val.username or None
540539

541540
@cached_property
542-
def user(self) -> Optional[str]:
541+
def user(self) -> Union[str, None]:
543542
"""Decoded user part of URL.
544543
545544
None if user is missing.
@@ -551,7 +550,7 @@ def user(self) -> Optional[str]:
551550
return self._UNQUOTER(raw_user)
552551

553552
@property
554-
def raw_password(self) -> Optional[str]:
553+
def raw_password(self) -> Union[str, None]:
555554
"""Encoded password part of URL.
556555
557556
None if password is missing.
@@ -560,7 +559,7 @@ def raw_password(self) -> Optional[str]:
560559
return self._val.password
561560

562561
@cached_property
563-
def password(self) -> Optional[str]:
562+
def password(self) -> Union[str, None]:
564563
"""Decoded password part of URL.
565564
566565
None if password is missing.
@@ -572,7 +571,7 @@ def password(self) -> Optional[str]:
572571
return self._UNQUOTER(raw_password)
573572

574573
@cached_property
575-
def raw_host(self) -> Optional[str]:
574+
def raw_host(self) -> Union[str, None]:
576575
"""Encoded host part of URL.
577576
578577
None for relative URLs.
@@ -583,7 +582,7 @@ def raw_host(self) -> Optional[str]:
583582
return self._val.hostname
584583

585584
@cached_property
586-
def host(self) -> Optional[str]:
585+
def host(self) -> Union[str, None]:
587586
"""Decoded host part of URL.
588587
589588
None for relative URLs.
@@ -600,7 +599,7 @@ def host(self) -> Optional[str]:
600599
return _idna_decode(raw)
601600

602601
@cached_property
603-
def port(self) -> Optional[int]:
602+
def port(self) -> Union[int, None]:
604603
"""Port part of URL, with scheme-based fallback.
605604
606605
None for relative URLs or URLs without explicit port and
@@ -610,7 +609,7 @@ def port(self) -> Optional[int]:
610609
return self.explicit_port or self._default_port
611610

612611
@cached_property
613-
def explicit_port(self) -> Optional[int]:
612+
def explicit_port(self) -> Union[int, None]:
614613
"""Port part of URL, without scheme-based fallback.
615614
616615
None for relative URLs or URLs without explicit port.
@@ -893,10 +892,10 @@ def _encode_host(cls, host: str, human: bool = False) -> str:
893892
@classmethod
894893
def _make_netloc(
895894
cls,
896-
user: Optional[str],
897-
password: Optional[str],
898-
host: Optional[str],
899-
port: Optional[int],
895+
user: Union[str, None],
896+
password: Union[str, None],
897+
host: Union[str, None],
898+
port: Union[int, None],
900899
encode: bool = False,
901900
encode_host: bool = True,
902901
requote: bool = False,
@@ -934,7 +933,7 @@ def with_scheme(self, scheme: str) -> "URL":
934933
raise ValueError("scheme replacement is not allowed for relative URLs")
935934
return URL(self._val._replace(scheme=scheme.lower()), encoded=True)
936935

937-
def with_user(self, user: Optional[str]) -> "URL":
936+
def with_user(self, user: Union[str, None]) -> "URL":
938937
"""Return a new URL with user replaced.
939938
940939
Autoencode user if needed.
@@ -960,7 +959,7 @@ def with_user(self, user: Optional[str]) -> "URL":
960959
encoded=True,
961960
)
962961

963-
def with_password(self, password: Optional[str]) -> "URL":
962+
def with_password(self, password: Union[str, None]) -> "URL":
964963
"""Return a new URL with password replaced.
965964
966965
Autoencode password if needed.
@@ -1009,7 +1008,7 @@ def with_host(self, host: str) -> "URL":
10091008
encoded=True,
10101009
)
10111010

1012-
def with_port(self, port: Optional[int]) -> "URL":
1011+
def with_port(self, port: Union[int, None]) -> "URL":
10131012
"""Return a new URL with port replaced.
10141013
10151014
Clear port to default if None is passed.
@@ -1077,8 +1076,8 @@ def _query_var(v: _QueryVariable) -> str:
10771076
"of type {}".format(v, cls)
10781077
)
10791078

1080-
def _get_str_query(self, *args: Any, **kwargs: Any) -> Optional[str]:
1081-
query: Optional[Union[str, Mapping[str, _QueryVariable]]]
1079+
def _get_str_query(self, *args: Any, **kwargs: Any) -> Union[str, None]:
1080+
query: Union[str, Mapping[str, _QueryVariable], None]
10821081
if kwargs:
10831082
if len(args) > 0:
10841083
raise ValueError(
@@ -1163,7 +1162,7 @@ def update_query(self, *args: Any, **kwargs: Any) -> "URL":
11631162
self._val._replace(query=self._get_str_query(query) or ""), encoded=True
11641163
)
11651164

1166-
def with_fragment(self, fragment: Optional[str]) -> "URL":
1165+
def with_fragment(self, fragment: Union[str, None]) -> "URL":
11671166
"""Return a new URL with fragment replaced.
11681167
11691168
Autoencode fragment if needed.
@@ -1314,7 +1313,7 @@ def human_repr(self) -> str:
13141313
return urlunsplit(val)
13151314

13161315

1317-
def _human_quote(s: Optional[str], unsafe: str) -> Optional[str]:
1316+
def _human_quote(s: Union[str, None], unsafe: str) -> Union[str, None]:
13181317
if not s:
13191318
return s
13201319
for c in "%" + unsafe:
@@ -1372,9 +1371,9 @@ def cache_info() -> CacheInfo:
13721371
@rewrite_module
13731372
def cache_configure(
13741373
*,
1375-
idna_encode_size: Optional[int] = _MAXCACHE,
1376-
idna_decode_size: Optional[int] = _MAXCACHE,
1377-
ip_address_size: Optional[int] = _MAXCACHE,
1374+
idna_encode_size: Union[int, None] = _MAXCACHE,
1375+
idna_decode_size: Union[int, None] = _MAXCACHE,
1376+
ip_address_size: Union[int, None] = _MAXCACHE,
13781377
) -> None:
13791378
"""Configure LRU cache sizes."""
13801379
global _idna_decode, _idna_encode, _ip_compressed_version

0 commit comments

Comments
 (0)