Skip to content

Commit 9cda3ee

Browse files
committed
docs
1 parent f3f385d commit 9cda3ee

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/textual/getters.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66
from __future__ import annotations
77

8-
from typing import TYPE_CHECKING, Generic, TypeVar, overload
8+
from inspect import isclass
9+
from typing import TYPE_CHECKING, Callable, Generic, TypeVar, overload
910

1011
from textual._context import NoActiveAppError, active_app
1112
from textual.css.query import NoMatches, QueryType, WrongType
@@ -23,18 +24,23 @@
2324
class app(Generic[AppType]):
2425
"""Create a property to return the active app.
2526
27+
All widgets have a default `app` property which returns an App instance.
28+
Type checkers will complain if you try to access attributes defined on your App class, which aren't
29+
present in the base class. To keep the type checker happy you can add this property to get your
30+
specific App subclass.
31+
2632
Example:
2733
```python
2834
class MyWidget(Widget):
2935
app = getters.app(MyApp)
3036
```
3137
3238
Args:
33-
app_type: The app class.
39+
app_type: The App subclass, or a callable which returns an App subclass.
3440
"""
3541

36-
def __init__(self, app_type: type[AppType]) -> None:
37-
self._app_type = app_type
42+
def __init__(self, app_type: type[AppType] | Callable[[], type[AppType]]) -> None:
43+
self._app_type = app_type if isclass(app_type) else app_type()
3844

3945
def __get__(self, obj: MessagePump, obj_type: type[MessagePump]) -> AppType:
4046
try:

0 commit comments

Comments
 (0)