Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistency in json.JSONEncoder Description Regarding skipkeys Behavior #132021

Open
mouchen626 opened this issue Apr 2, 2025 · 3 comments
Open
Labels
docs Documentation in the Doc dir

Comments

@mouchen626
Copy link

mouchen626 commented Apr 2, 2025

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

@ericvsmith ericvsmith added the docs Documentation in the Doc dir label Apr 2, 2025
@ericvsmith
Copy link
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.

@mouchen626
Copy link
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.

@brianschubert
Copy link
Contributor

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

srinivasreddy added a commit to srinivasreddy/cpython that referenced this issue Apr 3, 2025
ericvsmith added a commit to srinivasreddy/cpython that referenced this issue Apr 7, 2025
ericvsmith added a commit to srinivasreddy/cpython that referenced this issue Apr 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir
Projects
Status: Todo
Development

No branches or pull requests

3 participants