mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
Add support for private watch methods
This change allows for private watch methods. By convention they start with
an underscore. If a reactive or var has a private watch method, it will be
used in preference to a public watch method.
With this change it becomes easier to have a private reactive/var whose
watcher is also private. For example:
_counter = var(0)
"""This is a private counter, it won't appear in the docs."
...
def _watch__counter(self) -> None:
"""Watch _counter, but don't appear in the docs either."
...
See #2382.
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
|
||||
import pytest
|
||||
@@ -386,3 +388,25 @@ async def test_watch_compute():
|
||||
assert app.show_ac is False
|
||||
|
||||
assert watch_called == [True, True, False, False, True, True, False, False]
|
||||
|
||||
|
||||
async def test_private_watch() -> None:
|
||||
"""A private watch method should win over a public watch method."""
|
||||
|
||||
calls: dict[str, bool] = {"private": False, "public": False}
|
||||
|
||||
class PrivateWatchTest(App):
|
||||
counter = var(0, init=False)
|
||||
|
||||
def watch_counter(self) -> None:
|
||||
calls["public"] = True
|
||||
|
||||
def _watch_counter(self) -> None:
|
||||
calls["private"] = True
|
||||
|
||||
async with PrivateWatchTest().run_test() as pilot:
|
||||
assert calls["private"] is False
|
||||
assert calls["public"] is False
|
||||
pilot.app.counter += 1
|
||||
assert calls["private"] is True
|
||||
assert calls["public"] is False
|
||||
|
||||
Reference in New Issue
Block a user