Skip to content

plistlib.dumps() raises TypeError when skipkeys=True and dict has mixed key types #145856

Description

@stefanzetzsche

Issue

The documentation for dump() states:

When sort_keys is true (the default) the keys for dictionaries will be
written to the plist in sorted order, otherwise they will be written in
the iteration order of the dictionary.

When skipkeys is false (the default) the function raises :exc:TypeError
when a key of a dictionary is not a string, otherwise such keys are skipped.

plistlib.dumps() with skipkeys=True and sort_keys=True (the default) currently raises TypeError when the dict contains both string and non-string keys. The expected behavior per the documentation is: non-string keys are skipped, and the remaining (string) keys are written in sorted order.

Reproducer

import plistlib

d = {1: 'a', 'z': 'b', 'a': 'c'}

plistlib.dumps(d, skipkeys=True)
# Raises: TypeError: '<' not supported between instances of 'str' and 'int'
# Expected: valid plist with {"a": "c", "z": "b"}

Both FMT_XML and FMT_BINARY formats raise the same error.

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Linked PRs

Activity

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