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

Commit a82719f

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

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

eel/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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:
77
from eel.types import OptionsDictT, WebSocketT
@@ -28,7 +28,7 @@
2828
mimetypes.add_type('application/javascript', '.js')
2929
_eel_js_file: str = pkg.resource_filename('eel', 'eel.js')
3030
_eel_js: str = open(_eel_js_file, encoding='utf-8').read()
31-
_eel_json_dumps_default_function: Callable = lambda o: None
31+
_eel_json_dumps_default_function: Callable[[Any], Any] = lambda o: None
3232
_websockets: List[Tuple[Any, WebSocketT]] = []
3333
_call_return_values: Dict[Any, Any] = {}
3434
_call_return_callbacks: Dict[float, Tuple[Callable[..., Any], Optional[Callable[..., Any]]]] = {}
@@ -302,10 +302,10 @@ def register_eel_routes(app: btl.Bottle) -> None:
302302
# Private functions
303303

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

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

311311

@@ -416,7 +416,7 @@ def _detect_shutdown() -> None:
416416
def _websocket_close(page: str) -> None:
417417
global _shutdown
418418

419-
close_callback = _start_args.get('close_callback')
419+
close_callback = cast(Callable[..., Any], _start_args.get('close_callback'))
420420

421421
if close_callback is not None:
422422
if not callable(close_callback):

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)