Skip to content

Add Json types and use for json.load(s) #5911

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
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion stdlib/_typeshed/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import array
import mmap
import sys
from os import PathLike
from typing import AbstractSet, Any, Container, Iterable, Protocol, Tuple, TypeVar, Union
from typing import AbstractSet, Any, Container, Dict, Iterable, List, Protocol, Tuple, TypeVar, Union
from typing_extensions import Literal, final

_KT = TypeVar("_KT")
Expand Down Expand Up @@ -167,3 +167,7 @@ else:
@final
class NoneType:
def __bool__(self) -> Literal[False]: ...

JsonObject = Dict[str, Any] # Any is Json
JsonArray = List[Any] # Any is Json
Json = Union[JsonObject, JsonArray, str, float, int, bool, None]
30 changes: 28 additions & 2 deletions stdlib/json/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from _typeshed import SupportsRead
from typing import IO, Any, Callable, Tuple, Type
from _typeshed import Json, SupportsRead
from typing import IO, Any, Callable, Tuple, Type, overload

from .decoder import JSONDecodeError as JSONDecodeError, JSONDecoder as JSONDecoder
from .encoder import JSONEncoder as JSONEncoder
Expand Down Expand Up @@ -33,6 +33,19 @@ def dump(
sort_keys: bool = ...,
**kwds: Any,
) -> None: ...
@overload
def loads(
s: str | bytes,
*,
cls: Type[JSONDecoder] | None = ...,
object_hook: None = ...,
parse_float: None = ...,
parse_int: None = ...,
parse_constant: None = ...,
object_pairs_hook: None = ...,
**kwds: Any,
) -> Json: ...
Copy link
Collaborator

@Akuli Akuli Aug 11, 2021

Choose a reason for hiding this comment

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

I don't like how this introduces an implicit union return and breaks code like json.loads(some_string)["Foo"]. It is more correct, but the usual reasons for avoiding union returns apply here IMO.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

True, another good reason for python/typing#566.

@overload
def loads(
s: str | bytes,
*,
Expand All @@ -44,6 +57,19 @@ def loads(
object_pairs_hook: Callable[[list[Tuple[Any, Any]]], Any] | None = ...,
**kwds: Any,
) -> Any: ...
@overload
def load(
fp: SupportsRead[str | bytes],
*,
cls: Type[JSONDecoder] | None = ...,
object_hook: None = ...,
parse_float: None = ...,
parse_int: None = ...,
parse_constant: None = ...,
object_pairs_hook: None = ...,
**kwds: Any,
) -> Any: ...
@overload
def load(
fp: SupportsRead[str | bytes],
*,
Expand Down