Skip to content

Re-export ._replace as .replace in urllib.parse result types #136083

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Lib/test/test_urllib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1675,5 +1675,23 @@ def test_with_method_arg(self):
self.assertEqual(request.get_method(), 'HEAD')


class TestReplaceMethod:
"""Testcase to test the '.replace' method"""

def test_defrag(self):
result = urllib.parse.defrag("http://www.python.org")
result = result.replace(frament="something")
self.assertEqual(result.fragment, "something")

def test_urlsplit(self):
result = urllib.parse.urlsplit("http://www.python.org")
result = result.replace(frament="something")
self.assertEqual(result.fragment, "something")

def test_urlparse(self):
result = urllib.parse.urlparse("http://www.python.org")
result = result.replace(frament="something")
self.assertEqual(result.fragment, "something")

if __name__ == '__main__':
unittest.main()
5 changes: 5 additions & 0 deletions Lib/urllib/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,11 @@ def _hostinfo(self):
_ParseResultBase.query.__doc__ = _SplitResultBase.query.__doc__
_ParseResultBase.fragment.__doc__ = _SplitResultBase.fragment.__doc__

# Re-export these, since we don't have a 'replace' field
# and making the user call '._replace' feels yucky
_DefragResultBase.replace = _DefragResultBase._replace
_SplitResultBase.replace = _SplitResultBase._replace
_ParseResultBase.replace = _ParseResultBase._replace

# For backwards compatibility, alias _NetlocResultMixinStr
# ResultBase is no longer part of the documented API, but it is
Expand Down
Loading