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

Commit 831eaf7

Browse files
committed
Refined type annotations to reflect move to python>=3.7
1 parent 7e88610 commit 831eaf7

File tree

11 files changed

+323
-102
lines changed

11 files changed

+323
-102
lines changed

eel/__init__.py

Lines changed: 267 additions & 55 deletions
Large diffs are not rendered by default.

eel/__main__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from __future__ import annotations
12
import pkg_resources as pkg
23
import PyInstaller.__main__ as pyi
34
import os

eel/browsers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from __future__ import annotations
12
import subprocess as sps
23
import webbrowser as wbr
34
from typing import Union, List, Dict, Iterable, Optional
@@ -51,7 +52,7 @@ def open(start_pages: Iterable[Union[str, Dict[str, str]]], options: OptionsDict
5152
start_urls = _build_urls(start_pages, options)
5253

5354
mode = options.get('mode')
54-
if not isinstance(mode, (str, bool, type(None))) or mode is True:
55+
if not isinstance(mode, (str, type(None))) and mode is not False:
5556
raise TypeError("'mode' option must by either a string, False, or None")
5657
if mode is None or mode is False:
5758
# Don't open a browser

eel/chrome.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
import sys, subprocess as sps, os
1+
from __future__ import annotations
2+
import sys
3+
import os
4+
import subprocess as sps
5+
from shutil import which
26
from typing import List, Optional
3-
47
from eel.types import OptionsDictT
58

69
# Every browser specific module must define run(), find_path() and name like this
@@ -57,16 +60,15 @@ def _find_chromium_mac() -> Optional[str]:
5760

5861

5962
def _find_chrome_linux() -> Optional[str]:
60-
import whichcraft as wch
6163
chrome_names = ['chromium-browser',
6264
'chromium',
6365
'google-chrome',
6466
'google-chrome-stable']
6567

6668
for name in chrome_names:
67-
chrome = wch.which(name)
69+
chrome = which(name)
6870
if chrome is not None:
69-
return chrome # type: ignore # whichcraft doesn't currently have type hints
71+
return chrome
7072
return None
7173

7274

eel/edge.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from __future__ import annotations
12
import platform
23
import subprocess as sps
34
import sys

eel/electron.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
#from __future__ import annotations
1+
from __future__ import annotations
22
import sys
33
import os
44
import subprocess as sps
5-
import whichcraft as wch
5+
from shutil import which
66
from typing import List, Optional
77

88
from eel.types import OptionsDictT
@@ -20,11 +20,10 @@ def run(path: str, options: OptionsDictT, start_urls: List[str]) -> None:
2020
def find_path() -> Optional[str]:
2121
if sys.platform in ['win32', 'win64']:
2222
# It doesn't work well passing the .bat file to Popen, so we get the actual .exe
23-
bat_path = wch.which('electron')
24-
return os.path.join(bat_path, r'..\node_modules\electron\dist\electron.exe')
23+
bat_path = which('electron')
24+
if bat_path:
25+
return os.path.join(bat_path, r'..\node_modules\electron\dist\electron.exe')
2526
elif sys.platform in ['darwin', 'linux']:
26-
# This should work find...
27-
return wch.which('electron') # type: ignore # whichcraft doesn't currently have type hints
28-
else:
29-
return None
30-
27+
# This should work fine...
28+
return which('electron')
29+
return None

eel/types.py

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,42 @@
1+
from __future__ import annotations
12
from typing import Union, Dict, List, Tuple, Callable, Optional, Any, TYPE_CHECKING
3+
from typing_extensions import Literal, TypedDict, TypeAlias
4+
from bottle import Bottle
25

36
# This business is slightly awkward, but needed for backward compatibility,
4-
# because Python < 3.7 doesn't have __future__/annotations, and <3.10 doesn't
5-
# support TypeAlias.
7+
# because Python <3.10 doesn't support TypeAlias, jinja2 may not be available
8+
# at runtime, and geventwebsocket.websocket doesn't have type annotations so
9+
# that direct imports will raise an error.
610
if TYPE_CHECKING:
711
from jinja2 import Environment
8-
try:
9-
from typing import TypeAlias # Introduced in Python 3.10
10-
JinjaEnvironmentT: TypeAlias = Environment
11-
except ImportError:
12-
JinjaEnvironmentT = Environment # type: ignore
12+
JinjaEnvironmentT: TypeAlias = Environment
1313
from geventwebsocket.websocket import WebSocket
14-
WebSocketT = WebSocket
14+
WebSocketT: TypeAlias = WebSocket
1515
else:
16-
JinjaEnvironmentT = None
17-
WebSocketT = Any
16+
JinjaEnvironmentT: TypeAlias = Any
17+
WebSocketT: TypeAlias = Any
1818

19-
OptionsDictT = Dict[
20-
str,
21-
Optional[
22-
Union[
23-
str, bool, int, float,
24-
List[str], Tuple[int, int], Dict[str, Tuple[int, int]],
25-
Callable[..., Any], JinjaEnvironmentT
26-
]
27-
]
28-
]
19+
OptionsDictT = TypedDict(
20+
'OptionsDictT',
21+
{
22+
'mode': Optional[Union[str, Literal[False]]],
23+
'host': str,
24+
'port': int,
25+
'block': bool,
26+
'jinja_templates': Optional[str],
27+
'cmdline_args': List[str],
28+
'size': Optional[Tuple[int, int]],
29+
'position': Optional[Tuple[int, int]],
30+
'geometry': Dict[str, Tuple[int, int]],
31+
'close_callback': Optional[Callable[..., Any]],
32+
'app_mode': bool,
33+
'all_interfaces': bool,
34+
'disable_cache': bool,
35+
'default_path': str,
36+
'app': Bottle,
37+
'shutdown_delay': float,
38+
'suppress_error': bool,
39+
'jinja_env': JinjaEnvironmentT,
40+
},
41+
total=False
42+
)

examples/07 - CreateReactApp/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ If you run into any issues with this example, open a [new issue](https://github.
1919

2020
## Quick Start
2121

22-
1. **Configure:** In the app's directory, run `npm install` and `pip install bottle bottle-websocket future whichcraft pyinstaller`
22+
1. **Configure:** In the app's directory, run `npm install` and `pip install bottle bottle-websocket future pyinstaller`
2323
2. **Demo:** Build static files with `npm run build` then run the application with `python eel_CRA.py`. A Chrome-app window should open running the built code from `build/`
2424
3. **Distribute:** (Run `npm run build` first) Build a binary distribution with PyInstaller using `python -m eel eel_CRA.py build --onefile` (See more detailed PyInstaller instructions at bottom of [the main README](https://github.com/ChrisKnott/Eel))
2525
4. **Develop:** Open two prompts. In one, run `python eel_CRA.py true` and the other, `npm start`. A browser window should open in your default web browser at: [http://localhost:3000/](http://localhost:3000/). As you make changes to the JavaScript in `src/` the browser will reload. Any changes to `eel_CRA.py` will require a restart to take effect. You may need to refresh the browser window if it gets out of sync with eel.

mypy.ini

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
python_version = 3.10
33
warn_unused_configs = True
44

5-
[mypy-jinja2]
6-
ignore_missing_imports = True
7-
85
[mypy-gevent]
96
ignore_missing_imports = True
107

@@ -23,12 +20,6 @@ ignore_missing_imports = True
2320
[mypy-bottle.ext.websocket]
2421
ignore_missing_imports = True
2522

26-
[mypy-whichcraft]
27-
ignore_missing_imports = True
28-
29-
[mypy-pyparsing]
30-
ignore_missing_imports = True
31-
3223
[mypy-PyInstaller]
3324
ignore_missing_imports = True
3425

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ gevent
44
gevent-websocket<1.0.0
55
greenlet>=1.0.0,<2.0.0
66
pyparsing>=3.0.0,<4.0.0
7-
whichcraft~=0.4.1
7+
typing-extensions>=4.3.0

0 commit comments

Comments
 (0)