Skip to content

Add return types to JSON decoders #3005

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 1 commit 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
13 changes: 8 additions & 5 deletions stdlib/2/json.pyi
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from typing import Any, IO, Optional, Tuple, Callable, Dict, List, Union, Text, Protocol

# documentation for return type: https://docs.python.org/3.7/library/json.html#json-to-py-table
_LoadsReturnType = Union[Dict[Any,Any], List[Any], str, int, float, None]

class JSONDecodeError(ValueError):
def dumps(self, obj: Any) -> str: ...
def dump(self, obj: Any, fp: IO[str], *args: Any, **kwds: Any) -> None: ...
def loads(self, s: str) -> Any: ...
def load(self, fp: IO[str]) -> Any: ...
def loads(self, s: str) -> _LoadsReturnType: ...
def load(self, fp: IO[str]) -> _LoadsReturnType: ...

def dumps(obj: Any,
skipkeys: bool = ...,
Expand Down Expand Up @@ -41,7 +44,7 @@ def loads(s: Union[Text, bytes],
parse_int: Optional[Callable[[str], Any]] = ...,
parse_constant: Optional[Callable[[str], Any]] = ...,
object_pairs_hook: Optional[Callable[[List[Tuple[Any, Any]]], Any]] = ...,
**kwds: Any) -> Any: ...
**kwds: Any) -> _LoadsReturnType: ...

class _Reader(Protocol):
def read(self) -> Union[Text, bytes]: ...
Expand All @@ -54,7 +57,7 @@ def load(fp: _Reader,
parse_int: Optional[Callable[[str], Any]] = ...,
parse_constant: Optional[Callable[[str], Any]] = ...,
object_pairs_hook: Optional[Callable[[List[Tuple[Any, Any]]], Any]] = ...,
**kwds: Any) -> Any: ...
**kwds: Any) -> _LoadsReturnType: ...

class JSONDecoder(object):
def __init__(self,
Expand All @@ -65,7 +68,7 @@ class JSONDecoder(object):
parse_constant: Callable[[str], Any] = ...,
strict: bool = ...,
object_pairs_hook: Callable[..., Any] = ...) -> None: ...
def decode(self, s: Union[Text, bytes], _w: Any = ...) -> Any: ...
def decode(self, s: Union[Text, bytes], _w: Any = ...) -> _LoadsReturnType: ...
def raw_decode(self, s: Union[Text, bytes], idx: int = ...) -> Tuple[Any, Any]: ...

class JSONEncoder(object):
Expand Down
6 changes: 3 additions & 3 deletions stdlib/3/json/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sys
from typing import Any, IO, Optional, Tuple, Callable, Dict, List, Union, Protocol

from .decoder import JSONDecoder as JSONDecoder
from .decoder import JSONDecoder as JSONDecoder, _LoadsReturnType
from .encoder import JSONEncoder as JSONEncoder
if sys.version_info >= (3, 5):
from .decoder import JSONDecodeError as JSONDecodeError
Expand Down Expand Up @@ -43,7 +43,7 @@ def loads(s: _LoadsString,
parse_int: Optional[Callable[[str], Any]] = ...,
parse_constant: Optional[Callable[[str], Any]] = ...,
object_pairs_hook: Optional[Callable[[List[Tuple[Any, Any]]], Any]] = ...,
**kwds: Any) -> Any: ...
**kwds: Any) -> _LoadsReturnType: ...

class _Reader(Protocol):
def read(self) -> _LoadsString: ...
Expand All @@ -55,4 +55,4 @@ def load(fp: _Reader,
parse_int: Optional[Callable[[str], Any]] = ...,
parse_constant: Optional[Callable[[str], Any]] = ...,
object_pairs_hook: Optional[Callable[[List[Tuple[Any, Any]]], Any]] = ...,
**kwds: Any) -> Any: ...
**kwds: Any) -> _LoadsReturnType: ...
7 changes: 5 additions & 2 deletions stdlib/3/json/decoder.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sys
from typing import Any, Callable, Dict, List, Optional, Tuple
from typing import Any, Callable, Dict, List, Optional, Tuple, Union

if sys.version_info >= (3, 5):
class JSONDecodeError(ValueError):
Expand All @@ -10,6 +10,9 @@ if sys.version_info >= (3, 5):
colno: int
def __init__(self, msg: str, doc: str, pos: int) -> None: ...

# documentation for return type: https://docs.python.org/3.7/library/json.html#json-to-py-table
_LoadsReturnType = Union[Dict[Any,Any], List[Any], str, int, float, None]

class JSONDecoder:
object_hook: Callable[[Dict[str, Any]], Any]
parse_float: Callable[[str], Any]
Expand All @@ -24,5 +27,5 @@ class JSONDecoder:
parse_constant: Optional[Callable[[str], Any]] = ...,
strict: bool = ...,
object_pairs_hook: Optional[Callable[[List[Tuple[str, Any]]], Any]] = ...) -> None: ...
def decode(self, s: str) -> Any: ...
def decode(self, s: str) -> _LoadsReturnType: ...
def raw_decode(self, s: str, idx: int = ...) -> Tuple[Any, int]: ...