Skip to content

gh-160761: Fix TypeError when pickling directly instantiated urllib.parse result objects - #160762

Open
austinaviv wants to merge 3 commits into
python:mainfrom
austinaviv:fix-urlparse-getstate-none
Open

austinaviv wants to merge 3 commits into
python:mainfrom
austinaviv:fix-urlparse-getstate-none

Conversation

@austinaviv

Copy link
Copy Markdown

Fixes gh-160761

When URL result objects (SplitResult, ParseResult, DefragResult, etc.) are instantiated directly rather than returned from urlsplit() or urlparse(), their _keep_empty attribute is not set.

In this case, super().__getstate__() returns None. _ResultBase.__getstate__() attempted to subscript state[1], causing TypeError: 'NoneType' object is not subscriptable.

This change checks if state is not None before accessing state[1], and adds tests verifying that all result types can be pickled when created directly.

@python-cla-bot

python-cla-bot Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

Comment thread Lib/urllib/parse.py Outdated
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Comment thread Lib/test/test_urlparse.py Outdated
Comment thread Lib/urllib/parse.py
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
@austinaviv

Copy link
Copy Markdown
Author

Thanks for the review, @picnixz!

I've addressed the feedback:

Applied if state is None: return None in urllib.parse to keep the diff minimal and the check explicit.
Removed the extra blank line in test_urlparse.py.

Please take another look when you get a chance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TypeError when pickling directly instantiated urllib.parse result objects

2 participants