Skip to content

Commit 68706b0

Browse files
committed
wrap overloads
1 parent 2c97dec commit 68706b0

File tree

11 files changed

+260
-206
lines changed

11 files changed

+260
-206
lines changed

src/textual/_immutable_sequence_view.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from __future__ import annotations
44

55
from sys import maxsize
6-
from typing import Generic, Iterator, Sequence, TypeVar, overload
6+
from typing import TYPE_CHECKING, Generic, Iterator, Sequence, TypeVar, overload
77

88
T = TypeVar("T")
99

@@ -19,11 +19,13 @@ def __init__(self, wrap: Sequence[T]) -> None:
1919
"""
2020
self._wrap = wrap
2121

22-
@overload
23-
def __getitem__(self, index: int) -> T: ...
22+
if TYPE_CHECKING:
2423

25-
@overload
26-
def __getitem__(self, index: slice) -> ImmutableSequenceView[T]: ...
24+
@overload
25+
def __getitem__(self, index: int) -> T: ...
26+
27+
@overload
28+
def __getitem__(self, index: slice) -> ImmutableSequenceView[T]: ...
2729

2830
def __getitem__(self, index: int | slice) -> T | ImmutableSequenceView[T]:
2931
return (

src/textual/_node_list.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,13 @@ def __iter__(self) -> Iterator[Widget]:
157157
def __reversed__(self) -> Iterator[Widget]:
158158
return reversed(self._nodes)
159159

160-
@overload
161-
def __getitem__(self, index: int) -> Widget: ...
160+
if TYPE_CHECKING:
162161

163-
@overload
164-
def __getitem__(self, index: slice) -> list[Widget]: ...
162+
@overload
163+
def __getitem__(self, index: int) -> Widget: ...
164+
165+
@overload
166+
def __getitem__(self, index: slice) -> list[Widget]: ...
165167

166168
def __getitem__(self, index: int | slice) -> Widget | list[Widget]:
167169
return self._nodes[index]

src/textual/_work_decorator.py

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -33,42 +33,42 @@ class WorkerDeclarationError(Exception):
3333
"""An error in the declaration of a worker method."""
3434

3535

36-
@overload
37-
def work(
38-
method: Callable[FactoryParamSpec, Coroutine[None, None, ReturnType]],
39-
*,
40-
name: str = "",
41-
group: str = "default",
42-
exit_on_error: bool = True,
43-
exclusive: bool = False,
44-
description: str | None = None,
45-
thread: bool = False,
46-
) -> Callable[FactoryParamSpec, "Worker[ReturnType]"]: ...
47-
48-
49-
@overload
50-
def work(
51-
method: Callable[FactoryParamSpec, ReturnType],
52-
*,
53-
name: str = "",
54-
group: str = "default",
55-
exit_on_error: bool = True,
56-
exclusive: bool = False,
57-
description: str | None = None,
58-
thread: bool = False,
59-
) -> Callable[FactoryParamSpec, "Worker[ReturnType]"]: ...
60-
36+
if TYPE_CHECKING:
6137

62-
@overload
63-
def work(
64-
*,
65-
name: str = "",
66-
group: str = "default",
67-
exit_on_error: bool = True,
68-
exclusive: bool = False,
69-
description: str | None = None,
70-
thread: bool = False,
71-
) -> Decorator[..., ReturnType]: ...
38+
@overload
39+
def work(
40+
method: Callable[FactoryParamSpec, Coroutine[None, None, ReturnType]],
41+
*,
42+
name: str = "",
43+
group: str = "default",
44+
exit_on_error: bool = True,
45+
exclusive: bool = False,
46+
description: str | None = None,
47+
thread: bool = False,
48+
) -> Callable[FactoryParamSpec, "Worker[ReturnType]"]: ...
49+
50+
@overload
51+
def work(
52+
method: Callable[FactoryParamSpec, ReturnType],
53+
*,
54+
name: str = "",
55+
group: str = "default",
56+
exit_on_error: bool = True,
57+
exclusive: bool = False,
58+
description: str | None = None,
59+
thread: bool = False,
60+
) -> Callable[FactoryParamSpec, "Worker[ReturnType]"]: ...
61+
62+
@overload
63+
def work(
64+
*,
65+
name: str = "",
66+
group: str = "default",
67+
exit_on_error: bool = True,
68+
exclusive: bool = False,
69+
description: str | None = None,
70+
thread: bool = False,
71+
) -> Decorator[..., ReturnType]: ...
7272

7373

7474
def work(
@@ -103,7 +103,7 @@ def decorator(
103103
method: (
104104
Callable[DecoratorParamSpec, ReturnType]
105105
| Callable[DecoratorParamSpec, Coroutine[None, None, ReturnType]]
106-
)
106+
),
107107
) -> Callable[DecoratorParamSpec, Worker[ReturnType]]:
108108
"""The decorator."""
109109

src/textual/app.py

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1680,11 +1680,15 @@ def render(self) -> RenderResult:
16801680

16811681
ExpectType = TypeVar("ExpectType", bound=Widget)
16821682

1683-
@overload
1684-
def get_child_by_id(self, id: str) -> Widget: ...
1683+
if TYPE_CHECKING:
16851684

1686-
@overload
1687-
def get_child_by_id(self, id: str, expect_type: type[ExpectType]) -> ExpectType: ...
1685+
@overload
1686+
def get_child_by_id(self, id: str) -> Widget: ...
1687+
1688+
@overload
1689+
def get_child_by_id(
1690+
self, id: str, expect_type: type[ExpectType]
1691+
) -> ExpectType: ...
16881692

16891693
def get_child_by_id(
16901694
self, id: str, expect_type: type[ExpectType] | None = None
@@ -1709,13 +1713,15 @@ def get_child_by_id(
17091713
else self.screen.get_child_by_id(id, expect_type)
17101714
)
17111715

1712-
@overload
1713-
def get_widget_by_id(self, id: str) -> Widget: ...
1716+
if TYPE_CHECKING:
17141717

1715-
@overload
1716-
def get_widget_by_id(
1717-
self, id: str, expect_type: type[ExpectType]
1718-
) -> ExpectType: ...
1718+
@overload
1719+
def get_widget_by_id(self, id: str) -> Widget: ...
1720+
1721+
@overload
1722+
def get_widget_by_id(
1723+
self, id: str, expect_type: type[ExpectType]
1724+
) -> ExpectType: ...
17191725

17201726
def get_widget_by_id(
17211727
self, id: str, expect_type: type[ExpectType] | None = None
@@ -2044,21 +2050,23 @@ def _replace_screen(self, screen: Screen) -> Screen:
20442050
self.log.system(f"{screen} REMOVED")
20452051
return screen
20462052

2047-
@overload
2048-
def push_screen(
2049-
self,
2050-
screen: Screen[ScreenResultType] | str,
2051-
callback: ScreenResultCallbackType[ScreenResultType] | None = None,
2052-
wait_for_dismiss: Literal[False] = False,
2053-
) -> AwaitMount: ...
2053+
if TYPE_CHECKING:
20542054

2055-
@overload
2056-
def push_screen(
2057-
self,
2058-
screen: Screen[ScreenResultType] | str,
2059-
callback: ScreenResultCallbackType[ScreenResultType] | None = None,
2060-
wait_for_dismiss: Literal[True] = True,
2061-
) -> asyncio.Future[ScreenResultType]: ...
2055+
@overload
2056+
def push_screen(
2057+
self,
2058+
screen: Screen[ScreenResultType] | str,
2059+
callback: ScreenResultCallbackType[ScreenResultType] | None = None,
2060+
wait_for_dismiss: Literal[False] = False,
2061+
) -> AwaitMount: ...
2062+
2063+
@overload
2064+
def push_screen(
2065+
self,
2066+
screen: Screen[ScreenResultType] | str,
2067+
callback: ScreenResultCallbackType[ScreenResultType] | None = None,
2068+
wait_for_dismiss: Literal[True] = True,
2069+
) -> asyncio.Future[ScreenResultType]: ...
20622070

20632071
def push_screen(
20642072
self,
@@ -2120,13 +2128,15 @@ def push_screen(
21202128
else:
21212129
return await_mount
21222130

2123-
@overload
2124-
async def push_screen_wait(
2125-
self, screen: Screen[ScreenResultType]
2126-
) -> ScreenResultType: ...
2131+
if TYPE_CHECKING:
2132+
2133+
@overload
2134+
async def push_screen_wait(
2135+
self, screen: Screen[ScreenResultType]
2136+
) -> ScreenResultType: ...
21272137

2128-
@overload
2129-
async def push_screen_wait(self, screen: str) -> Any: ...
2138+
@overload
2139+
async def push_screen_wait(self, screen: str) -> Any: ...
21302140

21312141
async def push_screen_wait(
21322142
self, screen: Screen[ScreenResultType] | str

src/textual/cache.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from __future__ import annotations
1010

11-
from typing import Dict, Generic, KeysView, TypeVar, overload
11+
from typing import TYPE_CHECKING, Dict, Generic, KeysView, TypeVar, overload
1212

1313
CacheKey = TypeVar("CacheKey")
1414
CacheValue = TypeVar("CacheValue")
@@ -127,13 +127,15 @@ def set(self, key: CacheKey, value: CacheValue) -> None:
127127

128128
__setitem__ = set
129129

130-
@overload
131-
def get(self, key: CacheKey) -> CacheValue | None: ...
130+
if TYPE_CHECKING:
132131

133-
@overload
134-
def get(
135-
self, key: CacheKey, default: DefaultValue
136-
) -> CacheValue | DefaultValue: ...
132+
@overload
133+
def get(self, key: CacheKey) -> CacheValue | None: ...
134+
135+
@overload
136+
def get(
137+
self, key: CacheKey, default: DefaultValue
138+
) -> CacheValue | DefaultValue: ...
137139

138140
def get(
139141
self, key: CacheKey, default: DefaultValue | None = None
@@ -267,13 +269,15 @@ def set(self, key: CacheKey, value: CacheValue) -> None:
267269

268270
__setitem__ = set
269271

270-
@overload
271-
def get(self, key: CacheKey) -> CacheValue | None: ...
272+
if TYPE_CHECKING:
272273

273-
@overload
274-
def get(
275-
self, key: CacheKey, default: DefaultValue
276-
) -> CacheValue | DefaultValue: ...
274+
@overload
275+
def get(self, key: CacheKey) -> CacheValue | None: ...
276+
277+
@overload
278+
def get(
279+
self, key: CacheKey, default: DefaultValue
280+
) -> CacheValue | DefaultValue: ...
277281

278282
def get(
279283
self, key: CacheKey, default: DefaultValue | None = None

src/textual/css/query.py

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,13 @@ def __iter__(self) -> Iterator[QueryType]:
145145
def __reversed__(self) -> Iterator[QueryType]:
146146
return reversed(self.nodes)
147147

148-
@overload
149-
def __getitem__(self, index: int) -> QueryType: ...
148+
if TYPE_CHECKING:
150149

151-
@overload
152-
def __getitem__(self, index: slice) -> list[QueryType]: ...
150+
@overload
151+
def __getitem__(self, index: int) -> QueryType: ...
152+
153+
@overload
154+
def __getitem__(self, index: slice) -> list[QueryType]: ...
153155

154156
def __getitem__(self, index: int | slice) -> QueryType | list[QueryType]:
155157
return self.nodes[index]
@@ -208,11 +210,13 @@ def exclude(self, selector: str) -> DOMQuery[QueryType]:
208210
parent=self,
209211
)
210212

211-
@overload
212-
def first(self) -> QueryType: ...
213+
if TYPE_CHECKING:
214+
215+
@overload
216+
def first(self) -> QueryType: ...
213217

214-
@overload
215-
def first(self, expect_type: type[ExpectType]) -> ExpectType: ...
218+
@overload
219+
def first(self, expect_type: type[ExpectType]) -> ExpectType: ...
216220

217221
def first(
218222
self, expect_type: type[ExpectType] | None = None
@@ -242,11 +246,13 @@ def first(
242246
else:
243247
raise NoMatches(f"No nodes match {self!r} on {self.node!r}")
244248

245-
@overload
246-
def only_one(self) -> QueryType: ...
249+
if TYPE_CHECKING:
250+
251+
@overload
252+
def only_one(self) -> QueryType: ...
247253

248-
@overload
249-
def only_one(self, expect_type: type[ExpectType]) -> ExpectType: ...
254+
@overload
255+
def only_one(self, expect_type: type[ExpectType]) -> ExpectType: ...
250256

251257
def only_one(
252258
self, expect_type: type[ExpectType] | None = None
@@ -287,11 +293,13 @@ def only_one(
287293
pass
288294
return the_one
289295

290-
@overload
291-
def last(self) -> QueryType: ...
296+
if TYPE_CHECKING:
292297

293-
@overload
294-
def last(self, expect_type: type[ExpectType]) -> ExpectType: ...
298+
@overload
299+
def last(self) -> QueryType: ...
300+
301+
@overload
302+
def last(self, expect_type: type[ExpectType]) -> ExpectType: ...
295303

296304
def last(
297305
self, expect_type: type[ExpectType] | None = None
@@ -318,11 +326,13 @@ def last(
318326
)
319327
return last
320328

321-
@overload
322-
def results(self) -> Iterator[QueryType]: ...
329+
if TYPE_CHECKING:
330+
331+
@overload
332+
def results(self) -> Iterator[QueryType]: ...
323333

324-
@overload
325-
def results(self, filter_type: type[ExpectType]) -> Iterator[ExpectType]: ...
334+
@overload
335+
def results(self, filter_type: type[ExpectType]) -> Iterator[ExpectType]: ...
326336

327337
def results(
328338
self, filter_type: type[ExpectType] | None = None

src/textual/document/_document.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,13 @@ def start(self) -> Location:
182182
def end(self) -> Location:
183183
"""Returns the location of the end of the document."""
184184

185-
@overload
186-
def __getitem__(self, line_index: int) -> str: ...
185+
if TYPE_CHECKING:
187186

188-
@overload
189-
def __getitem__(self, line_index: slice) -> list[str]: ...
187+
@overload
188+
def __getitem__(self, line_index: int) -> str: ...
189+
190+
@overload
191+
def __getitem__(self, line_index: slice) -> list[str]: ...
190192

191193
@abstractmethod
192194
def __getitem__(self, line_index: int | slice) -> str | list[str]:

0 commit comments

Comments
 (0)