Skip to content

Commit

Permalink
Merge pull request #5588 from Textualize/preflight
Browse files Browse the repository at this point in the history
Preflight
  • Loading branch information
willmcgugan authored Feb 27, 2025
2 parents 3955ece + f2ac6ae commit dd36b69
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased

### Added

- Added Widget.preflight_checks to perform some debug checks after a widget is instantiated, to catch common errors. https://github.com/Textualize/textual/pull/5588

## [2.1.2] - 2025-02-26

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion src/textual/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,7 @@ def is_attached(self) -> bool:
@property
def debug(self) -> bool:
"""Is debug mode enabled?"""
return "debug" in self.features
return "debug" in self.features or constants.DEBUG

@property
def is_headless(self) -> bool:
Expand Down
18 changes: 18 additions & 0 deletions src/textual/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,22 @@ def text_selection(self) -> Selection | None:
"""Text selection information, or `None` if no text is selected in this widget."""
return self.screen.selections.get(self, None)

def preflight_checks(self) -> None:
"""Called in debug mode to do preflight checks.
This is used by Textual to log some common errors, but you could implement this
in custom widgets to perform additional checks.
"""

if hasattr(self, "CSS"):
from textual.screen import Screen

if not isinstance(self, Screen):
self.log.warning(
f"'{self.__class__.__name__}.CSS' will be ignored (use 'DEFAULT_CSS' class variable for widgets)"
)

def _cover(self, widget: Widget) -> None:
"""Set a widget used to replace the visuals of this widget (used for loading indicator).
Expand Down Expand Up @@ -1475,6 +1491,8 @@ def _post_register(self, app: App) -> None:
tie_breaker=tie_breaker,
scope=scope,
)
if app.debug:
app.call_next(self.preflight_checks)

def _get_box_model(
self,
Expand Down

0 comments on commit dd36b69

Please sign in to comment.