Skip to content

Commit

Permalink
Refined type annotations to reflect move to python>=3.7 (#683)
Browse files Browse the repository at this point in the history
Co-authored-by: Samuel Williams <samuelhwilliams@gmail.com>
  • Loading branch information
thatfloflo and samuelhwilliams authored Nov 19, 2024
1 parent 094d446 commit 7c3f6ad
Show file tree
Hide file tree
Showing 11 changed files with 330 additions and 105 deletions.
332 changes: 274 additions & 58 deletions eel/__init__.py

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions eel/__main__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import annotations
import pkg_resources as pkg
import PyInstaller.__main__ as pyi
import os
Expand Down
3 changes: 2 additions & 1 deletion eel/browsers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import annotations
import subprocess as sps
import webbrowser as wbr
from typing import Union, List, Dict, Iterable, Optional
Expand Down Expand Up @@ -53,7 +54,7 @@ def open(start_pages: Iterable[Union[str, Dict[str, str]]], options: OptionsDict
start_urls = _build_urls(start_pages, options)

mode = options.get('mode')
if not isinstance(mode, (str, bool, type(None))) or mode is True:
if not isinstance(mode, (str, type(None))) and mode is not False:
raise TypeError("'mode' option must by either a string, False, or None")
if mode is None or mode is False:
# Don't open a browser
Expand Down
12 changes: 7 additions & 5 deletions eel/chrome.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import sys, subprocess as sps, os
from __future__ import annotations
import sys
import os
import subprocess as sps
from shutil import which
from typing import List, Optional

from eel.types import OptionsDictT

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


def _find_chrome_linux() -> Optional[str]:
import whichcraft as wch
chrome_names = ['chromium-browser',
'chromium',
'google-chrome',
'google-chrome-stable']

for name in chrome_names:
chrome = wch.which(name)
chrome = which(name)
if chrome is not None:
return chrome # type: ignore # whichcraft doesn't currently have type hints
return chrome
return None


Expand Down
1 change: 1 addition & 0 deletions eel/edge.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import annotations
import platform
import subprocess as sps
import sys
Expand Down
17 changes: 8 additions & 9 deletions eel/electron.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#from __future__ import annotations
from __future__ import annotations
import sys
import os
import subprocess as sps
import whichcraft as wch
from shutil import which
from typing import List, Optional

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

# This should work fine...
return which('electron')
return None
54 changes: 34 additions & 20 deletions eel/types.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,42 @@
from __future__ import annotations
from typing import Union, Dict, List, Tuple, Callable, Optional, Any, TYPE_CHECKING
from typing_extensions import Literal, TypedDict, TypeAlias
from bottle import Bottle

# This business is slightly awkward, but needed for backward compatibility,
# because Python < 3.7 doesn't have __future__/annotations, and <3.10 doesn't
# support TypeAlias.
# because Python <3.10 doesn't support TypeAlias, jinja2 may not be available
# at runtime, and geventwebsocket.websocket doesn't have type annotations so
# that direct imports will raise an error.
if TYPE_CHECKING:
from jinja2 import Environment
try:
from typing import TypeAlias # Introduced in Python 3.10
JinjaEnvironmentT: TypeAlias = Environment
except ImportError:
JinjaEnvironmentT = Environment # type: ignore
JinjaEnvironmentT: TypeAlias = Environment
from geventwebsocket.websocket import WebSocket
WebSocketT = WebSocket
WebSocketT: TypeAlias = WebSocket
else:
JinjaEnvironmentT = None
WebSocketT = Any
JinjaEnvironmentT: TypeAlias = Any
WebSocketT: TypeAlias = Any

OptionsDictT = Dict[
str,
Optional[
Union[
str, bool, int, float,
List[str], Tuple[int, int], Dict[str, Tuple[int, int]],
Callable[..., Any], JinjaEnvironmentT
]
]
]
OptionsDictT = TypedDict(
'OptionsDictT',
{
'mode': Optional[Union[str, Literal[False]]],
'host': str,
'port': int,
'block': bool,
'jinja_templates': Optional[str],
'cmdline_args': List[str],
'size': Optional[Tuple[int, int]],
'position': Optional[Tuple[int, int]],
'geometry': Dict[str, Tuple[int, int]],
'close_callback': Optional[Callable[..., Any]],
'app_mode': bool,
'all_interfaces': bool,
'disable_cache': bool,
'default_path': str,
'app': Bottle,
'shutdown_delay': float,
'suppress_error': bool,
'jinja_env': JinjaEnvironmentT,
},
total=False
)
2 changes: 1 addition & 1 deletion examples/07 - CreateReactApp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ If you run into any issues with this example, open a [new issue](https://github.

## Quick Start

1. **Configure:** In the app's directory, run `npm install` and `pip install bottle bottle-websocket future whichcraft pyinstaller`
1. **Configure:** In the app's directory, run `npm install` and `pip install bottle bottle-websocket future pyinstaller`
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/`
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))
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.
Expand Down
9 changes: 0 additions & 9 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ warn_unused_configs = True
[mypy-bottle_websocket]
ignore_missing_imports = True

[mypy-jinja2]
ignore_missing_imports = True

[mypy-gevent]
ignore_missing_imports = True

Expand All @@ -26,12 +23,6 @@ ignore_missing_imports = True
[mypy-bottle.ext.websocket]
ignore_missing_imports = True

[mypy-whichcraft]
ignore_missing_imports = True

[mypy-pyparsing]
ignore_missing_imports = True

[mypy-PyInstaller]
ignore_missing_imports = True

Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ gevent
gevent-websocket<1.0.0
greenlet>=1.0.0,<2.0.0
pyparsing>=3.0.0,<4.0.0
whichcraft~=0.4.1
typing-extensions>=4.3.0
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
package_data={
'eel': ['eel.js', 'py.typed'],
},
install_requires=['bottle', 'bottle-websocket', 'future', 'pyparsing', 'whichcraft'],
install_requires=['bottle', 'bottle-websocket', 'future', 'pyparsing'],
extras_require={
"jinja2": ['jinja2>=2.10']
},
Expand Down

0 comments on commit 7c3f6ad

Please sign in to comment.