|
1 | 1 | from _typeshed import SupportsRead, SupportsWrite
|
2 |
| -from collections.abc import Callable, Mapping, Sequence |
3 |
| -from typing import Any, TypeVar, overload |
| 2 | +from collections.abc import Callable, Iterator, Mapping, Sequence |
| 3 | +from typing import Any, Protocol, TypeVar |
4 | 4 | from typing_extensions import TypeAlias
|
5 | 5 |
|
6 | 6 | from .decoder import JSONDecodeError as JSONDecodeError, JSONDecoder as JSONDecoder
|
7 | 7 | from .encoder import JSONEncoder as JSONEncoder
|
8 | 8 |
|
9 | 9 | __all__ = ["dump", "dumps", "load", "loads", "JSONDecoder", "JSONDecodeError", "JSONEncoder"]
|
10 | 10 |
|
11 |
| -_T = TypeVar("_T") |
12 |
| - |
13 | 11 | _JSON: TypeAlias = Mapping[str, _JSON] | Sequence[_JSON] | str | float | bool | None
|
| 12 | +_EncodableTypes = TypeVar("_EncodableTypes", default=_JSON) |
| 13 | +_DefaultTypes = TypeVar("_DefaultTypes", default=_JSON) |
| 14 | +_ClsDefaultTypes = TypeVar("_ClsDefaultTypes", default=_JSON) |
| 15 | + |
| 16 | +_EncodableTypes_co = TypeVar("_EncodableTypes_co", covariant=True, default=_JSON) |
| 17 | +_Encode_Fallback_contra = TypeVar("_Encode_Fallback_contra", contravariant=True, default=_JSON) |
| 18 | + |
| 19 | +class _JSONEncoder(Protocol[_EncodableTypes_co, _Encode_Fallback_contra]): |
| 20 | + """Custom encoder for `json.dumps` and `json.dump`.""" |
| 21 | + |
| 22 | + def encode(self, obj: _EncodableTypes) -> str: ... |
| 23 | + def iterencode(self, obj: _EncodableTypes, _one_shot: bool = False) -> Iterator[str]: ... |
| 24 | + def default(self, obj: _Encode_Fallback_contra) -> _EncodableTypes_co: ... |
14 | 25 |
|
15 |
| -@overload |
16 |
| -def dumps( |
17 |
| - obj: _JSON, |
18 |
| - *, |
19 |
| - skipkeys: bool = False, |
20 |
| - ensure_ascii: bool = True, |
21 |
| - check_circular: bool = True, |
22 |
| - allow_nan: bool = True, |
23 |
| - cls: type[JSONEncoder] | None = None, |
24 |
| - indent: None | int | str = None, |
25 |
| - separators: tuple[str, str] | None = None, |
26 |
| - default: None = None, |
27 |
| - sort_keys: bool = False, |
28 |
| - **kwds: Any, |
29 |
| -) -> str: ... |
30 |
| -@overload |
31 | 26 | def dumps(
|
32 |
| - obj: _T, |
| 27 | + obj: _EncodableTypes | _DefaultTypes | _ClsDefaultTypes, |
33 | 28 | *,
|
34 | 29 | skipkeys: bool = False,
|
35 | 30 | ensure_ascii: bool = True,
|
36 | 31 | check_circular: bool = True,
|
37 | 32 | allow_nan: bool = True,
|
38 |
| - cls: type[JSONEncoder] | None = None, |
| 33 | + cls: type[_JSONEncoder[_EncodableTypes, _ClsDefaultTypes]] | None = None, |
39 | 34 | indent: None | int | str = None,
|
40 | 35 | separators: tuple[str, str] | None = None,
|
41 |
| - default: Callable[[_T], _JSON], |
| 36 | + default: Callable[[_DefaultTypes], _EncodableTypes] | None = None, |
42 | 37 | sort_keys: bool = False,
|
43 | 38 | **kwds: Any,
|
44 | 39 | ) -> str: ...
|
45 |
| -@overload |
46 |
| -def dump( |
47 |
| - obj: _JSON, |
48 |
| - fp: SupportsWrite[str], |
49 |
| - *, |
50 |
| - skipkeys: bool = False, |
51 |
| - ensure_ascii: bool = True, |
52 |
| - check_circular: bool = True, |
53 |
| - allow_nan: bool = True, |
54 |
| - cls: type[JSONEncoder] | None = None, |
55 |
| - indent: None | int | str = None, |
56 |
| - separators: tuple[str, str] | None = None, |
57 |
| - default: None = None, |
58 |
| - sort_keys: bool = False, |
59 |
| - **kwds: Any, |
60 |
| -) -> None: ... |
61 |
| -@overload |
62 | 40 | def dump(
|
63 |
| - obj: _T, |
| 41 | + obj: _EncodableTypes | _DefaultTypes | _ClsDefaultTypes, |
64 | 42 | fp: SupportsWrite[str],
|
65 | 43 | *,
|
66 | 44 | skipkeys: bool = False,
|
67 | 45 | ensure_ascii: bool = True,
|
68 | 46 | check_circular: bool = True,
|
69 | 47 | allow_nan: bool = True,
|
70 |
| - cls: type[JSONEncoder] | None = None, |
| 48 | + cls: type[_JSONEncoder[_EncodableTypes, _DefaultTypes]] | None = None, |
71 | 49 | indent: None | int | str = None,
|
72 | 50 | separators: tuple[str, str] | None = None,
|
73 |
| - default: Callable[[_T], _JSON], |
| 51 | + default: Callable[[_DefaultTypes], _EncodableTypes] | None = None, |
74 | 52 | sort_keys: bool = False,
|
75 | 53 | **kwds: Any,
|
76 | 54 | ) -> None: ...
|
|
0 commit comments