Skip to content

Add json types to _typeshed? #5910

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
srittau opened this issue Aug 11, 2021 · 7 comments
Closed

Add json types to _typeshed? #5910

srittau opened this issue Aug 11, 2021 · 7 comments

Comments

@srittau
Copy link
Collaborator

srittau commented Aug 11, 2021

As this is an issue that pops up quite often and would be useful in a few places in typeshed as well: Should we add json types to _typeshed? As recursive types aren't possible at the moment, I'd suggest something simple like this:

JsonObject = dict[str, Any]  # Any is Json
JsonArray = list[Any]  # Any is Json
Json = JsonObject | JsonArray | str | float | int | bool | None

This can be improved if recursive types become available and could be a useful placeholder if/when the stdlib gains a proper JSON type.

@srittau
Copy link
Collaborator Author

srittau commented Aug 11, 2021

Cf. python/typing#182.

srittau added a commit to srittau/typeshed that referenced this issue Aug 11, 2021
@JukkaL
Copy link
Contributor

JukkaL commented Aug 11, 2021

I wonder if we should have a third-party module on PyPI with only useful type definitions / aliases that can be more cleanly used in user code than importing from _typeshed. Unlike typing_extensions, these wouldn't introduce any new type system features -- it would all be just for convenience. And unlike typing_extensions, we could add arbitrary things not in typing.

@srittau
Copy link
Collaborator Author

srittau commented Aug 11, 2021

No one is forced to use, but we can use it in our stubs. And it's an improvement over the status quo of just returning Any. Even just using Json = Any is an improvement, in my opinion.

@srittau
Copy link
Collaborator Author

srittau commented Aug 11, 2021

@JukkaL That's actually a good idea. Just have a types-typeshed (or similar) module that can then be depended on by type checkers, third-party stubs and other packages.

@wbolster
Copy link
Contributor

just for reference/inspiration, there's a mindblowing and mypy-specific hack for ‘correct’ json type checking:

python/typing#182 (comment)

@CobaltCause
Copy link

Fwiw, the following appears to work as expected with Pyright currently:

JsonScalar = None | int | float | bool | str
JsonObject = dict[str, "JsonScalar | JsonList | JsonObject"]
JsonList = list["JsonScalar | JsonObject | JsonList"]
Json = JsonScalar | JsonList | JsonObject

I think this version allows all the possible cases and nothing else based on the conversion table here.

@hauntsaninja
Copy link
Collaborator

See also python/typing#182 (comment)

I think we can close this, since we shouldn't add anything to typeshed. I'd accept a PR adding JSON and JSON_ro from that message to https://github.com/hauntsaninja/useful_types/

@hauntsaninja hauntsaninja closed this as not planned Won't fix, can't repro, duplicate, stale Jun 15, 2024
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 a pull request may close this issue.

5 participants