Skip to content

Commit 3ca5208

Browse files
committed
revert
1 parent 08a6059 commit 3ca5208

File tree

2 files changed

+57
-43
lines changed

2 files changed

+57
-43
lines changed

stdlib/@tests/test_cases/check_json.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import json
44
from decimal import Decimal
5+
from typing import Iterator
56

67

78
class _File:
@@ -77,3 +78,38 @@ def custom_encoder(obj: Decimal) -> Decimal:
7778

7879
json.dumps(Decimal(1), default=custom_encoder) # type: ignore
7980
json.dump(Decimal(1), fp, default=custom_encoder) # type: ignore
81+
82+
83+
# Custom encoders support informing `json.dumps` and `json.dump` about the type of the object.
84+
class CustomEncoder(json.JSONEncoder):
85+
def default(self, obj: str) -> str:
86+
return str(obj)
87+
88+
def encode(self, obj: Decimal) -> str:
89+
return str(obj)
90+
91+
def iterencode(self, obj: Decimal, _one_shot: bool = False) -> Iterator[str]:
92+
return iter([str(obj)])
93+
94+
95+
# Now, Decimal 1 is supported because of the default method.
96+
json.dumps(Decimal(1), cls=CustomEncoder)
97+
json.dump(Decimal(1), fp, cls=CustomEncoder)
98+
99+
100+
# If Default didn't support, but encode did that would also work.
101+
102+
103+
class CustomEncoder2(json.JSONEncoder):
104+
def default(self, obj: str) -> str:
105+
return str(obj)
106+
107+
def encode(self, obj: Decimal) -> str:
108+
return str(obj)
109+
110+
def iterencode(self, obj: Decimal, _one_shot: bool = False) -> Iterator[str]:
111+
return iter([str(obj)])
112+
113+
114+
json.dumps(Decimal(1), cls=CustomEncoder2)
115+
json.dump(Decimal(1), fp, cls=CustomEncoder2)

stdlib/json/__init__.pyi

Lines changed: 21 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,54 @@
11
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
44
from typing_extensions import TypeAlias
55

66
from .decoder import JSONDecodeError as JSONDecodeError, JSONDecoder as JSONDecoder
77
from .encoder import JSONEncoder as JSONEncoder
88

99
__all__ = ["dump", "dumps", "load", "loads", "JSONDecoder", "JSONDecodeError", "JSONEncoder"]
1010

11-
_T = TypeVar("_T")
12-
1311
_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: ...
1425

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
3126
def dumps(
32-
obj: _T,
27+
obj: _EncodableTypes | _DefaultTypes | _ClsDefaultTypes,
3328
*,
3429
skipkeys: bool = False,
3530
ensure_ascii: bool = True,
3631
check_circular: bool = True,
3732
allow_nan: bool = True,
38-
cls: type[JSONEncoder] | None = None,
33+
cls: type[_JSONEncoder[_EncodableTypes, _ClsDefaultTypes]] | None = None,
3934
indent: None | int | str = None,
4035
separators: tuple[str, str] | None = None,
41-
default: Callable[[_T], _JSON],
36+
default: Callable[[_DefaultTypes], _EncodableTypes] | None = None,
4237
sort_keys: bool = False,
4338
**kwds: Any,
4439
) -> 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
6240
def dump(
63-
obj: _T,
41+
obj: _EncodableTypes | _DefaultTypes | _ClsDefaultTypes,
6442
fp: SupportsWrite[str],
6543
*,
6644
skipkeys: bool = False,
6745
ensure_ascii: bool = True,
6846
check_circular: bool = True,
6947
allow_nan: bool = True,
70-
cls: type[JSONEncoder] | None = None,
48+
cls: type[_JSONEncoder[_EncodableTypes, _DefaultTypes]] | None = None,
7149
indent: None | int | str = None,
7250
separators: tuple[str, str] | None = None,
73-
default: Callable[[_T], _JSON],
51+
default: Callable[[_DefaultTypes], _EncodableTypes] | None = None,
7452
sort_keys: bool = False,
7553
**kwds: Any,
7654
) -> None: ...

0 commit comments

Comments
 (0)