Skip to content

Commit da5395b

Browse files
committed
event loop test
1 parent 5cd70ad commit da5395b

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1010
### Fixed
1111

1212
- Fixed `OptionList` causing excessive redrawing https://github.com/Textualize/textual/pull/5766
13-
- Log messages could be written to stdout when there was no app, which could happen when using run_async or threads. Now they will be supressed, unless the env var `TEXTUAL_DEBUG` is set
13+
- Log messages could be written to stdout when there was no app, which could happen when using run_async or threads. Now they will be suppressed, unless the env var `TEXTUAL_DEBUG` is set
1414

1515
### Added
1616

src/textual/app.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2126,7 +2126,7 @@ def run(
21262126
mouse: bool = True,
21272127
size: tuple[int, int] | None = None,
21282128
auto_pilot: AutopilotCallbackType | None = None,
2129-
loop_factory: Callable[[], AbstractEventLoop] | None = None,
2129+
loop: AbstractEventLoop | None = None,
21302130
) -> ReturnType | None:
21312131
"""Run the app.
21322132
@@ -2138,7 +2138,7 @@ def run(
21382138
size: Force terminal size to `(WIDTH, HEIGHT)`,
21392139
or None to auto-detect.
21402140
auto_pilot: An auto pilot coroutine.
2141-
loop_factory: Callable which returns a new asyncio Loop, or `None` to use default.
2141+
loop: Asyncio loop instance, or `None` to use default.
21422142
Returns:
21432143
App return value.
21442144
"""
@@ -2156,12 +2156,10 @@ async def run_app() -> None:
21562156

21572157
if _ASYNCIO_GET_EVENT_LOOP_IS_DEPRECATED:
21582158
# N.B. This doesn't work with Python<3.10, as we end up with 2 event loops:
2159-
asyncio.run(run_app(), loop_factory=loop_factory)
2159+
asyncio.run(run_app(), loop_factory=None if loop is None else lambda: loop)
21602160
else:
21612161
# However, this works with Python<3.10:
2162-
event_loop = (
2163-
asyncio.get_event_loop() if loop_factory is None else loop_factory()
2164-
)
2162+
event_loop = asyncio.get_event_loop() if loop is None else loop
21652163
event_loop.run_until_complete(run_app())
21662164
return self.return_value
21672165

tests/test_app.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import asyncio
12
import contextlib
23

34
import pytest
@@ -340,3 +341,15 @@ def on_click(self, event: events.Click) -> None:
340341
# Each click is outwith the time threshold, so a click chain is never created.
341342
await raw_click(pilot, "#one")
342343
assert click_count == i
344+
345+
346+
def test_app_loop() -> None:
347+
"""Test that App.run accepts a loop argument."""
348+
349+
class MyApp(App[int]):
350+
def on_mount(self) -> None:
351+
self.exit(42)
352+
353+
app = MyApp()
354+
result = app.run(loop=asyncio.new_event_loop())
355+
assert result == 42

0 commit comments

Comments
 (0)