diff --git a/src/textual/_immutable_sequence_view.py b/src/textual/_immutable_sequence_view.py index 2f01ddd2d5..823c73e6a4 100644 --- a/src/textual/_immutable_sequence_view.py +++ b/src/textual/_immutable_sequence_view.py @@ -3,7 +3,7 @@ from __future__ import annotations from sys import maxsize -from typing import Generic, Iterator, Sequence, TypeVar, overload +from typing import TYPE_CHECKING, Generic, Iterator, Sequence, TypeVar, overload T = TypeVar("T") @@ -19,11 +19,13 @@ def __init__(self, wrap: Sequence[T]) -> None: """ self._wrap = wrap - @overload - def __getitem__(self, index: int) -> T: ... + if TYPE_CHECKING: - @overload - def __getitem__(self, index: slice) -> ImmutableSequenceView[T]: ... + @overload + def __getitem__(self, index: int) -> T: ... + + @overload + def __getitem__(self, index: slice) -> ImmutableSequenceView[T]: ... def __getitem__(self, index: int | slice) -> T | ImmutableSequenceView[T]: return ( diff --git a/src/textual/_node_list.py b/src/textual/_node_list.py index ac9b425f32..198558777d 100644 --- a/src/textual/_node_list.py +++ b/src/textual/_node_list.py @@ -157,11 +157,13 @@ def __iter__(self) -> Iterator[Widget]: def __reversed__(self) -> Iterator[Widget]: return reversed(self._nodes) - @overload - def __getitem__(self, index: int) -> Widget: ... + if TYPE_CHECKING: - @overload - def __getitem__(self, index: slice) -> list[Widget]: ... + @overload + def __getitem__(self, index: int) -> Widget: ... + + @overload + def __getitem__(self, index: slice) -> list[Widget]: ... def __getitem__(self, index: int | slice) -> Widget | list[Widget]: return self._nodes[index] diff --git a/src/textual/_work_decorator.py b/src/textual/_work_decorator.py index 71bd67ac1e..9863d08662 100644 --- a/src/textual/_work_decorator.py +++ b/src/textual/_work_decorator.py @@ -33,42 +33,42 @@ class WorkerDeclarationError(Exception): """An error in the declaration of a worker method.""" -@overload -def work( - method: Callable[FactoryParamSpec, Coroutine[None, None, ReturnType]], - *, - name: str = "", - group: str = "default", - exit_on_error: bool = True, - exclusive: bool = False, - description: str | None = None, - thread: bool = False, -) -> Callable[FactoryParamSpec, "Worker[ReturnType]"]: ... - - -@overload -def work( - method: Callable[FactoryParamSpec, ReturnType], - *, - name: str = "", - group: str = "default", - exit_on_error: bool = True, - exclusive: bool = False, - description: str | None = None, - thread: bool = False, -) -> Callable[FactoryParamSpec, "Worker[ReturnType]"]: ... - +if TYPE_CHECKING: -@overload -def work( - *, - name: str = "", - group: str = "default", - exit_on_error: bool = True, - exclusive: bool = False, - description: str | None = None, - thread: bool = False, -) -> Decorator[..., ReturnType]: ... + @overload + def work( + method: Callable[FactoryParamSpec, Coroutine[None, None, ReturnType]], + *, + name: str = "", + group: str = "default", + exit_on_error: bool = True, + exclusive: bool = False, + description: str | None = None, + thread: bool = False, + ) -> Callable[FactoryParamSpec, "Worker[ReturnType]"]: ... + + @overload + def work( + method: Callable[FactoryParamSpec, ReturnType], + *, + name: str = "", + group: str = "default", + exit_on_error: bool = True, + exclusive: bool = False, + description: str | None = None, + thread: bool = False, + ) -> Callable[FactoryParamSpec, "Worker[ReturnType]"]: ... + + @overload + def work( + *, + name: str = "", + group: str = "default", + exit_on_error: bool = True, + exclusive: bool = False, + description: str | None = None, + thread: bool = False, + ) -> Decorator[..., ReturnType]: ... def work( @@ -103,7 +103,7 @@ def decorator( method: ( Callable[DecoratorParamSpec, ReturnType] | Callable[DecoratorParamSpec, Coroutine[None, None, ReturnType]] - ) + ), ) -> Callable[DecoratorParamSpec, Worker[ReturnType]]: """The decorator.""" diff --git a/src/textual/app.py b/src/textual/app.py index eb1d1e8978..f34ca20370 100644 --- a/src/textual/app.py +++ b/src/textual/app.py @@ -1680,11 +1680,15 @@ def render(self) -> RenderResult: ExpectType = TypeVar("ExpectType", bound=Widget) - @overload - def get_child_by_id(self, id: str) -> Widget: ... + if TYPE_CHECKING: - @overload - def get_child_by_id(self, id: str, expect_type: type[ExpectType]) -> ExpectType: ... + @overload + def get_child_by_id(self, id: str) -> Widget: ... + + @overload + def get_child_by_id( + self, id: str, expect_type: type[ExpectType] + ) -> ExpectType: ... def get_child_by_id( self, id: str, expect_type: type[ExpectType] | None = None @@ -1709,13 +1713,15 @@ def get_child_by_id( else self.screen.get_child_by_id(id, expect_type) ) - @overload - def get_widget_by_id(self, id: str) -> Widget: ... + if TYPE_CHECKING: - @overload - def get_widget_by_id( - self, id: str, expect_type: type[ExpectType] - ) -> ExpectType: ... + @overload + def get_widget_by_id(self, id: str) -> Widget: ... + + @overload + def get_widget_by_id( + self, id: str, expect_type: type[ExpectType] + ) -> ExpectType: ... def get_widget_by_id( self, id: str, expect_type: type[ExpectType] | None = None @@ -2044,21 +2050,23 @@ def _replace_screen(self, screen: Screen) -> Screen: self.log.system(f"{screen} REMOVED") return screen - @overload - def push_screen( - self, - screen: Screen[ScreenResultType] | str, - callback: ScreenResultCallbackType[ScreenResultType] | None = None, - wait_for_dismiss: Literal[False] = False, - ) -> AwaitMount: ... + if TYPE_CHECKING: - @overload - def push_screen( - self, - screen: Screen[ScreenResultType] | str, - callback: ScreenResultCallbackType[ScreenResultType] | None = None, - wait_for_dismiss: Literal[True] = True, - ) -> asyncio.Future[ScreenResultType]: ... + @overload + def push_screen( + self, + screen: Screen[ScreenResultType] | str, + callback: ScreenResultCallbackType[ScreenResultType] | None = None, + wait_for_dismiss: Literal[False] = False, + ) -> AwaitMount: ... + + @overload + def push_screen( + self, + screen: Screen[ScreenResultType] | str, + callback: ScreenResultCallbackType[ScreenResultType] | None = None, + wait_for_dismiss: Literal[True] = True, + ) -> asyncio.Future[ScreenResultType]: ... def push_screen( self, @@ -2120,13 +2128,15 @@ def push_screen( else: return await_mount - @overload - async def push_screen_wait( - self, screen: Screen[ScreenResultType] - ) -> ScreenResultType: ... + if TYPE_CHECKING: + + @overload + async def push_screen_wait( + self, screen: Screen[ScreenResultType] + ) -> ScreenResultType: ... - @overload - async def push_screen_wait(self, screen: str) -> Any: ... + @overload + async def push_screen_wait(self, screen: str) -> Any: ... async def push_screen_wait( self, screen: Screen[ScreenResultType] | str diff --git a/src/textual/cache.py b/src/textual/cache.py index aa368e137b..9186df7cda 100644 --- a/src/textual/cache.py +++ b/src/textual/cache.py @@ -8,7 +8,7 @@ from __future__ import annotations -from typing import Dict, Generic, KeysView, TypeVar, overload +from typing import TYPE_CHECKING, Dict, Generic, KeysView, TypeVar, overload CacheKey = TypeVar("CacheKey") CacheValue = TypeVar("CacheValue") @@ -127,13 +127,15 @@ def set(self, key: CacheKey, value: CacheValue) -> None: __setitem__ = set - @overload - def get(self, key: CacheKey) -> CacheValue | None: ... + if TYPE_CHECKING: - @overload - def get( - self, key: CacheKey, default: DefaultValue - ) -> CacheValue | DefaultValue: ... + @overload + def get(self, key: CacheKey) -> CacheValue | None: ... + + @overload + def get( + self, key: CacheKey, default: DefaultValue + ) -> CacheValue | DefaultValue: ... def get( self, key: CacheKey, default: DefaultValue | None = None @@ -267,13 +269,15 @@ def set(self, key: CacheKey, value: CacheValue) -> None: __setitem__ = set - @overload - def get(self, key: CacheKey) -> CacheValue | None: ... + if TYPE_CHECKING: - @overload - def get( - self, key: CacheKey, default: DefaultValue - ) -> CacheValue | DefaultValue: ... + @overload + def get(self, key: CacheKey) -> CacheValue | None: ... + + @overload + def get( + self, key: CacheKey, default: DefaultValue + ) -> CacheValue | DefaultValue: ... def get( self, key: CacheKey, default: DefaultValue | None = None diff --git a/src/textual/css/query.py b/src/textual/css/query.py index 649738d3a1..bc91f5253d 100644 --- a/src/textual/css/query.py +++ b/src/textual/css/query.py @@ -145,11 +145,13 @@ def __iter__(self) -> Iterator[QueryType]: def __reversed__(self) -> Iterator[QueryType]: return reversed(self.nodes) - @overload - def __getitem__(self, index: int) -> QueryType: ... + if TYPE_CHECKING: - @overload - def __getitem__(self, index: slice) -> list[QueryType]: ... + @overload + def __getitem__(self, index: int) -> QueryType: ... + + @overload + def __getitem__(self, index: slice) -> list[QueryType]: ... def __getitem__(self, index: int | slice) -> QueryType | list[QueryType]: return self.nodes[index] @@ -208,11 +210,13 @@ def exclude(self, selector: str) -> DOMQuery[QueryType]: parent=self, ) - @overload - def first(self) -> QueryType: ... + if TYPE_CHECKING: + + @overload + def first(self) -> QueryType: ... - @overload - def first(self, expect_type: type[ExpectType]) -> ExpectType: ... + @overload + def first(self, expect_type: type[ExpectType]) -> ExpectType: ... def first( self, expect_type: type[ExpectType] | None = None @@ -242,11 +246,13 @@ def first( else: raise NoMatches(f"No nodes match {self!r} on {self.node!r}") - @overload - def only_one(self) -> QueryType: ... + if TYPE_CHECKING: + + @overload + def only_one(self) -> QueryType: ... - @overload - def only_one(self, expect_type: type[ExpectType]) -> ExpectType: ... + @overload + def only_one(self, expect_type: type[ExpectType]) -> ExpectType: ... def only_one( self, expect_type: type[ExpectType] | None = None @@ -287,11 +293,13 @@ def only_one( pass return the_one - @overload - def last(self) -> QueryType: ... + if TYPE_CHECKING: - @overload - def last(self, expect_type: type[ExpectType]) -> ExpectType: ... + @overload + def last(self) -> QueryType: ... + + @overload + def last(self, expect_type: type[ExpectType]) -> ExpectType: ... def last( self, expect_type: type[ExpectType] | None = None @@ -318,11 +326,13 @@ def last( ) return last - @overload - def results(self) -> Iterator[QueryType]: ... + if TYPE_CHECKING: + + @overload + def results(self) -> Iterator[QueryType]: ... - @overload - def results(self, filter_type: type[ExpectType]) -> Iterator[ExpectType]: ... + @overload + def results(self, filter_type: type[ExpectType]) -> Iterator[ExpectType]: ... def results( self, filter_type: type[ExpectType] | None = None diff --git a/src/textual/document/_document.py b/src/textual/document/_document.py index 4a63ff2054..386a6a7713 100644 --- a/src/textual/document/_document.py +++ b/src/textual/document/_document.py @@ -182,11 +182,13 @@ def start(self) -> Location: def end(self) -> Location: """Returns the location of the end of the document.""" - @overload - def __getitem__(self, line_index: int) -> str: ... + if TYPE_CHECKING: - @overload - def __getitem__(self, line_index: slice) -> list[str]: ... + @overload + def __getitem__(self, line_index: int) -> str: ... + + @overload + def __getitem__(self, line_index: slice) -> list[str]: ... @abstractmethod def __getitem__(self, line_index: int | slice) -> str | list[str]: diff --git a/src/textual/dom.py b/src/textual/dom.py index c8243d1edd..340217d0be 100644 --- a/src/textual/dom.py +++ b/src/textual/dom.py @@ -1183,24 +1183,26 @@ def _add_children(self, *nodes: Widget) -> None: WalkType = TypeVar("WalkType", bound="DOMNode") - @overload - def walk_children( - self, - filter_type: type[WalkType], - *, - with_self: bool = False, - method: WalkMethod = "depth", - reverse: bool = False, - ) -> list[WalkType]: ... + if TYPE_CHECKING: - @overload - def walk_children( - self, - *, - with_self: bool = False, - method: WalkMethod = "depth", - reverse: bool = False, - ) -> list[DOMNode]: ... + @overload + def walk_children( + self, + filter_type: type[WalkType], + *, + with_self: bool = False, + method: WalkMethod = "depth", + reverse: bool = False, + ) -> list[WalkType]: ... + + @overload + def walk_children( + self, + *, + with_self: bool = False, + method: WalkMethod = "depth", + reverse: bool = False, + ) -> list[DOMNode]: ... def walk_children( self, @@ -1236,11 +1238,13 @@ def walk_children( nodes.reverse() return cast("list[DOMNode]", nodes) - @overload - def query(self, selector: str | None = None) -> DOMQuery[Widget]: ... + if TYPE_CHECKING: + + @overload + def query(self, selector: str | None = None) -> DOMQuery[Widget]: ... - @overload - def query(self, selector: type[QueryType]) -> DOMQuery[QueryType]: ... + @overload + def query(self, selector: type[QueryType]) -> DOMQuery[QueryType]: ... def query( self, selector: str | type[QueryType] | None = None @@ -1261,11 +1265,13 @@ def query( else: return DOMQuery[QueryType](self, filter=selector.__name__) - @overload - def query_children(self, selector: str | None = None) -> DOMQuery[Widget]: ... + if TYPE_CHECKING: - @overload - def query_children(self, selector: type[QueryType]) -> DOMQuery[QueryType]: ... + @overload + def query_children(self, selector: str | None = None) -> DOMQuery[Widget]: ... + + @overload + def query_children(self, selector: type[QueryType]) -> DOMQuery[QueryType]: ... def query_children( self, selector: str | type[QueryType] | None = None @@ -1290,14 +1296,18 @@ def query_children( else: return DOMQuery[QueryType](self, deep=False, filter=selector.__name__) - @overload - def query_one(self, selector: str) -> Widget: ... + if TYPE_CHECKING: + + @overload + def query_one(self, selector: str) -> Widget: ... - @overload - def query_one(self, selector: type[QueryType]) -> QueryType: ... + @overload + def query_one(self, selector: type[QueryType]) -> QueryType: ... - @overload - def query_one(self, selector: str, expect_type: type[QueryType]) -> QueryType: ... + @overload + def query_one( + self, selector: str, expect_type: type[QueryType] + ) -> QueryType: ... def query_one( self, diff --git a/src/textual/reactive.py b/src/textual/reactive.py index c9b153a743..fb0224dad0 100644 --- a/src/textual/reactive.py +++ b/src/textual/reactive.py @@ -221,15 +221,19 @@ def __set_name__(self, owner: Type[MessageTarget], name: str) -> None: default = self._default setattr(owner, f"_default_{name}", default) - @overload - def __get__( - self: Reactive[ReactiveType], obj: ReactableType, obj_type: type[ReactableType] - ) -> ReactiveType: ... - - @overload - def __get__( - self: Reactive[ReactiveType], obj: None, obj_type: type[ReactableType] - ) -> Reactive[ReactiveType]: ... + if TYPE_CHECKING: + + @overload + def __get__( + self: Reactive[ReactiveType], + obj: ReactableType, + obj_type: type[ReactableType], + ) -> ReactiveType: ... + + @overload + def __get__( + self: Reactive[ReactiveType], obj: None, obj_type: type[ReactableType] + ) -> Reactive[ReactiveType]: ... def __get__( self: Reactive[ReactiveType], diff --git a/src/textual/walk.py b/src/textual/walk.py index 759649be3b..0f6790c882 100644 --- a/src/textual/walk.py +++ b/src/textual/walk.py @@ -17,21 +17,22 @@ WalkType = TypeVar("WalkType", bound=DOMNode) -@overload -def walk_depth_first( - root: DOMNode, - *, - with_root: bool = True, -) -> Iterable[DOMNode]: ... +if TYPE_CHECKING: + @overload + def walk_depth_first( + root: DOMNode, + *, + with_root: bool = True, + ) -> Iterable[DOMNode]: ... -@overload -def walk_depth_first( - root: WalkType, - filter_type: type[WalkType], - *, - with_root: bool = True, -) -> Iterable[WalkType]: ... + @overload + def walk_depth_first( + root: WalkType, + filter_type: type[WalkType], + *, + with_root: bool = True, + ) -> Iterable[WalkType]: ... def walk_depth_first( @@ -74,21 +75,22 @@ def walk_depth_first( push(iter(children)) -@overload -def walk_breadth_first( - root: DOMNode, - *, - with_root: bool = True, -) -> Iterable[DOMNode]: ... - +if TYPE_CHECKING: -@overload -def walk_breadth_first( - root: WalkType, - filter_type: type[WalkType], - *, - with_root: bool = True, -) -> Iterable[WalkType]: ... + @overload + def walk_breadth_first( + root: DOMNode, + *, + with_root: bool = True, + ) -> Iterable[DOMNode]: ... + + @overload + def walk_breadth_first( + root: WalkType, + filter_type: type[WalkType], + *, + with_root: bool = True, + ) -> Iterable[WalkType]: ... def walk_breadth_first( diff --git a/src/textual/widget.py b/src/textual/widget.py index 5c701695c3..5d14903d3d 100644 --- a/src/textual/widget.py +++ b/src/textual/widget.py @@ -686,11 +686,15 @@ async def _watch_loading(self, loading: bool) -> None: ExpectType = TypeVar("ExpectType", bound="Widget") - @overload - def get_child_by_id(self, id: str) -> Widget: ... + if TYPE_CHECKING: - @overload - def get_child_by_id(self, id: str, expect_type: type[ExpectType]) -> ExpectType: ... + @overload + def get_child_by_id(self, id: str) -> Widget: ... + + @overload + def get_child_by_id( + self, id: str, expect_type: type[ExpectType] + ) -> ExpectType: ... def get_child_by_id( self, id: str, expect_type: type[ExpectType] | None = None @@ -720,13 +724,15 @@ def get_child_by_id( ) return child - @overload - def get_widget_by_id(self, id: str) -> Widget: ... + if TYPE_CHECKING: - @overload - def get_widget_by_id( - self, id: str, expect_type: type[ExpectType] - ) -> ExpectType: ... + @overload + def get_widget_by_id(self, id: str) -> Widget: ... + + @overload + def get_widget_by_id( + self, id: str, expect_type: type[ExpectType] + ) -> ExpectType: ... def get_widget_by_id( self, id: str, expect_type: type[ExpectType] | None = None @@ -1010,23 +1016,25 @@ def mount_all( await_mount = self.mount(*widgets, before=before, after=after) return await_mount - @overload - def move_child( - self, - child: int | Widget, - *, - before: int | Widget, - after: None = None, - ) -> None: ... - - @overload - def move_child( - self, - child: int | Widget, - *, - after: int | Widget, - before: None = None, - ) -> None: ... + if TYPE_CHECKING: + + @overload + def move_child( + self, + child: int | Widget, + *, + before: int | Widget, + after: None = None, + ) -> None: ... + + @overload + def move_child( + self, + child: int | Widget, + *, + after: int | Widget, + before: None = None, + ) -> None: ... def move_child( self,