Skip to content

Inconsistency in json.JSONEncoder Description Regarding skipkeys Behavior #132021

Description

@mouchen626

If skipkeys is false (the default), a TypeError will be raised when trying to encode keys that are not str, int, float or None. If skipkeys is true, such items are simply skipped.

However, boolean values (True and False) can be used as dictionary keys without raising a TypeError. I think this is a mistake.

json.JSONEncoder(skipkeys=False).encode({True:1})

Linked PRs

Activity

  1. ericvsmith commented on Apr 2, 2025

    @ericvsmith
    Member

    bools are ints, so the statement is technically true.

    >>> isinstance(True, int)
    True
    >>> isinstance(False, int)
    True

    Although maybe it makes sense to add bool to the list of types.

  2. mouchen626 commented on Apr 2, 2025

    @mouchen626
    Author

    Thank you, I understand. I noticed that the description of json.dump includes bool. It would be better to keep it consistent.

    skipkeys (bool) – If True, keys that are not of a basic type (str, int, float, bool, None) will be skipped instead of raising a TypeError. Default False.

  3. brianschubert commented on Apr 2, 2025

    @brianschubert
    Contributor

    Seems good - would you like to put up a PR?

  4. added a commit that references this issue on Apr 3, 2025
  5. added a commit that references this issue on Apr 8, 2025
  6. added 2 commits that reference this issue on Apr 8, 2025
  7. added 2 commits that reference this issue on Apr 8, 2025
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

    docsDocumentation in the Doc dir

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions