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

Introducing wxPython type stubs #801

Merged
merged 36 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
8cfde69
formatted and fixed linting of clut_raycasting
omar-abdelgawad Jun 19, 2024
287ed77
changed wl_px initialization to None to make it compatible with Tuple…
omar-abdelgawad Jun 19, 2024
72a317a
Merge branch 'invesalius:master' into type-info-3
omar-abdelgawad Jun 20, 2024
9e9634f
Merge branch 'invesalius:master' into type-info-3
omar-abdelgawad Jun 30, 2024
0d339ab
Removed default values of Node attributes had initialization inside i…
omar-abdelgawad Jun 19, 2024
a62522c
Added type annotations to function signatures
omar-abdelgawad Jun 19, 2024
3939203
Formatted and fixed linting of clut_imagedata
omar-abdelgawad Jun 19, 2024
bdca5b2
Added typings directory for stubs
omar-abdelgawad Jun 30, 2024
c9dcb1a
fixed some annotations after addings stubs
omar-abdelgawad Jun 30, 2024
5272f7d
Annotated rest of widgets
omar-abdelgawad Jun 30, 2024
dbc342d
Added more type defs
omar-abdelgawad Jun 30, 2024
c296dec
removed python2 weakrefmethod compatibility
omar-abdelgawad Jun 30, 2024
7f63119
changed draw_text function to take value as input instead of relying …
omar-abdelgawad Jul 1, 2024
9df4bae
Added extra parameter to _move_node
omar-abdelgawad Jul 1, 2024
46cad3e
Added typings/utils for useful protocols
omar-abdelgawad Jul 1, 2024
be3a770
Changed Button API to take bitmap img on construction
omar-abdelgawad Jul 1, 2024
7cd7efe
Changed Node to a dataclass using slots
omar-abdelgawad Jul 1, 2024
6a94184
Removed wx.dataview
omar-abdelgawad Jul 1, 2024
3d3648e
Changed return None to exception
omar-abdelgawad Jul 2, 2024
426d53e
type hinted missing signatures
omar-abdelgawad Jul 2, 2024
9d5313f
Merge branch 'master' into type-info-3
omar-abdelgawad Jul 3, 2024
cc71e09
Merge branch 'invesalius:master' into type-info-3
omar-abdelgawad Jul 5, 2024
1f2c72e
Merge branch 'invesalius:master' into type-info-3
omar-abdelgawad Jul 5, 2024
aa7c598
Formatted constants.py
omar-abdelgawad Jul 5, 2024
7be144e
Merge branch 'invesalius:master' into type-info-3
omar-abdelgawad Jul 10, 2024
73616a0
resolved review comments
omar-abdelgawad Jul 10, 2024
6a0e34e
Fixed | to Union and turned types imported at typechecking only to str
omar-abdelgawad Jul 10, 2024
fd7b3ef
Merge branch 'invesalius:master' into type-info-3
omar-abdelgawad Jul 10, 2024
6ba59f9
Annotated draw_list
omar-abdelgawad Jul 10, 2024
0fa96e5
Changed CanvasHandlerBase to ABC
omar-abdelgawad Jul 10, 2024
1e1d319
Changed ColourType to accept str
omar-abdelgawad Jul 10, 2024
3b227d3
Added generic annotation for mypy
omar-abdelgawad Jul 10, 2024
180a35c
fixed second review errors
omar-abdelgawad Jul 12, 2024
d6a21b2
Merge branch 'master' into type-info-3
omar-abdelgawad Jul 14, 2024
a3141a4
Fixed Merge conflict
omar-abdelgawad Jul 14, 2024
841ccf3
Merge branch 'invesalius:master' into type-info-3
omar-abdelgawad Jul 17, 2024
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
816 changes: 455 additions & 361 deletions invesalius/constants.py

Large diffs are not rendered by default.

604 changes: 381 additions & 223 deletions invesalius/gui/widgets/canvas_renderer.py

Large diffs are not rendered by default.

165 changes: 83 additions & 82 deletions invesalius/gui/widgets/clut_imagedata.py

Large diffs are not rendered by default.

413 changes: 210 additions & 203 deletions invesalius/gui/widgets/clut_raycasting.py

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions invesalius/gui/widgets/gradient.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def OnPaint(self, evt: wx.Event) -> None:

width_transparency = self.max_position - self.min_position

gc: wx.GraphicsContext = wx.GraphicsContext.Create(dc)
gc = wx.GraphicsContext.Create(dc)

points: Sequence[Tuple[int, int, ColourType, ColourType]] = (
(0, PUSH_WIDTH, (0, 0, 0), (0, 0, 0)),
Expand All @@ -162,8 +162,12 @@ def OnPaint(self, evt: wx.Event) -> None:
)

# Drawing the gradient background
p1: float
p2: float
for p1, p2, c1, c2 in points:
brush = gc.CreateLinearGradientBrush(p1, 0, p2, h, c1, c2)
brush: Union[wx.GraphicsBrush, wx.Brush] = gc.CreateLinearGradientBrush(
p1, 0, p2, h, c1, c2
)
gc.SetBrush(brush)
path = gc.CreatePath()
path.AddRectangle(p1, 0, p2 - p1, h)
Expand Down
6 changes: 3 additions & 3 deletions invesalius/gui/widgets/inv_spinctrl.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def __init__(

self._textctrl = wx.TextCtrl(self, -1, style=style)
if spin_button and wx.Platform != "__WXGTK__":
self._spinbtn = wx.SpinButton(self, -1)
self._spinbtn: Optional[wx.SpinButton] = wx.SpinButton(self, -1)
omar-abdelgawad marked this conversation as resolved.
Show resolved Hide resolved
else:
self._spinbtn = None

Expand Down Expand Up @@ -94,7 +94,7 @@ def SetRange(self, min_value: int, max_value: int) -> None:
def GetValue(self) -> int:
return self._value

def SetValue(self, value: Any) -> None:
def SetValue(self, value: Union[float, str]) -> None:
try:
value = int(value)
except ValueError:
Expand Down Expand Up @@ -194,7 +194,7 @@ def __init__(

self._textctrl = wx.TextCtrl(self, -1, style=style)
if spin_button and wx.Platform != "__WXGTK__":
self._spinbtn = wx.SpinButton(self, -1)
self._spinbtn: Optional[wx.SpinButton] = wx.SpinButton(self, -1)
omar-abdelgawad marked this conversation as resolved.
Show resolved Hide resolved
else:
self._spinbtn = None

Expand Down
44 changes: 17 additions & 27 deletions invesalius/gui/widgets/slice_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,17 @@
# detalhes.
# --------------------------------------------------------------------------
import sys


from collections import OrderedDict
from typing import Dict

import wx
from invesalius.pubsub import pub as Publisher

import invesalius.constants as const
import invesalius.data.slice_ as sl
import invesalius.presets as presets
from invesalius.gui.dialogs import ClutImagedataDialog
from invesalius.i18n import tr as _
from invesalius.pubsub import pub as Publisher

PROJECTIONS_ID = OrderedDict(
(
Expand All @@ -47,9 +45,9 @@


class SliceMenu(wx.Menu):
def __init__(self):
def __init__(self) -> None:
wx.Menu.__init__(self)
self.ID_TO_TOOL_ITEM = {}
self.ID_TO_TOOL_ITEM: Dict[int | wx.WindowIDRef, wx.MenuItem] = {}
self.cdialog = None

# ------------ Sub menu of the window and level ----------
Expand Down Expand Up @@ -92,7 +90,7 @@ def __init__(self):
mkind = wx.ITEM_RADIO

submenu_pseudo_colours = wx.Menu()
self.pseudo_color_items: Dict[int, wx.MenuItem] = {}
self.pseudo_color_items: Dict[wx.WindowIDRef, wx.MenuItem] = {}
new_id = self.id_pseudo_first = wx.NewIdRef()
color_item = submenu_pseudo_colours.Append(new_id, _("Default "), kind=mkind)
color_item.Check(True)
Expand All @@ -119,13 +117,11 @@ def __init__(self):
self.pseudo_color_items[new_id] = color_item

# --------------- Sub menu of the projection type ---------------------
self.projection_items = {}
self.projection_items: Dict[int, wx.MenuItem] = {}
tfmoraes marked this conversation as resolved.
Show resolved Hide resolved
submenu_projection = wx.Menu()
for name in PROJECTIONS_ID:
new_id = wx.NewIdRef()
projection_item = submenu_projection.Append(
new_id, name, kind=wx.ITEM_RADIO
)
projection_item = submenu_projection.Append(new_id, name, kind=wx.ITEM_RADIO)
self.ID_TO_TOOL_ITEM[new_id] = projection_item
self.projection_items[PROJECTIONS_ID[name]] = projection_item

Expand All @@ -134,9 +130,7 @@ def __init__(self):
submenu_image_tiling = wx.Menu()
for name in sorted(const.IMAGE_TILING):
new_id = wx.NewIdRef()
image_tiling_item = submenu_image_tiling.Append(
new_id, name, kind=wx.ITEM_RADIO
)
image_tiling_item = submenu_image_tiling.Append(new_id, name, kind=wx.ITEM_RADIO)
self.ID_TO_TOOL_ITEM[new_id] = image_tiling_item

# Save first id item
Expand All @@ -161,14 +155,14 @@ def __init__(self):

self.__bind_events()

def __bind_events(self):
def __bind_events(self) -> None:
Publisher.subscribe(self.CheckWindowLevelOther, "Check window and level other")
Publisher.subscribe(self.FirstItemSelect, "Select first item from slice menu")
Publisher.subscribe(self._close, "Close project data")

Publisher.subscribe(self._check_projection_menu, "Check projection menu")

def FirstItemSelect(self):
def FirstItemSelect(self) -> None:
item = self.ID_TO_TOOL_ITEM[self.id_wl_first]
item.Check(True)

Expand All @@ -182,26 +176,24 @@ def FirstItemSelect(self):
# item = self.ID_TO_TOOL_ITEM[self.id_tiling_first]
# item.Check(True)

def CheckWindowLevelOther(self):
def CheckWindowLevelOther(self) -> None:
item = self.ID_TO_TOOL_ITEM[self.other_wl_id]
item.Check()

def _check_projection_menu(self, projection_id):
def _check_projection_menu(self, projection_id: int) -> None:
item = self.projection_items[projection_id]
item.Check()

def OnPopup(self, evt):
id = evt.GetId()
def OnPopup(self, evt: wx.Event) -> None:
omar-abdelgawad marked this conversation as resolved.
Show resolved Hide resolved
# id = evt.GetId()
item = self.ID_TO_TOOL_ITEM[evt.GetId()]
key = item.GetItemLabelText()
if key in const.WINDOW_LEVEL.keys():
window, level = const.WINDOW_LEVEL[key]
Publisher.sendMessage(
"Bright and contrast adjustment image", window=window, level=level
)
Publisher.sendMessage(
"Update window level value", window=window, level=level
)
Publisher.sendMessage("Update window level value", window=window, level=level)
# Publisher.sendMessage('Update window and level text',
# "WL: %d WW: %d"%(level, window))
Publisher.sendMessage("Update slice viewer")
Expand All @@ -211,9 +203,7 @@ def OnPopup(self, evt):

elif key in const.SLICE_COLOR_TABLE.keys():
values = const.SLICE_COLOR_TABLE[key]
Publisher.sendMessage(
"Change colour table from background image", values=values
)
Publisher.sendMessage("Change colour table from background image", values=values)
Publisher.sendMessage("Update slice viewer")

if sys.platform.startswith("linux"):
Expand Down Expand Up @@ -275,11 +265,11 @@ def OnPopup(self, evt):

evt.Skip()

def HideClutDialog(self):
def HideClutDialog(self) -> None:
if self.cdialog:
self.cdialog.Hide()

def _close(self):
def _close(self) -> None:
if self.cdialog:
self.cdialog.Destroy()
self.cdialog = None
13 changes: 10 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ requires-python = ">=3.8"
packages = ["invesalius", "invesalius_cy"]

[tool.mypy]
mypy_path = "typings"
show_error_codes = true
check_untyped_defs = false
disallow_any_generics = false
Expand All @@ -31,10 +32,15 @@ warn_unreachable = false
warn_unused_configs = false
no_implicit_reexport = false
follow_imports = "normal"

disable_error_code = ["attr-defined", "no-redef"]
explicit_package_bases = true
disable_error_code = ["attr-defined", "no-redef"]

[mypy-invesalius.gui.widgets.listctrl]
ignore_errors = true

[[tool.mypy.overrides]]
module = 'wx.*'
disable_error_code = ["no-redef"] # because some classes are redefined in the stubs
[[tool.mypy.overrides]]
module = [
'wx.*',
Expand Down Expand Up @@ -85,11 +91,12 @@ ignore_missing_imports = true

[tool.ruff]
include = ["pyproject.toml", "invesalius/**/*.py",'setup.py','app.py']
extend-exclude = ["./invesalius/gui/widgets/listctrl.py"]
line-length = 100
# extend-exclude = ["plugins"]

[tool.ruff.lint]
extend-select = ["UP"] # Introduces linting rules for pyupgrade
extend-select = ["UP","I"] # Introduces linting rules for pyupgrade
# We can extend the list with 'I' and 'PL' in the future

[tool.ruff.format]
Expand Down
6 changes: 6 additions & 0 deletions typings/utils/__init__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from typing import Generic, Protocol, TypeVar

T = TypeVar("T", covariant=True)

class SupportsGetItem(Protocol, Generic[T]):
def __getitem__(self, key: int, /) -> T: ...
1 change: 1 addition & 0 deletions typings/wx/__init__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .core import *
Loading
Loading