@@ -491,24 +491,30 @@ def build(
491491 def __init_subclass__ (cls ) -> NoReturn :
492492 raise TypeError (f"Inheriting a class { cls !r} from URL is forbidden" )
493493
494- def __str__ (self ) -> str :
494+ def _make_string (self , mask_password : bool = False ) -> str :
495495 if not self ._path and self ._netloc and (self ._query or self ._fragment ):
496496 path = "/"
497497 else :
498498 path = self ._path
499- if (port := self .explicit_port ) is not None and port == DEFAULT_PORTS .get (
500- self ._scheme
501- ):
499+ if (
500+ (port := self .explicit_port ) is not None
501+ and port == DEFAULT_PORTS .get (self ._scheme )
502+ ) or mask_password :
502503 # port normalization - using None for default ports to remove from rendering
503504 # https://datatracker.ietf.org/doc/html/rfc3986.html#section-6.2.3
504505 host = self .host_subcomponent
505- netloc = make_netloc (self .raw_user , self .raw_password , host , None )
506+ netloc = make_netloc (
507+ self .raw_user , self .raw_password , host , None , mask_password = True
508+ )
506509 else :
507510 netloc = self ._netloc
508511 return unsplit_result (self ._scheme , netloc , path , self ._query , self ._fragment )
509512
513+ def __str__ (self ) -> str :
514+ return self ._make_string (mask_password = False )
515+
510516 def __repr__ (self ) -> str :
511- return f"{ self .__class__ .__name__ } ('{ str ( self )} ')"
517+ return f"{ self .__class__ .__name__ } ('{ self . _make_string ( mask_password = True )} ')"
512518
513519 def __bytes__ (self ) -> bytes :
514520 return str (self ).encode ("ascii" )
0 commit comments