Skip to content

TypeError when pickling directly instantiated urllib.parse result objects #160761

Description

@austinaviv

Python Version

Python 3.15 / main

Operating System

All platforms

Description

When directly instantiating result objects from urllib.parse (SplitResult, ParseResult, DefragResult, or their bytes counterparts), pickling or calling __getstate__() raises a TypeError:

import urllib.parse, pickle
res = urllib.parse.SplitResult('http', 'example.com', '/', '', '')
pickle.dumps(res)

Output:

TypeError: 'NoneType' object is not subscriptable

This occurs because _ResultBase.__getstate__() assumes super().__getstate__() returns a 2-tuple:

def __getstate__(self):
    state = super().__getstate__()
    try:
        if state[1]['_keep_empty'] == _MISSING_AS_NONE_DEFAULT:
            del state[1]['_keep_empty']
            if state == (None, {}):
                state = None
    except LookupError:
        pass
    return state

When _keep_empty has not been set (for instance, when constructing the result class directly rather than via urlsplit() or urlparse()), object.__getstate__() returns None. state[1] then fails with TypeError: 'NoneType' object is not subscriptable, which is not handled by except LookupError:.

Checking if state is not None: before subscripting resolves the error.

Linked PRs

Activity

  1. added a commit that references this issue on Oct 27, 2026
  2. added
    type-bugAn unexpected behavior, bug, or error
    stdlibStandard Library Python modules in the Lib/ directory
    on Oct 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions