-
-
Notifications
You must be signed in to change notification settings - Fork 30.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
urllib.parse.splituser has no suitable replacement #80072
Comments
The removal of splituser (bpo-27485) has the undesirable effect of leaving the programmer without a suitable alternative. The deprecation warning states to use Consider for example: >>> import urllib.parse
>>> url = 'https://user:password@host:port/path'
>>> parsed = urllib.parse.urlparse(url)
>>> urllib.parse.splituser(parsed.netloc)
('user:password', 'host:port') It's not readily obvious how one might get those two values, the credential and the address, from This recommendation and limitation led to issues in production code and ultimately the inline adoption of the deprecated function, summarized here. I believe if splituser is to be deprecated, the netloc should provide a suitable alternative - namely that a Even better would be for the urlparse result to support
I realize that because of the nesting of abstractions (namedtuple for the main parts), that maybe this technique doesn't extend nicely, so maybe the netloc itself should provide this extensibility for a usage something like this:
It's not as elegant, but likely simpler to implement, with netloc being extended with a _replace method to support replacing segments of itself (and still immutable)... and is dramatically less error-prone than the status quo without splituser. In any case, I don't think it's suitable to leave it to the programmer to have to muddle around with their own URL parsing logic. urllib.parse should provide some help here. |
class _NetlocResultMixinStr(...):
...
@property
def userinfo(self):
return ":".join([info for info in self._userinfo if info is not None])
@property
def hostinfo(self):
return ":".join([info for info in parsed._hostinfo if info is not None])
# or even
@property
def userinfo(self):
if self.username:
return self.username + (f":{self.password}" if self.password else "")
return None
@property
def hostinfo(self):
if self.hostname:
return self.hostname + ("" if self.port is None else f":{self.port}")
return None |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: