@@ -102,7 +102,6 @@ class _InternalURLCache(TypedDict, total=False):
102
102
absolute : bool
103
103
scheme : str
104
104
raw_authority : str
105
- _default_port : Union [int , None ]
106
105
authority : str
107
106
raw_user : Union [str , None ]
108
107
user : Union [str , None ]
@@ -444,7 +443,9 @@ def __str__(self) -> str:
444
443
path = "/"
445
444
else :
446
445
path = self ._path
447
- if (port := self .explicit_port ) is not None and port == self ._default_port :
446
+ if (port := self .explicit_port ) is not None and port == DEFAULT_PORTS .get (
447
+ self ._scheme
448
+ ):
448
449
# port normalization - using None for default ports to remove from rendering
449
450
# https://datatracker.ietf.org/doc/html/rfc3986.html#section-6.2.3
450
451
host = self .host_subcomponent
@@ -556,7 +557,7 @@ def is_default_port(self) -> bool:
556
557
# using the default port unless its a relative URL
557
558
# which does not have an implicit port / default port
558
559
return self ._netloc != ""
559
- return explicit == self ._default_port
560
+ return explicit == DEFAULT_PORTS . get ( self ._scheme )
560
561
561
562
def origin (self ) -> "URL" :
562
563
"""Return an URL with scheme, host and port parts only.
@@ -630,11 +631,6 @@ def raw_authority(self) -> str:
630
631
"""
631
632
return self ._netloc
632
633
633
- @cached_property
634
- def _default_port (self ) -> Union [int , None ]:
635
- """Default port for the scheme or None if not known."""
636
- return DEFAULT_PORTS .get (self ._scheme )
637
-
638
634
@cached_property
639
635
def authority (self ) -> str :
640
636
"""Decoded authority part of URL.
@@ -771,7 +767,7 @@ def host_port_subcomponent(self) -> Union[str, None]:
771
767
# To avoid string manipulation we only call rstrip if
772
768
# the last character is a dot.
773
769
raw = raw .rstrip ("." )
774
- if port is None or port == self ._default_port :
770
+ if port is None or port == DEFAULT_PORTS . get ( self ._scheme ) :
775
771
return f"[{ raw } ]" if ":" in raw else raw
776
772
return f"[{ raw } ]:{ port } " if ":" in raw else f"{ raw } :{ port } "
777
773
@@ -783,10 +779,9 @@ def port(self) -> Union[int, None]:
783
779
scheme without default port substitution.
784
780
785
781
"""
786
- port = self .explicit_port
787
- if port is None :
788
- port = self ._default_port
789
- return port
782
+ if (explicit_port := self .explicit_port ) is not None :
783
+ return explicit_port
784
+ return DEFAULT_PORTS .get (self ._scheme )
790
785
791
786
@cached_property
792
787
def explicit_port (self ) -> Union [int , None ]:
0 commit comments