Skip to content

Commit

Permalink
Typing fixes from review
Browse files Browse the repository at this point in the history
  • Loading branch information
rmartin16 committed Jun 5, 2024
1 parent 0d736a4 commit a326c0e
Show file tree
Hide file tree
Showing 22 changed files with 74 additions and 45 deletions.
1 change: 0 additions & 1 deletion core/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ dev = [
"pytest-freezer == 0.4.8",
"setuptools-scm == 8.1.0",
"tox == 4.15.0",
"types-Pillow == 10.1.0.2", # TODO:PR: does this help anything?
# typing-extensions needed for TypeAlias added in Py 3.10
"typing-extensions == 4.9.0 ; python_version < '3.10'",
]
Expand Down
6 changes: 3 additions & 3 deletions core/src/toga/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import sys
import warnings
import webbrowser
from collections.abc import Iterator, Set
from collections.abc import Iterator
from email.message import Message
from pathlib import Path
from typing import TYPE_CHECKING, Any, MutableSet, Protocol
Expand Down Expand Up @@ -100,7 +100,7 @@ def discard(self, window: Window) -> None:
# 2023-10: Backwards compatibility
######################################################################

def __iadd__(self, window: Set[Any]) -> WindowSet:
def __iadd__(self, window: Window) -> WindowSet:
# The standard set type does not have a += operator.
warn(
"Windows are automatically associated with the app; += is not required",
Expand All @@ -109,7 +109,7 @@ def __iadd__(self, window: Set[Any]) -> WindowSet:
)
return self

def __isub__(self, other: Set[Any]) -> WindowSet:
def __isub__(self, other: Window) -> WindowSet:
# The standard set type does have a -= operator, but it takes sets rather than
# individual items.
warn(
Expand Down
8 changes: 6 additions & 2 deletions core/src/toga/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,12 @@ def __eq__(self, other: object) -> bool:


class CommandSetChangeHandler(Protocol):
def __call__(self, /) -> object:
"""A handler that will be invoked when a Command or Group is added to the CommandSet."""
def __call__(self, **kwargs) -> object:
"""A handler that will be invoked when a Command or Group is added to the
CommandSet.
:param kwargs: Ensures compatibility with arguments added in future versions.
"""


class CommandSet:
Expand Down
2 changes: 1 addition & 1 deletion core/src/toga/hardware/location.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def __call__(
location: toga.LatLng,
altitude: float | None,
**kwargs: Any,
) -> None:
) -> object:
"""A handler that will be invoked when the user's location changes.
:param service: the location service that generated the update.
Expand Down
2 changes: 1 addition & 1 deletion core/src/toga/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
ImageContent: TypeAlias = PathLike | BytesLike | ImageLike

# Define a type variable representing an image of an externally defined type.
ExternalImageT = TypeVar("ExternalImageT", bound=object)
ExternalImageT = TypeVar("ExternalImageT")


class ImageConverter(Protocol):
Expand Down
10 changes: 8 additions & 2 deletions core/src/toga/widgets/canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -1174,7 +1174,7 @@ def color(self, value: object) -> None:


class OnTouchHandler(Protocol):
def __call__(self, widget: Canvas, x: int, y: int, **kwargs: Any) -> None:
def __call__(self, widget: Canvas, x: int, y: int, **kwargs: Any) -> object:
"""A handler that will be invoked when a :any:`Canvas` is touched with a finger
or mouse.
Expand All @@ -1186,7 +1186,13 @@ def __call__(self, widget: Canvas, x: int, y: int, **kwargs: Any) -> None:


class OnResizeHandler(Protocol):
def __call__(self, widget: Canvas, width: int, height: int, **kwargs: Any) -> None:
def __call__(
self,
widget: Canvas,
width: int,
height: int,
**kwargs: Any,
) -> object:
"""A handler that will be invoked when a :any:`Canvas` is resized.
:param widget: The canvas that was resized.
Expand Down
3 changes: 2 additions & 1 deletion core/src/toga/widgets/dateinput.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@


class OnChangeHandler(Protocol):
def __call__(self, **kwargs: Any) -> None:
def __call__(self, widget: DateInput, **kwargs: Any) -> object:
"""A handler that will be invoked when a change occurs.
:param widget: The DateInput that was changed.
:param kwargs: Ensures compatibility with arguments added in future versions.
"""

Expand Down
14 changes: 9 additions & 5 deletions core/src/toga/widgets/detailedlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,35 +21,39 @@


class OnPrimaryActionHandler(Protocol):
def __call__(self, row: Any, **kwargs: Any) -> object:
def __call__(self, widget: DetailedList, row: Any, **kwargs: Any) -> object:
"""A handler to invoke for the primary action.
:param widget: The button that was pressed.
:param widget: The DetailedList that was invoked.
:param row: The current row for the detailed list.
:param kwargs: Ensures compatibility with arguments added in future versions.
"""


class OnSecondaryActionHandler(Protocol):
def __call__(self, row: Any, **kwargs: Any) -> object:
def __call__(self, widget: DetailedList, row: Any, **kwargs: Any) -> object:
"""A handler to invoke for the secondary action.
:param widget: The DetailedList that was invoked.
:param row: The current row for the detailed list.
:param kwargs: Ensures compatibility with arguments added in future versions.
"""


class OnRefreshHandler(Protocol):
def __call__(self, **kwargs: Any) -> object:
def __call__(self, widget: DetailedList, **kwargs: Any) -> object:
"""A handler to invoke when the detailed list is refreshed.
:param widget: The DetailedList that was refreshed.
:param kwargs: Ensures compatibility with arguments added in future versions.
"""


class OnSelectHandler(Protocol):
def __call__(self, **kwargs: Any) -> object:
def __call__(self, widget: DetailedList, **kwargs: Any) -> object:
"""A handler to invoke when the detailed list is selected.
:param widget: The DetailedList that was selected.
:param kwargs: Ensures compatibility with arguments added in future versions.
"""

Expand Down
2 changes: 1 addition & 1 deletion core/src/toga/widgets/mapview.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class OnSelectHandler(Protocol):
def __call__(self, widget: MapView, *, pin: MapPin, **kwargs: Any) -> object:
"""A handler that will be invoked when the user selects a map pin.
:param widget: The button that was pressed.
:param widget: The MapView that was selected.
:param pin: The pin that was selected.
:param kwargs: Ensures compatibility with arguments added in future versions.
"""
Expand Down
3 changes: 2 additions & 1 deletion core/src/toga/widgets/multilinetextinput.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@


class OnChangeHandler(Protocol):
def __call__(self, **kwargs: Any) -> object:
def __call__(self, widget: MultilineTextInput, **kwargs: Any) -> object:
"""A handler to invoke when the value is changed.
:param widget: The MultilineTextInput that was changed.
:param kwargs: Ensures compatibility with arguments added in future versions.
"""

Expand Down
3 changes: 2 additions & 1 deletion core/src/toga/widgets/numberinput.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@ def _clean_decimal_str(value: str) -> str:


class OnChangeHandler(Protocol):
def __call__(self, **kwargs: Any) -> object:
def __call__(self, widget: NumberInput, **kwargs: Any) -> object:
"""A handler to invoke when the value is changed.
:param widget: The NumberInput that was changed.
:param kwargs: Ensures compatibility with arguments added in future versions.
"""

Expand Down
14 changes: 5 additions & 9 deletions core/src/toga/widgets/optioncontainer.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
from __future__ import annotations

from collections.abc import Iterable
from typing import (
TYPE_CHECKING,
Any,
Protocol,
overload,
)
from typing import TYPE_CHECKING, Any, Protocol, overload

import toga
from toga.handlers import wrapped_handler
Expand All @@ -27,9 +22,10 @@


class OnSelectHandler(Protocol):
def __call__(self, **kwargs: Any) -> object:
"""A handler to invoke when the option list is selected.
def __call__(self, widget: OptionContainer, **kwargs: Any) -> None:
"""A handler that will be invoked when a new tab is selected in the OptionContainer.
:param widget: The OptionContainer that had a selection change.
:param kwargs: Ensures compatibility with arguments added in future versions.
"""

Expand Down Expand Up @@ -380,7 +376,7 @@ def __init__(
self,
id: str | None = None,
style: StyleT | None = None,
content: Iterable[tuple[str, Widget]] | None = None,
content: Iterable[OptionContainerContent] | None = None,
on_select: toga.widgets.optioncontainer.OnSelectHandler | None = None,
):
"""Create a new OptionContainer.
Expand Down
3 changes: 2 additions & 1 deletion core/src/toga/widgets/scrollcontainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@


class OnScrollHandler(Protocol):
def __call__(self, **kwargs: Any) -> object:
def __call__(self, widget: ScrollContainer, **kwargs: Any) -> object:
"""A handler to invoke when the container is scrolled.
:param widget: The ScrollContainer that was scrolled.
:param kwargs: Ensures compatibility with arguments added in future versions.
"""

Expand Down
3 changes: 2 additions & 1 deletion core/src/toga/widgets/selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@


class OnChangeHandler(Protocol):
def __call__(self, **kwargs: Any) -> object:
def __call__(self, widget: Selection, **kwargs: Any) -> object:
"""A handler to invoke when the value is changed.
:param widget: The Selection that was changed.
:param kwargs: Ensures compatibility with arguments added in future versions.
"""

Expand Down
9 changes: 6 additions & 3 deletions core/src/toga/widgets/slider.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,28 @@


class OnChangeHandler(Protocol):
def __call__(self, **kwargs: Any) -> object:
def __call__(self, widget: Slider, **kwargs: Any) -> object:
"""A handler to invoke when the value is changed.
:param widget: The Slider that was changed.
:param kwargs: Ensures compatibility with arguments added in future versions.
"""


class OnPressHandler(Protocol):
def __call__(self, **kwargs) -> object:
def __call__(self, widget: Slider, **kwargs) -> object:
"""A handler to invoke when the slider is pressed.
:param widget: The Slider that was pressed.
:param kwargs: Ensures compatibility with arguments added in future versions.
"""


class OnReleaseHandler(Protocol):
def __call__(self, **kwargs: Any) -> object:
def __call__(self, widget: Slider, **kwargs: Any) -> object:
"""A handler to invoke when the slider is pressed.
:param widget: The Slider that was released.
:param kwargs: Ensures compatibility with arguments added in future versions.
"""

Expand Down
3 changes: 2 additions & 1 deletion core/src/toga/widgets/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@


class OnChangeHandler(Protocol):
def __call__(self, **kwargs: Any) -> object:
def __call__(self, widget: Switch, **kwargs: Any) -> object:
"""A handler to invoke when the value is changed.
:param widget: The Switch that was changed.
:param kwargs: Ensures compatibility with arguments added in future versions.
"""

Expand Down
7 changes: 5 additions & 2 deletions core/src/toga/widgets/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,20 @@


class OnSelectHandler(Protocol):
def __call__(self, **kwargs: Any) -> object:
def __call__(self, widget: Table, **kwargs: Any) -> object:
"""A handler to invoke when the table is selected.
:param widget: The Table that was selected.
:param kwargs: Ensures compatibility with arguments added in future versions.
"""


class OnActivateHandler(Protocol):
def __call__(self, row: Any, **kwargs: Any) -> object:
def __call__(self, widget: Table, row: Any, **kwargs: Any) -> object:
"""A handler to invoke when the table is activated.
:param widget: The Table that was activated.
:param row: The Table Row that was activated.
:param kwargs: Ensures compatibility with arguments added in future versions.
"""

Expand Down
12 changes: 8 additions & 4 deletions core/src/toga/widgets/textinput.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,37 @@


class OnChangeHandler(Protocol):
def __call__(self, **kwargs: Any) -> object:
def __call__(self, widget: TextInput, **kwargs: Any) -> object:
"""A handler to invoke when the text input is changed.
:param widget: The TextInput that was changed.
:param kwargs: Ensures compatibility with arguments added in future versions.
"""


class OnConfirmHandler(Protocol):
def __call__(self, **kwargs: Any) -> object:
def __call__(self, widget: TextInput, **kwargs: Any) -> object:
"""A handler to invoke when the text input is confirmed.
:param widget: The TextInput that was confirmed.
:param kwargs: Ensures compatibility with arguments added in future versions.
"""


class OnGainFocusHandler(Protocol):
def __call__(self, **kwargs: Any) -> object:
def __call__(self, widget: TextInput, **kwargs: Any) -> object:
"""A handler to invoke when the text input gains focus.
:param widget: The TextInput that gained focus.
:param kwargs: Ensures compatibility with arguments added in future versions.
"""


class OnLoseFocusHandler(Protocol):
def __call__(self, **kwargs: Any) -> object:
def __call__(self, widget: TextInput, **kwargs: Any) -> object:
"""A handler to invoke when the text input loses focus.
:param widget: The TextInput that lost focus.
:param kwargs: Ensures compatibility with arguments added in future versions.
"""

Expand Down
3 changes: 2 additions & 1 deletion core/src/toga/widgets/timeinput.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@


class OnChangeHandler(Protocol):
def __call__(self, **kwargs: Any) -> object:
def __call__(self, widget: TimeInput, **kwargs: Any) -> object:
"""A handler to invoke when the time input is changed.
:param widget: The TimeInput that was changed.
:param kwargs: Ensures compatibility with arguments added in future versions.
"""

Expand Down
6 changes: 4 additions & 2 deletions core/src/toga/widgets/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,19 @@


class OnSelectHandler(Protocol):
def __call__(self, **kwargs: Any) -> object:
def __call__(self, widget: Tree, **kwargs: Any) -> object:
"""A handler to invoke when the tree is selected.
:param widget: The Tree that was selected.
:param kwargs: Ensures compatibility with arguments added in future versions.
"""


class OnActivateHandler(Protocol):
def __call__(self, **kwargs: Any) -> object:
def __call__(self, widget: Tree, **kwargs: Any) -> object:
"""A handler to invoke when the tree is activated.
:param widget: The Tree that was activated.
:param kwargs: Ensures compatibility with arguments added in future versions.
"""

Expand Down
3 changes: 2 additions & 1 deletion core/src/toga/widgets/webview.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ class JavaScriptResult(AsyncResult):


class OnWebViewLoadHandler(Protocol):
def __call__(self, **kwargs: Any) -> object:
def __call__(self, widget: WebView, **kwargs: Any) -> object:
"""A handler to invoke when the WebView is loaded.
:param widget: The WebView that was loaded.
:param kwargs: Ensures compatibility with arguments added in future versions.
"""

Expand Down
Loading

0 comments on commit a326c0e

Please sign in to comment.