simplify types

This commit is contained in:
Will McGugan
2024-11-10 17:31:38 +00:00
parent 7b2214c43c
commit c942d8dc21
4 changed files with 9 additions and 18 deletions

View File

@@ -190,8 +190,7 @@ DEFAULT_COLORS = {
ComposeResult = Iterable[Widget]
RenderResult = RenderableType | Visual | SupportsTextualize
ContentType = RenderableType | Visual | SupportsTextualize
"""Any content that can be rendered."""
"""Result of Widget.render()"""
AutopilotCallbackType: TypeAlias = (
"Callable[[Pilot[object]], Coroutine[Any, Any, None]]"

View File

@@ -238,10 +238,6 @@ class Content(Visual):
lines = self.without_spans.split("\n")
return max(line.expand_tabs(8).cell_length for line in lines)
# def textualize(self) -> Content:
# ""
# return self
def render_strips(
self,
widget: Widget,

View File

@@ -61,10 +61,12 @@ def visualize(widget: Widget, obj: object) -> Visual:
if isinstance(obj, Visual):
# Already a visual
return obj
# The visualize method should return a Visual if present.
visualize = getattr(obj, "visualize", None)
if visualize is None:
# Doesn't expose the textualize protocol
if is_renderable(obj):
# If it is a string, render it to Text
if isinstance(obj, str):
obj = widget.render_str(obj)

View File

@@ -22,7 +22,7 @@ from textual.visual import Padding, Visual, visualize
if TYPE_CHECKING:
from typing_extensions import Self, TypeAlias
from textual.app import ContentType
from textual.app import RenderResult
class DuplicateID(Exception):
@@ -42,7 +42,7 @@ class Option:
"""Class that holds the details of an individual option."""
def __init__(
self, prompt: ContentType, id: str | None = None, disabled: bool = False
self, prompt: RenderResult, id: str | None = None, disabled: bool = False
) -> None:
"""Initialise the option.
@@ -56,11 +56,11 @@ class Option:
self.disabled = disabled
@property
def prompt(self) -> ContentType:
def prompt(self) -> RenderResult:
"""The prompt for the option."""
return self._prompt
def set_prompt(self, prompt: ContentType) -> None:
def set_prompt(self, prompt: RenderResult) -> None:
"""Set the prompt for the option.
Args:
@@ -81,7 +81,7 @@ class Option:
yield "id", self.id, None
yield "disabled", self.disabled, False
def __rich__(self) -> ContentType:
def __rich__(self) -> RenderResult:
return self._prompt
@@ -459,7 +459,7 @@ class OptionList(ScrollView, can_focus=True):
return Option(content)
def _render_option_content(
self, option_index: int, content: ContentType, component_class: str, width: int
self, option_index: int, content: RenderResult, component_class: str, width: int
) -> list[Strip]:
"""Render content for option and style.
@@ -874,12 +874,6 @@ class OptionList(ScrollView, can_focus=True):
elif mouse_over:
component_class = "option-list--option-hover"
# style = (
# self.get_component_rich_style(component_class)
# if component_class
# else self.rich_style
# )
strips = self._render_option_content(
option_index,
renderable,