Skip to content

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

Closed

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

  1. stefanzetzsche commented on Mar 12, 2026

    @stefanzetzsche
    ContributorAuthor

    I believe this is an implementation error rather than a documentation issue. The fix is likely to reverse the order of operations: filter non-string keys first, then sort. Happy to file a PR if confirmed a bug.

  2. stefanzetzsche commented on Mar 12, 2026

    @stefanzetzsche
    ContributorAuthor

    For context, json has a similar issue: #69643.

  3. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Mar 12, 2026
  4. added a commit that references this issue on Mar 12, 2026
  5. added 3 commits that reference this issue on May 19, 2026
  6. added 3 commits that reference this issue on Sep 18, 2026
  7. added a commit that references this issue on Oct 10, 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