Skip to content

Commit

Permalink
Merge pull request #4303 from Textualize/will-typing
Browse files Browse the repository at this point in the history
typing
  • Loading branch information
willmcgugan authored Mar 19, 2024
2 parents d3df9d0 + ba92c57 commit 88896ba
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/textual/_styles_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ def post(segments: Iterable[Segment]) -> Iterable[Segment]:
has_left,
has_right,
label_segments,
label_alignment,
label_alignment, # type: ignore
)

# Draw padding (B)
Expand Down
4 changes: 2 additions & 2 deletions src/textual/_two_way_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def __delitem__(self, key: Key) -> None:
def __iter__(self):
return iter(self._forward)

def get(self, key: Key) -> Value:
def get(self, key: Key) -> Value | None:
"""Given a key, efficiently lookup and return the associated value.
Args:
Expand All @@ -43,7 +43,7 @@ def get(self, key: Key) -> Value:
"""
return self._forward.get(key)

def get_key(self, value: Value) -> Key:
def get_key(self, value: Value) -> Key | None:
"""Given a value, efficiently lookup and return the associated key.
Args:
Expand Down
19 changes: 6 additions & 13 deletions src/textual/_widget_navigation.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@

from functools import partial
from itertools import count
from typing import TYPE_CHECKING, Literal, Protocol, Sequence
from typing import Literal, Protocol, Sequence

from typing_extensions import TypeAlias, TypeVar

if TYPE_CHECKING:
from .widget import Widget
from typing_extensions import TypeAlias


class Disableable(Protocol):
Expand All @@ -30,8 +27,6 @@ class Disableable(Protocol):
In a vertical setting, 1 points down and -1 points up.
In a horizontal setting, 1 points right and -1 points left.
"""
_DisableableType = TypeVar("_DisableableType", bound=Disableable)
_WidgetType = TypeVar("_WidgetType", bound="Widget")


def get_directed_distance(
Expand Down Expand Up @@ -70,7 +65,7 @@ def get_directed_distance(


def find_first_enabled(
candidates: Sequence[_DisableableType] | Sequence[_WidgetType],
candidates: Sequence[Disableable],
) -> int | None:
"""Find the first enabled candidate in a sequence of possibly-disabled objects.
Expand All @@ -86,9 +81,7 @@ def find_first_enabled(
)


def find_last_enabled(
candidates: Sequence[_DisableableType] | Sequence[_WidgetType],
) -> int | None:
def find_last_enabled(candidates: Sequence[Disableable]) -> int | None:
"""Find the last enabled candidate in a sequence of possibly-disabled objects.
Args:
Expand All @@ -109,7 +102,7 @@ def find_last_enabled(


def find_next_enabled(
candidates: Sequence[_DisableableType] | Sequence[_WidgetType],
candidates: Sequence[Disableable],
anchor: int | None,
direction: Direction,
with_anchor: bool = False,
Expand Down Expand Up @@ -155,7 +148,7 @@ def find_next_enabled(


def find_next_enabled_no_wrap(
candidates: Sequence[_DisableableType] | Sequence[_WidgetType],
candidates: Sequence[Disableable],
anchor: int | None,
direction: Direction,
with_anchor: bool = False,
Expand Down
11 changes: 7 additions & 4 deletions src/textual/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from importlib.metadata import version
from pathlib import Path
from typing import cast

from rich import box
from rich.console import RenderableType
Expand Down Expand Up @@ -194,8 +195,9 @@ def compose(self) -> ComposeResult:
yield Button("Start", variant="success")

def on_button_pressed(self, event: Button.Pressed) -> None:
self.app.add_note("[b magenta]Start!")
self.app.query_one(".location-first").scroll_visible(duration=0.5, top=True)
app = cast(DemoApp, self.app)
app.add_note("[b magenta]Start!")
app.query_one(".location-first").scroll_visible(duration=0.5, top=True)


class OptionGroup(Container):
Expand Down Expand Up @@ -248,8 +250,9 @@ def __init__(self, label: str, reveal: str) -> None:
self.reveal = reveal

def on_click(self) -> None:
self.app.query_one(self.reveal).scroll_visible(top=True, duration=0.5)
self.app.add_note(f"Scrolling to [b]{self.reveal}[/b]")
app = cast(DemoApp, self.app)
app.query_one(self.reveal).scroll_visible(top=True, duration=0.5)
app.add_note(f"Scrolling to [b]{self.reveal}[/b]")


class LoginForm(Container):
Expand Down
4 changes: 2 additions & 2 deletions src/textual/widgets/_radio_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class RadioSet(Container, can_focus=True, can_focus_children=False):

BINDINGS: ClassVar[list[BindingType]] = [
Binding("down,right", "next_button", "", show=False),
Binding("enter,space", "toggle", "Toggle", show=False),
Binding("enter,space", "toggle_button", "Toggle", show=False),
Binding("up,left", "previous_button", "", show=False),
]
"""
Expand Down Expand Up @@ -265,7 +265,7 @@ def action_next_button(self) -> None:
direction=1,
)

def action_toggle(self) -> None:
def action_toggle_button(self) -> None:
"""Toggle the state of the currently-selected button."""
if self._selected is not None:
button = self._nodes[self._selected]
Expand Down
3 changes: 2 additions & 1 deletion src/textual/widgets/_selection_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from rich.text import Text, TextType
from typing_extensions import Self

from .. import events
from ..binding import Binding
from ..messages import Message
from ..strip import Strip
Expand Down Expand Up @@ -277,7 +278,7 @@ def selected(self) -> list[SelectionType]:
"""
return list(self._selected.keys())

def _on_mount(self) -> None:
def _on_mount(self, _event: events.Mount) -> None:
"""Configure the list once the DOM is ready."""
self._send_messages = True

Expand Down
6 changes: 3 additions & 3 deletions src/textual/widgets/_tabs.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ def __init__(
"""
super().__init__(id=id, classes=classes, disabled=disabled)
self._label: Text
self.label = label
# Setter takes Text or str
self.label = label # type: ignore[assignment]

@property
def label(self) -> Text:
Expand Down Expand Up @@ -531,8 +532,7 @@ async def do_remove() -> None:
if next_tab is None or (removing_active_tab and next_tab.id is None):
self.active = ""
elif removing_active_tab:
assert next_tab.id is not None
self.active = next_tab.id
self.active = next_tab.id or ""
next_tab.add_class("-active")

highlight_updated.set()
Expand Down
4 changes: 2 additions & 2 deletions src/textual/widgets/_toggle_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class ToggleButton(Static, can_focus=True):
"""

BINDINGS: ClassVar[list[BindingType]] = [
Binding("enter,space", "toggle", "Toggle", show=False),
Binding("enter,space", "toggle_button", "Toggle", show=False),
]
"""
| Key(s) | Description |
Expand Down Expand Up @@ -238,7 +238,7 @@ def toggle(self) -> Self:
self.value = not self.value
return self

def action_toggle(self) -> None:
def action_toggle_button(self) -> None:
"""Toggle the value of the widget when called as an action.
This would normally be used for a keyboard binding.
Expand Down
8 changes: 4 additions & 4 deletions src/textual/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def __rich_repr__(self) -> rich.repr.Result:
def __init__(
self,
node: DOMNode,
work: WorkType | None = None,
work: WorkType,
*,
name: str = "",
group: str = "default",
Expand Down Expand Up @@ -316,9 +316,9 @@ def run_callable(work: Callable[[], ResultType]) -> ResultType:
else:
raise WorkerError("Unsupported attempt to run a thread worker")

return await asyncio.get_running_loop().run_in_executor(
None, runner, self._work
)
loop = asyncio.get_running_loop()
assert loop is not None
return await loop.run_in_executor(None, runner, self._work)

async def _run_async(self) -> ResultType:
"""Run an async worker.
Expand Down

0 comments on commit 88896ba

Please sign in to comment.