@@ -491,24 +491,31 @@ 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
499+ if (
500+ (port := self .explicit_port ) is not None
501+ and port == DEFAULT_PORTS .get (self ._scheme )
502+ or mask_password
501503 ):
502504 # port normalization - using None for default ports to remove from rendering
503505 # https://datatracker.ietf.org/doc/html/rfc3986.html#section-6.2.3
504506 host = self .host_subcomponent
505- netloc = make_netloc (self .raw_user , self .raw_password , host , None )
507+ netloc = make_netloc (
508+ self .raw_user , self .raw_password , host , None , mask_password = True
509+ )
506510 else :
507511 netloc = self ._netloc
508512 return unsplit_result (self ._scheme , netloc , path , self ._query , self ._fragment )
509513
514+ def __str__ (self ) -> str :
515+ return self ._make_string (mask_password = False )
516+
510517 def __repr__ (self ) -> str :
511- return f"{ self .__class__ .__name__ } ('{ str ( self )} ')"
518+ return f"{ self .__class__ .__name__ } ('{ self . _make_string ( mask_password = True )} ')"
512519
513520 def __bytes__ (self ) -> bytes :
514521 return str (self ).encode ("ascii" )
0 commit comments