8000 Inconsistency in json.JSONEncoder Description Regarding skipkeys Behavior · Issue #132021 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

Inconsistency in json.JSONEncoder Description Regarding skipkeys Behavior #132021

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

Closed
mouchen626 opened this issue Apr 2, 2025 &m 8000 iddot; 3 comments
Closed
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
hugovk pushed a commit that referenced this issue Apr 8, 2025
Co-authored-by: Eric V. Smith <ericvsmith@users.noreply.github.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Apr 8, 2025
…ythonGH-132048)

(cherry picked from commit 403886c)

Co-authored-by: Srinivas Reddy Thatiparthy (తాటిపర్తి శ్రీనివాస్  రెడ్డి) <thatiparthysreenivas@gmail.com>
Co-authored-by: Eric V. Smith <ericvsmith@users.noreply.github.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Apr 8, 2025
…ythonGH-132048)

(cherry picked from commit 403886c)

Co-authored-by: Srinivas Reddy Thatiparthy (తాటిపర్తి శ్రీనివాస్  రెడ్డి) <thatiparthysreenivas@gmail.com>
Co-authored-by: Eric V. Smith <ericvsmith@users.noreply.github.com>
@hugovk hugovk closed this as completed Apr 8, 2025
hugovk pushed a commit that referenced this issue Apr 8, 2025
…GH-132048) (#132256)

gh-132021: Add bool type to the list of allowed JSON key types (GH-132048)
(cherry picked from commit 403886c)

Co-authored-by: Srinivas Reddy Thatiparthy (తాటిపర్తి శ్రీనివాస్  రెడ్డి) <thatiparthysreenivas@gmail.com>
Co-authored-by: Eric V. Smith <ericvsmith@users.noreply.github.com>
hugovk pushed a commit that referenced this issue Apr 8, 2025
…GH-132048) (#132255)

gh-132021: Add bool type to the list of allowed JSON key types (GH-132048)
(cherry picked from commit 403886c)

Co-authored-by: Srinivas Reddy Thatiparthy (తాటిపర్తి శ్రీనివాస్  రెడ్డి) <thatiparthysreenivas@gmail.com>
Co-authored-by: Eric V. Smith <ericvsmith@users.noreply.github.com>
seehwan pushed a commit to seehwan/cpython that referenced this issue Apr 16, 2025
…ython#132048)

Co-authored-by: Eric V. Smith <ericvsmith@users.noreply.github.com>
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

4 participants
0