Skip to content

Commit 420d6c3

Browse files
committed
Improve annotations for json.load() and loads()
* loads() only accepts str as first argument for Python < 3.6 * Use a protocol for the first argument to load() (cf python/typing#564)
1 parent 80a0a75 commit 420d6c3

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

stdlib/3/json/__init__.pyi

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import sys
2-
from typing import Any, IO, Optional, Tuple, Callable, Dict, List, Union
2+
from typing import Any, IO, Optional, Tuple, Callable, Dict, List, Union, Protocol
33

44
from .decoder import JSONDecoder as JSONDecoder
55
from .encoder import JSONEncoder as JSONEncoder
@@ -31,7 +31,11 @@ def dump(obj: Any,
3131
sort_keys: bool = ...,
3232
**kwds: Any) -> None: ...
3333

34-
def loads(s: Union[str, bytes, bytearray],
34+
if sys.version_info >= (3, 6):
35+
_LoadsString = Union[str, bytes, bytearray]
36+
else:
37+
_LoadsString = str
38+
def loads(s: _LoadsString,
3539
encoding: Any = ..., # ignored and deprecated
3640
cls: Any = ...,
3741
object_hook: Optional[Callable[[Dict], Any]] = ...,
@@ -41,11 +45,10 @@ def loads(s: Union[str, bytes, bytearray],
4145
object_pairs_hook: Optional[Callable[[List[Tuple[Any, Any]]], Any]] = ...,
4246
**kwds: Any) -> Any: ...
4347

44-
if sys.version_info >= (3, 6):
45-
_LoadIO = IO[Any]
46-
else:
47-
_LoadIO = IO[str]
48-
def load(fp: _LoadIO,
48+
class _Reader(Protocol):
49+
def read(self) -> _LoadsString: ...
50+
51+
def load(fp: _Reader,
4952
cls: Any = ...,
5053
object_hook: Optional[Callable[[Dict], Any]] = ...,
5154
parse_float: Optional[Callable[[str], Any]] = ...,

0 commit comments

Comments
 (0)