Skip to content
This repository was archived by the owner on Jun 22, 2025. It is now read-only.

Commit da32489

Browse files
Fix type hints for json encoder/decoder
1 parent 7332538 commit da32489

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

eel/__init__.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
from builtins import range
22
import traceback
33
from io import open
4-
from typing import Union, Any, Dict, List, Set, Tuple, Optional, Callable, TYPE_CHECKING
4+
from typing import Union, Any, Dict, List, Set, Tuple, Optional, Callable, TYPE_CHECKING, cast, Type
55

66
if TYPE_CHECKING:
7-
from eel.types import OptionsDictT, WebSocketT
7+
from eel.types import OptionsDictT, WebSocketT, JSONEncoder, JSONDecoder
88
else:
99
WebSocketT = Any
1010
OptionsDictT = Any
11+
JSONEncoder = Any
12+
JSONDecoder = Any
1113

1214
from gevent.threading import Timer
1315
import gevent as gvt
@@ -28,7 +30,7 @@
2830
mimetypes.add_type('application/javascript', '.js')
2931
_eel_js_file: str = pkg.resource_filename('eel', 'eel.js')
3032
_eel_js: str = open(_eel_js_file, encoding='utf-8').read()
31-
_eel_json_dumps_default_function: Callable = lambda o: None
33+
_eel_json_dumps_default_function: Callable[[Any], Any] = lambda o: None
3234
_websockets: List[Tuple[Any, WebSocketT]] = []
3335
_call_return_values: Dict[Any, Any] = {}
3436
_call_return_callbacks: Dict[float, Tuple[Callable[..., Any], Optional[Callable[..., Any]]]] = {}
@@ -302,10 +304,10 @@ def register_eel_routes(app: btl.Bottle) -> None:
302304
# Private functions
303305

304306
def _safe_json_loads(obj: str) -> Any:
305-
return jsn.loads(obj, cls=_start_args['json_decoder'])
307+
return jsn.loads(obj, cls=cast(Optional[Type[JSONDecoder]], _start_args['json_decoder']))
306308

307309
def _safe_json_dumps(obj: Any) -> str:
308-
return jsn.dumps(obj, cls=_start_args['json_encoder'],
310+
return jsn.dumps(obj, cls=cast(Optional[Type[JSONEncoder]], _start_args['json_encoder']),
309311
default=_eel_json_dumps_default_function if not _start_args['json_encoder'] else None)
310312

311313

eel/types.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Union, Dict, List, Tuple, Callable, Optional, Any, TYPE_CHECKING
1+
from typing import Union, Dict, List, Tuple, Callable, Optional, Any, TYPE_CHECKING, Type
22

33
# This business is slightly awkward, but needed for backward compatibility,
44
# because Python < 3.7 doesn't have __future__/annotations, and <3.10 doesn't
@@ -12,17 +12,20 @@
1212
JinjaEnvironmentT = Environment # type: ignore
1313
from geventwebsocket.websocket import WebSocket
1414
WebSocketT = WebSocket
15+
from json import JSONDecoder, JSONEncoder
1516
else:
1617
JinjaEnvironmentT = None
1718
WebSocketT = Any
19+
JSONEncoder = Any
20+
JSONDecoder = Any
1821

1922
OptionsDictT = Dict[
2023
str,
2124
Optional[
2225
Union[
2326
str, bool, int, float,
2427
List[str], Tuple[int, int], Dict[str, Tuple[int, int]],
25-
Callable[..., Any], JinjaEnvironmentT
28+
Callable[..., Any], JinjaEnvironmentT, Type[JSONEncoder], Type[JSONDecoder]
2629
]
2730
]
2831
]

0 commit comments

Comments
 (0)