Repository: Textualize/textual

Issue:
DataTable doesn't update styles when component classes are changed with hot-reloading
If you change the data table cursor component style `.datatable--header` with hot reloading, then the style update isn't applied properly.
I suspect this is transversal to all other component classes and it's related to the dead coroutine `_on_styles_updated`.
DataTable doesn't update styles when component classes are changed with hot-reloading
If you change the data table cursor component style `.datatable--header` with hot reloading, then the style update isn't applied properly.
I suspect this is transversal to all other component classes and it's related to the dead coroutine `_on_styles_updated`.


Discussion:
That may have been the original name. If you rename it to `notify_style_update` I think it will work.
Yup, I figured that much. I'm just writing a decent regression test.
I'm glad I found this because it gives also some clarity on the caching issue you brought up in https://github.com/Textualize/textual/pull/3213#discussion_r1326071862
That may have been the original name. If you rename it to `notify_style_update` I think it will work.
Yup, I figured that much. I'm just writing a decent regression test.
I'm glad I found this because it gives also some clarity on the caching issue you brought up in https://github.com/Textualize/textual/pull/3213#discussion_r1326071862

Resolving patch:
diff --git a/src/textual/widgets/_data_table.py b/src/textual/widgets/_data_table.py
--- a/src/textual/widgets/_data_table.py
+++ b/src/textual/widgets/_data_table.py
@@ -965,7 +965,7 @@ def get_row_height(self, row_key: RowKey) -> int:
             return self.header_height
         return self.rows[row_key].height
 
-    async def _on_styles_updated(self) -> None:
+    def notify_style_update(self) -> None:
         self._clear_caches()
         self.refresh()
 


Test patch:
diff --git a/tests/snapshot_tests/__snapshots__/test_snapshots.ambr b/tests/snapshot_tests/__snapshots__/test_snapshots.ambr
--- a/tests/snapshot_tests/__snapshots__/test_snapshots.ambr
+++ b/tests/snapshot_tests/__snapshots__/test_snapshots.ambr
@@ -13846,6 +13846,167 @@
   
   '''
 # ---
+# name: test_datatable_hot_reloading
+  '''
+  <svg class="rich-terminal" viewBox="0 0 994 635.5999999999999" xmlns="http://www.w3.org/2000/svg">
+      <!-- Generated with Rich https://www.textualize.io -->
+      <style>
+  
+      @font-face {
+          font-family: "Fira Code";
+          src: local("FiraCode-Regular"),
+                  url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff2/FiraCode-Regular.woff2") format("woff2"),
+                  url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff/FiraCode-Regular.woff") format("woff");
+          font-style: normal;
+          font-weight: 400;
+      }
+      @font-face {
+          font-family: "Fira Code";
+          src: local("FiraCode-Bold"),
+                  url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff2/FiraCode-Bold.woff2") format("woff2"),
+                  url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff/FiraCode-Bold.woff") format("woff");
+          font-style: bold;
+          font-weight: 700;
+      }
+  
+      .terminal-1782919787-matrix {
+          font-family: Fira Code, monospace;
+          font-size: 20px;
+          line-height: 24.4px;
+          font-variant-east-asian: full-width;
+      }
+  
+      .terminal-1782919787-title {
+          font-size: 18px;
+          font-weight: bold;
+          font-family: arial;
+      }
+  
+      .terminal-1782919787-r1 { fill: #dde6ed;font-weight: bold }
+  .terminal-1782919787-r2 { fill: #dde6ed }
+  .terminal-1782919787-r3 { fill: #c5c8c6 }
+  .terminal-1782919787-r4 { fill: #1e1405 }
+  .terminal-1782919787-r5 { fill: #211505 }
+  .terminal-1782919787-r6 { fill: #e1e2e3 }
+  .terminal-1782919787-r7 { fill: #dfe4e7 }
+  .terminal-1782919787-r8 { fill: #e1e1e1 }
+      </style>
+  
+      <defs>
+      <clipPath id="terminal-1782919787-clip-terminal">
+        <rect x="0" y="0" width="975.0" height="584.5999999999999" />
+      </clipPath>
+      <clipPath id="terminal-1782919787-line-0">
+      <rect x="0" y="1.5" width="976" height="24.65"/>
+              </clipPath>
+  <clipPath id="terminal-1782919787-line-1">
+      <rect x="0" y="25.9" width="976" height="24.65"/>
+              </clipPath>
+  <clipPath id="terminal-1782919787-line-2">
+      <rect x="0" y="50.3" width="976" height="24.65"/>
+              </clipPath>
+  <clipPath id="terminal-1782919787-line-3">
+      <rect x="0" y="74.7" width="976" height="24.65"/>
+              </clipPath>
+  <clipPath id="terminal-1782919787-line-4">
+      <rect x="0" y="99.1" width="976" height="24.65"/>
+              </clipPath>
+  <clipPath id="terminal-1782919787-line-5">
+      <rect x="0" y="123.5" width="976" height="24.65"/>
+              </clipPath>
+  <clipPath id="terminal-1782919787-line-6">
+      <rect x="0" y="147.9" width="976" height="24.65"/>
+              </clipPath>
+  <clipPath id="terminal-1782919787-line-7">
+      <rect x="0" y="172.3" width="976" height="24.65"/>
+              </clipPath>
+  <clipPath id="terminal-1782919787-line-8">
+      <rect x="0" y="196.7" width="976" height="24.65"/>
+              </clipPath>
+  <clipPath id="terminal-1782919787-line-9">
+      <rect x="0" y="221.1" width="976" height="24.65"/>
+              </clipPath>
+  <clipPath id="terminal-1782919787-line-10">
+      <rect x="0" y="245.5" width="976" height="24.65"/>
+              </clipPath>
+  <clipPath id="terminal-1782919787-line-11">
+      <rect x="0" y="269.9" width="976" height="24.65"/>
+              </clipPath>
+  <clipPath id="terminal-1782919787-line-12">
+      <rect x="0" y="294.3" width="976" height="24.65"/>
+              </clipPath>
+  <clipPath id="terminal-1782919787-line-13">
+      <rect x="0" y="318.7" width="976" height="24.65"/>
+              </clipPath>
+  <clipPath id="terminal-1782919787-line-14">
+      <rect x="0" y="343.1" width="976" height="24.65"/>
+              </clipPath>
+  <clipPath id="terminal-1782919787-line-15">
+      <rect x="0" y="367.5" width="976" height="24.65"/>
+              </clipPath>
+  <clipPath id="terminal-1782919787-line-16">
+      <rect x="0" y="391.9" width="976" height="24.65"/>
+              </clipPath>
+  <clipPath id="terminal-1782919787-line-17">
+      <rect x="0" y="416.3" width="976" height="24.65"/>
+              </clipPath>
+  <clipPath id="terminal-1782919787-line-18">
+      <rect x="0" y="440.7" width="976" height="24.65"/>
+              </clipPath>
+  <clipPath id="terminal-1782919787-line-19">
+      <rect x="0" y="465.1" width="976" height="24.65"/>
+              </clipPath>
+  <clipPath id="terminal-1782919787-line-20">
+      <rect x="0" y="489.5" width="976" height="24.65"/>
+              </clipPath>
+  <clipPath id="terminal-1782919787-line-21">
+      <rect x="0" y="513.9" width="976" height="24.65"/>
+              </clipPath>
+  <clipPath id="terminal-1782919787-line-22">
+      <rect x="0" y="538.3" width="976" height="24.65"/>
+              </clipPath>
+      </defs>
+  
+      <rect fill="#292929" stroke="rgba(255,255,255,0.35)" stroke-width="1" x="1" y="1" width="992" height="633.6" rx="8"/><text class="terminal-1782919787-title" fill="#c5c8c6" text-anchor="middle" x="496" y="27">DataTableHotReloadingApp</text>
+              <g transform="translate(26,22)">
+              <circle cx="0" cy="0" r="7" fill="#ff5f57"/>
+              <circle cx="22" cy="0" r="7" fill="#febc2e"/>
+              <circle cx="44" cy="0" r="7" fill="#28c840"/>
+              </g>
+          
+      <g transform="translate(9, 41)" clip-path="url(#terminal-1782919787-clip-terminal)">
+      <rect fill="#004578" x="0" y="1.5" width="146.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#004578" x="146.4" y="1.5" width="73.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#073b61" x="219.6" y="1.5" width="756.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#ec9b29" x="0" y="25.9" width="146.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#fea62b" x="146.4" y="25.9" width="73.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#1a2127" x="219.6" y="25.9" width="756.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#0f314b" x="0" y="50.3" width="146.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="146.4" y="50.3" width="73.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="219.6" y="50.3" width="756.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#0f314b" x="0" y="74.7" width="146.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#19232b" x="146.4" y="74.7" width="73.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#1a2127" x="219.6" y="74.7" width="756.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="99.1" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="123.5" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="147.9" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="172.3" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="196.7" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="221.1" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="245.5" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="269.9" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="294.3" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="318.7" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="343.1" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="367.5" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="391.9" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="416.3" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="440.7" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="465.1" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="489.5" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="513.9" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="538.3" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="562.7" width="976" height="24.65" shape-rendering="crispEdges"/>
+      <g class="terminal-1782919787-matrix">
+      <text class="terminal-1782919787-r1" x="0" y="20" textLength="146.4" clip-path="url(#terminal-1782919787-line-0)">&#160;A&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</text><text class="terminal-1782919787-r1" x="146.4" y="20" textLength="73.2" clip-path="url(#terminal-1782919787-line-0)">&#160;B&#160;&#160;&#160;&#160;</text><text class="terminal-1782919787-r3" x="976" y="20" textLength="12.2" clip-path="url(#terminal-1782919787-line-0)">
+  </text><text class="terminal-1782919787-r4" x="0" y="44.4" textLength="146.4" clip-path="url(#terminal-1782919787-line-1)">&#160;one&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</text><text class="terminal-1782919787-r5" x="146.4" y="44.4" textLength="73.2" clip-path="url(#terminal-1782919787-line-1)">&#160;two&#160;&#160;</text><text class="terminal-1782919787-r3" x="976" y="44.4" textLength="12.2" clip-path="url(#terminal-1782919787-line-1)">
+  </text><text class="terminal-1782919787-r7" x="0" y="68.8" textLength="146.4" clip-path="url(#terminal-1782919787-line-2)">&#160;three&#160;&#160;&#160;&#160;&#160;&#160;</text><text class="terminal-1782919787-r8" x="146.4" y="68.8" textLength="73.2" clip-path="url(#terminal-1782919787-line-2)">&#160;four&#160;</text><text class="terminal-1782919787-r3" x="976" y="68.8" textLength="12.2" clip-path="url(#terminal-1782919787-line-2)">
+  </text><text class="terminal-1782919787-r7" x="0" y="93.2" textLength="146.4" clip-path="url(#terminal-1782919787-line-3)">&#160;five&#160;&#160;&#160;&#160;&#160;&#160;&#160;</text><text class="terminal-1782919787-r6" x="146.4" y="93.2" textLength="73.2" clip-path="url(#terminal-1782919787-line-3)">&#160;six&#160;&#160;</text><text class="terminal-1782919787-r3" x="976" y="93.2" textLength="12.2" clip-path="url(#terminal-1782919787-line-3)">
+  </text><text class="terminal-1782919787-r3" x="976" y="117.6" textLength="12.2" clip-path="url(#terminal-1782919787-line-4)">
+  </text><text class="terminal-1782919787-r3" x="976" y="142" textLength="12.2" clip-path="url(#terminal-1782919787-line-5)">
+  </text><text class="terminal-1782919787-r3" x="976" y="166.4" textLength="12.2" clip-path="url(#terminal-1782919787-line-6)">
+  </text><text class="terminal-1782919787-r3" x="976" y="190.8" textLength="12.2" clip-path="url(#terminal-1782919787-line-7)">
+  </text><text class="terminal-1782919787-r3" x="976" y="215.2" textLength="12.2" clip-path="url(#terminal-1782919787-line-8)">
+  </text><text class="terminal-1782919787-r3" x="976" y="239.6" textLength="12.2" clip-path="url(#terminal-1782919787-line-9)">
+  </text><text class="terminal-1782919787-r3" x="976" y="264" textLength="12.2" clip-path="url(#terminal-1782919787-line-10)">
+  </text><text class="terminal-1782919787-r3" x="976" y="288.4" textLength="12.2" clip-path="url(#terminal-1782919787-line-11)">
+  </text><text class="terminal-1782919787-r3" x="976" y="312.8" textLength="12.2" clip-path="url(#terminal-1782919787-line-12)">
+  </text><text class="terminal-1782919787-r3" x="976" y="337.2" textLength="12.2" clip-path="url(#terminal-1782919787-line-13)">
+  </text><text class="terminal-1782919787-r3" x="976" y="361.6" textLength="12.2" clip-path="url(#terminal-1782919787-line-14)">
+  </text><text class="terminal-1782919787-r3" x="976" y="386" textLength="12.2" clip-path="url(#terminal-1782919787-line-15)">
+  </text><text class="terminal-1782919787-r3" x="976" y="410.4" textLength="12.2" clip-path="url(#terminal-1782919787-line-16)">
+  </text><text class="terminal-1782919787-r3" x="976" y="434.8" textLength="12.2" clip-path="url(#terminal-1782919787-line-17)">
+  </text><text class="terminal-1782919787-r3" x="976" y="459.2" textLength="12.2" clip-path="url(#terminal-1782919787-line-18)">
+  </text><text class="terminal-1782919787-r3" x="976" y="483.6" textLength="12.2" clip-path="url(#terminal-1782919787-line-19)">
+  </text><text class="terminal-1782919787-r3" x="976" y="508" textLength="12.2" clip-path="url(#terminal-1782919787-line-20)">
+  </text><text class="terminal-1782919787-r3" x="976" y="532.4" textLength="12.2" clip-path="url(#terminal-1782919787-line-21)">
+  </text><text class="terminal-1782919787-r3" x="976" y="556.8" textLength="12.2" clip-path="url(#terminal-1782919787-line-22)">
+  </text>
+      </g>
+      </g>
+  </svg>
+  
+  '''
+# ---
 # name: test_datatable_labels_and_fixed_data
   '''
   <svg class="rich-terminal" viewBox="0 0 994 635.5999999999999" xmlns="http://www.w3.org/2000/svg">
diff --git a/tests/snapshot_tests/snapshot_apps/datatable_hot_reloading.py b/tests/snapshot_tests/snapshot_apps/datatable_hot_reloading.py
new file mode 100644
--- /dev/null
+++ b/tests/snapshot_tests/snapshot_apps/datatable_hot_reloading.py
@@ -0,0 +1,58 @@
+from pathlib import Path
+
+from textual.app import App, ComposeResult
+from textual.widgets import DataTable
+
+CSS_PATH = (Path(__file__) / "../datatable_hot_reloading.tcss").resolve()
+
+# Write some CSS to the file before the app loads.
+# Then, the test will clear all the CSS to see if the
+# hot reloading applies the changes correctly.
+CSS_PATH.write_text(
+    """\
+DataTable > .datatable--cursor {
+    background: purple;
+}
+
+DataTable > .datatable--fixed {
+    background: red;
+}
+
+DataTable > .datatable--fixed-cursor {
+    background: blue;
+}
+
+DataTable > .datatable--header {
+    background: yellow;
+}
+
+DataTable > .datatable--odd-row {
+    background: pink;
+}
+
+DataTable > .datatable--even-row {
+    background: brown;
+}
+"""
+)
+
+
+class DataTableHotReloadingApp(App[None]):
+    CSS_PATH = CSS_PATH
+
+    def compose(self) -> ComposeResult:
+        yield DataTable(zebra_stripes=True, cursor_type="row")
+
+    def on_mount(self) -> None:
+        dt = self.query_one(DataTable)
+        dt.add_column("A", width=10)
+        self.c = dt.add_column("B")
+        dt.fixed_columns = 1
+        dt.add_row("one", "two")
+        dt.add_row("three", "four")
+        dt.add_row("five", "six")
+
+
+if __name__ == "__main__":
+    app = DataTableHotReloadingApp()
+    app.run()
diff --git a/tests/snapshot_tests/snapshot_apps/datatable_hot_reloading.tcss b/tests/snapshot_tests/snapshot_apps/datatable_hot_reloading.tcss
new file mode 100644
--- /dev/null
+++ b/tests/snapshot_tests/snapshot_apps/datatable_hot_reloading.tcss
@@ -0,0 +1 @@
+/* This file is purposefully empty. */
diff --git a/tests/snapshot_tests/test_snapshots.py b/tests/snapshot_tests/test_snapshots.py
--- a/tests/snapshot_tests/test_snapshots.py
+++ b/tests/snapshot_tests/test_snapshots.py
@@ -511,6 +511,20 @@ async def run_before(pilot):
     )
 
 
+def test_datatable_hot_reloading(snap_compare):
+    """Regression test for https://github.com/Textualize/textual/issues/3312."""
+
+    async def run_before(pilot):
+        css_file = pilot.app.CSS_PATH
+        with open(css_file, "w") as f:
+            f.write("/* This file is purposefully empty. */\n")  # Clear all the CSS.
+        await pilot.app._on_css_change()
+
+    assert snap_compare(
+        SNAPSHOT_APPS_DIR / "datatable_hot_reloading.py", run_before=run_before
+    )
+
+
 def test_layer_fix(snap_compare):
     # Check https://github.com/Textualize/textual/issues/1358
     assert snap_compare(SNAPSHOT_APPS_DIR / "layer_fix.py", press=["d"])