Skip to content

Commit c81df43

Browse files
authored
Merge pull request #5045 from Textualize/fix-textarea-construct
prevent error in TextArea constructor
2 parents 12d1573 + 9dce34c commit c81df43

File tree

4 files changed

+51
-6
lines changed

4 files changed

+51
-6
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

8-
## Unreleased
8+
## [0.80.1] - 2024-09-24
99

1010
### Fixed
1111

1212
- Fixed crash when exiting the app prematurely https://github.com/Textualize/textual/pull/5039
13+
- Fixed exception constructing TextArea outside of App https://github.com/Textualize/textual/pull/5045
1314

1415
## [0.80.0] - 2024-09-23
1516

@@ -2387,6 +2388,7 @@ https://textual.textualize.io/blog/2022/11/08/version-040/#version-040
23872388
- New handler system for messages that doesn't require inheritance
23882389
- Improved traceback handling
23892390

2391+
[0.80.1]: https://github.com/Textualize/textual/compare/v0.80.0...v0.80.1
23902392
[0.80.0]: https://github.com/Textualize/textual/compare/v0.79.0...v0.80.0
23912393
[0.79.0]: https://github.com/Textualize/textual/compare/v0.78.0...v0.79.0
23922394
[0.78.0]: https://github.com/Textualize/textual/compare/v0.77.0...v0.78.0

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "textual"
3-
version = "0.80.0"
3+
version = "0.80.1"
44
homepage = "https://github.com/Textualize/textual"
55
repository = "https://github.com/Textualize/textual"
66
documentation = "https://textual.textualize.io/"

src/textual/widgets/_text_area.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -464,9 +464,6 @@ def __init__(
464464

465465
self.tab_behavior = tab_behavior
466466

467-
# When `app.dark` is toggled, reset the theme (since it caches values).
468-
self.watch(self.app, "dark", self._app_dark_toggled, init=False)
469-
470467
if tooltip is not None:
471468
self.tooltip = tooltip
472469

@@ -610,6 +607,10 @@ def _watch_selection(
610607
) -> None:
611608
"""When the cursor moves, scroll it into view."""
612609
# Find the visual offset of the cursor in the document
610+
611+
if not self.is_mounted:
612+
return
613+
613614
cursor_location = selection.end
614615

615616
self.scroll_cursor_visible()
@@ -1519,6 +1520,9 @@ def gutter_width(self) -> int:
15191520
return gutter_width
15201521

15211522
def _on_mount(self, event: events.Mount) -> None:
1523+
# When `app.dark` is toggled, reset the theme (since it caches values).
1524+
self.watch(self.app, "dark", self._app_dark_toggled, init=False)
1525+
15221526
self.blink_timer = self.set_interval(
15231527
0.5,
15241528
self._toggle_cursor_blink_visible,

tests/test_widget.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,46 @@
1212
from textual.geometry import Offset, Size
1313
from textual.message import Message
1414
from textual.widget import BadWidgetName, MountError, PseudoClasses, Widget
15-
from textual.widgets import Label, LoadingIndicator
15+
from textual.widgets import (
16+
Button,
17+
DataTable,
18+
Footer,
19+
Header,
20+
Input,
21+
Label,
22+
LoadingIndicator,
23+
Log,
24+
OptionList,
25+
RichLog,
26+
Switch,
27+
TextArea,
28+
)
29+
30+
31+
async def test_widget_construct():
32+
"""Regression test for https://github.com/Textualize/textual/issues/5042"""
33+
34+
# Check that constructing the widget outside of the app, doesn't invoke code that
35+
# expects an active app.
36+
class MyApp(App):
37+
def __init__(self) -> None:
38+
super().__init__()
39+
self.button = Button()
40+
self.data_table = DataTable()
41+
self.footer = Footer()
42+
self.header = Header()
43+
self.input = Input()
44+
self.label = Label()
45+
self.loading_indicator = LoadingIndicator()
46+
self.log_ = Log()
47+
self.option_list = OptionList()
48+
self.rich_log = RichLog()
49+
self.switch = Switch()
50+
self.text_area = TextArea(language="python")
51+
52+
app = MyApp()
53+
async with app.run_test():
54+
pass
1655

1756

1857
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)