From 899abf35492dc623984b29e1cd3b0b8bfbc0bdef Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Wed, 11 Aug 2021 11:38:31 +0200 Subject: [PATCH 1/2] Add Json types and use for json.load(s) Closes: #5910 --- stdlib/_typeshed/__init__.pyi | 4 ++++ stdlib/json/__init__.pyi | 30 ++++++++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/stdlib/_typeshed/__init__.pyi b/stdlib/_typeshed/__init__.pyi index cf67751f863b..0ae778d83d75 100644 --- a/stdlib/_typeshed/__init__.pyi +++ b/stdlib/_typeshed/__init__.pyi @@ -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 = JsonObject | JsonArray | str | float | int | bool | None diff --git a/stdlib/json/__init__.pyi b/stdlib/json/__init__.pyi index e37e68ca3b99..e48715015477 100644 --- a/stdlib/json/__init__.pyi +++ b/stdlib/json/__init__.pyi @@ -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 @@ -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: ... +@overload def loads( s: str | bytes, *, @@ -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], *, From 07db5f713a394ab56431da87b8cd24d3d344d52e Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Wed, 11 Aug 2021 11:45:05 +0200 Subject: [PATCH 2/2] Work around mypy & generic collections --- stdlib/_typeshed/__init__.pyi | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/stdlib/_typeshed/__init__.pyi b/stdlib/_typeshed/__init__.pyi index 0ae778d83d75..d65216079d57 100644 --- a/stdlib/_typeshed/__init__.pyi +++ b/stdlib/_typeshed/__init__.pyi @@ -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") @@ -168,6 +168,6 @@ else: class NoneType: def __bool__(self) -> Literal[False]: ... -JsonObject = dict[str, Any] # Any is Json -JsonArray = list[Any] # Any is Json -Json = JsonObject | JsonArray | str | float | int | bool | None +JsonObject = Dict[str, Any] # Any is Json +JsonArray = List[Any] # Any is Json +Json = Union[JsonObject, JsonArray, str, float, int, bool, None]