@@ -664,3 +664,44 @@ def watch_test_2_extra() -> None:
664664 app .query_one (SomeWidget ).test_2 = 73
665665 assert logs .count ("test_2_extra" ) == 2
666666 assert logs .count ("test_2" ) == 1
667+
668+
669+ async def test_external_watch_init_does_not_propagate_to_externals () -> None :
670+ """Regression test for https://github.com/Textualize/textual/issues/3878.
671+
672+ Make sure that when setting an extra watcher programmatically and `init` is set,
673+ we init only the new watcher and not the other ones (even if they were
674+ added dynamically with `watch`), but at the same time make sure all watchers
675+ work in regular circumstances.
676+ """
677+
678+ logs : list [str ] = []
679+
680+ class SomeWidget (Widget ):
681+ test_var : var [int ] = var (0 )
682+
683+ class MyApp (App [None ]):
684+ def compose (self ) -> ComposeResult :
685+ yield SomeWidget ()
686+
687+ def add_first_watcher (self ) -> None :
688+ def first_callback () -> None :
689+ logs .append ("first" )
690+
691+ self .watch (self .query_one (SomeWidget ), "test_var" , first_callback )
692+
693+ def add_second_watcher (self ) -> None :
694+ def second_callback () -> None :
695+ logs .append ("second" )
696+
697+ self .watch (self .query_one (SomeWidget ), "test_var" , second_callback )
698+
699+ app = MyApp ()
700+ async with app .run_test ():
701+ assert logs == []
702+ app .add_first_watcher ()
703+ assert logs == ["first" ]
704+ app .add_second_watcher ()
705+ assert logs == ["first" , "second" ]
706+ app .query_one (SomeWidget ).test_var = 73
707+ assert logs == ["first" , "second" , "first" , "second" ]
0 commit comments