From fcc9d5b6b3af7408b2842baea635d5fb681565f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20K=C3=B6pcke?= Date: Mon, 22 Apr 2024 11:07:45 +0200 Subject: [PATCH 1/3] Support files for Python typing --- .gitignore | 1 + python-aux/README.md | 31 +++++++++++++++++++ python-aux/mypy.ini | 5 +++ python-aux/requirements.txt | 4 +++ python-aux/workbench-typestubs/MANIFEST.in | 2 ++ python-aux/workbench-typestubs/pyproject.toml | 7 +++++ .../workbench/__init__.pyi | 7 +++++ .../workbench-typestubs/workbench/py.typed | 0 8 files changed, 57 insertions(+) create mode 100644 python-aux/README.md create mode 100644 python-aux/mypy.ini create mode 100644 python-aux/requirements.txt create mode 100644 python-aux/workbench-typestubs/MANIFEST.in create mode 100644 python-aux/workbench-typestubs/pyproject.toml create mode 100644 python-aux/workbench-typestubs/workbench/__init__.pyi create mode 100644 python-aux/workbench-typestubs/workbench/py.typed diff --git a/.gitignore b/.gitignore index cc5fd82c..0950d03e 100644 --- a/.gitignore +++ b/.gitignore @@ -14,6 +14,7 @@ install .venv __pycache__ *.pyc +*.egg-info # IDEs / editors .idea diff --git a/python-aux/README.md b/python-aux/README.md new file mode 100644 index 00000000..7b2d780c --- /dev/null +++ b/python-aux/README.md @@ -0,0 +1,31 @@ +# Python Development Support Files + +This directory contains support files for developing Python demos: + +- `requirements.txt`: + Requirements for development. Contains dependency for `ruff` (linter & formatter), `mypy` (type checker) + and `workbench-typestubs` (typestubs for the `workbench` module). All of these are also installed + in the `re.sonny.Workbench.Devel` Flatpak (TODO: not true yet, see below.) +- `mypy.ini`: + Rules for `mypy` used for all demos. +- `workbench-typestubs`: + Python package containing typestubs for the `workbench` package which is available to demos and + implements the Workbench API. + +## Run type checks. + +```sh +cd python-aux +pip3 install -r requirements.txt +cd .. +# Check single file: +mypy --config-file python-aux/mypy.ini src/Welcome/main.py +# Check all: +mypy --config-file python-aux/mypy.ini src +``` + +## TODO + +TODO: All requirements should be in the Devel Flatpak, but unsure how the +typestub itself could be added there...? We might need to put that in an extra repo and add it +as a module to the Devel Flatpak. Ruff is already in there. diff --git a/python-aux/mypy.ini b/python-aux/mypy.ini new file mode 100644 index 00000000..a47f0f90 --- /dev/null +++ b/python-aux/mypy.ini @@ -0,0 +1,5 @@ +[mypy] +warn_unused_configs = True +explicit_package_bases = True +namespace_packages = True +check_untyped_defs = True diff --git a/python-aux/requirements.txt b/python-aux/requirements.txt new file mode 100644 index 00000000..9eda497e --- /dev/null +++ b/python-aux/requirements.txt @@ -0,0 +1,4 @@ +./workbench-typestubs +ruff>=0.3.0 +pygobject-stubs>=2.11.0 --config-settings=config=Gtk4,Gdk4,GtkSource5 +mypy>=1.9.0 diff --git a/python-aux/workbench-typestubs/MANIFEST.in b/python-aux/workbench-typestubs/MANIFEST.in new file mode 100644 index 00000000..b449b749 --- /dev/null +++ b/python-aux/workbench-typestubs/MANIFEST.in @@ -0,0 +1,2 @@ +include workbench/py.typed +recursive-include workbench *.pyi diff --git a/python-aux/workbench-typestubs/pyproject.toml b/python-aux/workbench-typestubs/pyproject.toml new file mode 100644 index 00000000..07620bd6 --- /dev/null +++ b/python-aux/workbench-typestubs/pyproject.toml @@ -0,0 +1,7 @@ +[build-system] +requires = ["setuptools"] # REQUIRED if [build-system] table is used +build-backend = "setuptools.build_meta" # If not defined, then legacy behavior can happen. + +[project] +name = "workbench-typestubs" +version = "46.0.0" diff --git a/python-aux/workbench-typestubs/workbench/__init__.pyi b/python-aux/workbench-typestubs/workbench/__init__.pyi new file mode 100644 index 00000000..9ef5c551 --- /dev/null +++ b/python-aux/workbench-typestubs/workbench/__init__.pyi @@ -0,0 +1,7 @@ +from gi.repository import Gtk, Gio + +window: Gtk.Window +builder: Gtk.Builder + +def resolve(self, path: str) -> Gio.File: + ... diff --git a/python-aux/workbench-typestubs/workbench/py.typed b/python-aux/workbench-typestubs/workbench/py.typed new file mode 100644 index 00000000..e69de29b From b337cd3f7cfc67b126053376c8d0335f8fb6736f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20K=C3=B6pcke?= Date: Mon, 22 Apr 2024 11:07:56 +0200 Subject: [PATCH 2/3] Typing for Welcome demo --- python-aux/workbench-typestubs/MANIFEST.in | 2 +- python-aux/workbench-typestubs/pyproject.toml | 4 ++-- src/Welcome/main.py | 4 +++- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/python-aux/workbench-typestubs/MANIFEST.in b/python-aux/workbench-typestubs/MANIFEST.in index b449b749..49adbf0c 100644 --- a/python-aux/workbench-typestubs/MANIFEST.in +++ b/python-aux/workbench-typestubs/MANIFEST.in @@ -1,2 +1,2 @@ include workbench/py.typed -recursive-include workbench *.pyi +recursive-include workbench *.pyi diff --git a/python-aux/workbench-typestubs/pyproject.toml b/python-aux/workbench-typestubs/pyproject.toml index 07620bd6..d2f5d48d 100644 --- a/python-aux/workbench-typestubs/pyproject.toml +++ b/python-aux/workbench-typestubs/pyproject.toml @@ -1,6 +1,6 @@ [build-system] -requires = ["setuptools"] # REQUIRED if [build-system] table is used -build-backend = "setuptools.build_meta" # If not defined, then legacy behavior can happen. +requires = ["setuptools"] +build-backend = "setuptools.build_meta" [project] name = "workbench-typestubs" diff --git a/src/Welcome/main.py b/src/Welcome/main.py index 9b95a029..de0a792f 100644 --- a/src/Welcome/main.py +++ b/src/Welcome/main.py @@ -1,3 +1,5 @@ +from typing import cast + import gi gi.require_version("Gtk", "4.0") @@ -19,7 +21,7 @@ def dialog_response(dialog, response): dialog.close() -subtitle_box: Gtk.Box = workbench.builder.get_object("subtitle") +subtitle_box = cast(Gtk.Box, workbench.builder.get_object("subtitle")) button = Gtk.Button(label="Press me", margin_top=6, css_classes=["suggested-action"]) button.connect("clicked", greet) From d2df1582eecf1c0974b80630097e16bacc34d6e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20K=C3=B6pcke?= Date: Mon, 22 Apr 2024 11:23:39 +0200 Subject: [PATCH 3/3] fix wrong parameter in workbench typestub --- python-aux/workbench-typestubs/workbench/__init__.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python-aux/workbench-typestubs/workbench/__init__.pyi b/python-aux/workbench-typestubs/workbench/__init__.pyi index 9ef5c551..d95e2873 100644 --- a/python-aux/workbench-typestubs/workbench/__init__.pyi +++ b/python-aux/workbench-typestubs/workbench/__init__.pyi @@ -3,5 +3,5 @@ from gi.repository import Gtk, Gio window: Gtk.Window builder: Gtk.Builder -def resolve(self, path: str) -> Gio.File: +def resolve(path: str) -> Gio.File: ...