Skip to content

[json] Require a LiteralString for indent argument - #16402

Open
srittau wants to merge 1 commit into
python:mainfrom
srittau:json-indent
Open

srittau wants to merge 1 commit into
python:mainfrom
srittau:json-indent

Conversation

@srittau

@srittau srittau commented Sep 16, 2026

Copy link
Copy Markdown
Collaborator

Cf. https://discuss.python.org/t/should-json-restrict-string-indentation-to-json-whitespace/109060

Edit: If you read through the discussion, there seems to be a lot of arguments based on a misunderstanding about how LiteralString works. Please keep that in mind.

@github-actions

Copy link
Copy Markdown
Contributor

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

@JelleZijlstra

Copy link
Copy Markdown
Member

Yeah, that discussion isn't great.

Note that mypy-primer doesn't tell you much here because mypy doesn't meaningfully support LiteralString.

@picnixz

picnixz commented Sep 17, 2026

Copy link
Copy Markdown
Member

misunderstanding about how LiteralString works

I want to understand what was misunderstood. Was it about the intent of LiteralString? because for me, LiteralString should work as explained in PEP-675 and if type checkers aren't able to follow the specs, it's not really the problem of the user (semantically it's even wrong to mandate that something being literal is passed if the quantity can be dynamically determined).

AFAIK, the following should be wrong right? (but AFAIU, not flagged by mypy because it's not implemented)

from typing import LiteralString

def f(n: int) -> str: ...
def g(s: LiteralString) -> None: ...

g(f(1))

The discussion on DPO, while not very great, still tells me that we shouldn't try breaking user's code. I would be annoyed if I needed to change call sites or adding cast() once mypy decides to implement LiteralString entirely.

@AlexWaygood

Copy link
Copy Markdown
Member

I've set off an ecosystem run for ty that cherry-picks this change into ty's vendored stubs, so we can see the ecosystem impact with a type checker that supports LiteralString: astral-sh/ruff#28664

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.

4 participants