From bee681d359e6a980341263ecc0965551e8364b90 Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Thu, 26 Dec 2019 18:21:08 +0000 Subject: [PATCH] typing fix --- rich/_wrap.py | 11 ----------- rich/markdown.py | 6 +++--- rich/table.py | 31 +++++++++++++++++-------------- rich/text.py | 4 ++-- 4 files changed, 22 insertions(+), 30 deletions(-) diff --git a/rich/_wrap.py b/rich/_wrap.py index 6eddf307..992348ef 100644 --- a/rich/_wrap.py +++ b/rich/_wrap.py @@ -25,14 +25,3 @@ def divide_line(text: str, width: int) -> List[int]: line_size = 0 line_size += len(word) return divides - - -if __name__ == "__main__": # pragma: no cover - test = " Where there is a Will there is a way. Hello World. There can be only one." - - boundaries = [0, *wrap(test, 10), len(test)] - print(boundaries) - for start, end in zip(boundaries, boundaries[1:]): - line = test[start:end] - print(f"{len(line)}\t", repr(line)) - diff --git a/rich/markdown.py b/rich/markdown.py index 0cd32c39..885a0a14 100644 --- a/rich/markdown.py +++ b/rich/markdown.py @@ -1,5 +1,5 @@ from dataclasses import dataclass -from typing import Any, ClassVar, Dict, Iterable, List, Optional, Union +from typing import Any, ClassVar, Dict, Iterable, List, Optional, Type, Union from commonmark.blocks import Parser @@ -162,7 +162,7 @@ class CodeBlock(TextElement): style_name = "markdown.code_block" @classmethod - def create(cls, markdown: "Markdown", node: Any) -> "ListElement": + def create(cls, markdown: "Markdown", node: Any) -> "CodeBlock": if node.info is None: return cls("default", markdown.code_theme) lexer_name, _, _ = node.info.partition(" ") @@ -333,7 +333,7 @@ class MarkdownContext: class Markdown: - elements: ClassVar[Dict[str, MarkdownElement]] = { + elements: ClassVar[Dict[str, Type[MarkdownElement]]] = { "paragraph": Paragraph, "heading": Heading, "code_block": CodeBlock, diff --git a/rich/table.py b/rich/table.py index 837f4568..e27b8a40 100644 --- a/rich/table.py +++ b/rich/table.py @@ -33,17 +33,6 @@ from ._render_width import RenderWidth from ._tools import iter_first_last, iter_first, iter_last, ratio_divide -T = TypeVar("T") - - -def _pick_first(values: Iterable[Optional[T]], final: T) -> T: - """Pick first non-None value.""" - for value in values: - if value is not None: - return value - return final - - @dataclass class Column: """Defines a column in a table.""" @@ -167,12 +156,26 @@ class Table: width (int, optional): A minimum width in characters. Defaults to None. ratio (int, optional): Flexible ratio for the column. Defaults to None. """ + + def _pick_first_style( + *values: Optional[Union[Style, str]] + ) -> Union[Style, str]: + """Pick first non-None style.""" + for value in values: + if value is not None: + return value + raise ValueError("expected at least one non-None style") + column = Column( header=header, footer=footer, - header_style=_pick_first((header_style, self.header_style), "table.header"), - footer_style=_pick_first((footer_style, self.footer_style), "table.footer"), - style=_pick_first((style, self.style), "table.cell"), + header_style=_pick_first_style( + header_style, self.header_style, "table.header" + ), + footer_style=_pick_first_style( + footer_style, self.footer_style, "table.footer" + ), + style=_pick_first_style(style, self.style, "table.cell"), justify=justify, width=width, ratio=ratio, diff --git a/rich/text.py b/rich/text.py index 9898ca75..83bb6235 100644 --- a/rich/text.py +++ b/rich/text.py @@ -366,7 +366,7 @@ class Text: else: raise TypeError("Only str or Text can be appended to Text") - def split(self, separator="\n", include_separator: bool = False) -> List["Text"]: + def split(self, separator="\n", include_separator: bool = False) -> Lines: """Split rich text in to lines, preserving styles. Args: @@ -379,7 +379,7 @@ class Text: text = self.text if separator not in text: - return [self.copy()] + return Lines([self.copy()]) if text.endswith(separator): text = text[: -len(separator)] offsets: List[int] = []