Skip to content

_json: Type indent as str | None #14398

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

Merged
merged 3 commits into from
Jul 12, 2025
Merged

_json: Type indent as str | None #14398

merged 3 commits into from
Jul 12, 2025

Conversation

Levalicious
Copy link
Contributor

@Levalicious Levalicious commented Jul 11, 2025

Currently, indent is typed int | None.
As per the C code, I'm fairly certain this should be a str not an int. This matches the behavior in the python wrapper.

I think the C code actually doesn't allow it to be None either, but I haven't dug in that far.

This comment has been minimized.

Copy link
Member

@brianschubert brianschubert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can confirm that all uses in _json.c do seem to assume that indent is a string, and trying to use an int raises a TypeError at runtime. LGTM :-)

Can you also update the __new__ parameter?

@brianschubert
Copy link
Member

brianschubert commented Jul 11, 2025

I think the C code actually doesn't allow it to be None either

The C code has if (self->indent != Py_None) checks to handle the None case, so I think this is fine, unless I'm missing something.

Also as a bit of a historical note, it seems that indent was actually inert up until Python 3.13 (the code in _json.c just has some TODO comments, the value never seems to be used). So for older versions, it could in theory be set to be literally any object. I don't think that's necessarily worth reflecting in the stubs though.

@Levalicious
Copy link
Contributor Author

Levalicious commented Jul 11, 2025

Done! And thank you for the context!

This comment has been minimized.

Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@hauntsaninja hauntsaninja merged commit 9a0eaf8 into python:main Jul 12, 2025
64 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants