Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add proper Python typing to Welcome demo #146

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ install
.venv
__pycache__
*.pyc
*.egg-info

# IDEs / editors
.idea
Expand Down
31 changes: 31 additions & 0 deletions python-aux/README.md
Original file line number Diff line number Diff line change
@@ -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.
5 changes: 5 additions & 0 deletions python-aux/mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[mypy]
warn_unused_configs = True
explicit_package_bases = True
namespace_packages = True
check_untyped_defs = True
4 changes: 4 additions & 0 deletions python-aux/requirements.txt
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions python-aux/workbench-typestubs/MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
include workbench/py.typed
recursive-include workbench *.pyi
7 changes: 7 additions & 0 deletions python-aux/workbench-typestubs/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"

[project]
name = "workbench-typestubs"
version = "46.0.0"
7 changes: 7 additions & 0 deletions python-aux/workbench-typestubs/workbench/__init__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from gi.repository import Gtk, Gio

window: Gtk.Window
builder: Gtk.Builder

def resolve(path: str) -> Gio.File:
...
Empty file.
4 changes: 3 additions & 1 deletion src/Welcome/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import cast

import gi

gi.require_version("Gtk", "4.0")
Expand All @@ -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"))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is needed, otherwise mypy complains:

src/Welcome/main.py:22: error: Incompatible types in assignment (expression has type "Object | None", variable has type "Box")  [assignment]
Found 1 error in 1 file (checked 1 source file)

We will need to add these casts everywhere where the concrete return type can vary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cast is just the identity function at runtime: https://docs.python.org/3/library/typing.html#typing.cast


button = Gtk.Button(label="Press me", margin_top=6, css_classes=["suggested-action"])
button.connect("clicked", greet)
Expand Down
Loading