From 356bdf8d2a8319d49421d4fd0dd0e28c25ac3f60 Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Fri, 27 Dec 2019 10:07:38 +0000 Subject: [PATCH] typing fixes --- .gitignore | 1 + rich/_log_render.py | 47 ------------------------------------------- rich/_render_width.py | 6 +++++- rich/console.py | 6 +++--- rich/markdown.py | 11 +++++----- rich/panel.py | 2 +- rich/style.py | 5 +++++ rich/syntax.py | 10 ++++----- 8 files changed, 26 insertions(+), 62 deletions(-) diff --git a/.gitignore b/.gitignore index a586b1d6..e4894a6d 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ .vscode mypy_report docs/build +docs/source/_build # Byte-compiled / optimized / DLL files __pycache__/ diff --git a/rich/_log_render.py b/rich/_log_render.py index 9fd34810..75fd5494 100644 --- a/rich/_log_render.py +++ b/rich/_log_render.py @@ -56,50 +56,3 @@ class LogRender: output.add_row(*row) return output - - -if __name__ == "__main__": # pragma: no cover - from .console import Console - - console = Console() - print(console) - logger = Logger(console) - - from .markdown import Markdown - from .syntax import Syntax - - s = Syntax( - '''\ -@classmethod -def get(cls, renderable: RenderableType, max_width: int) -> RenderWidth: - """Get desired width for a renderable.""" - if hasattr(renderable, "__console__"): - get_console_width = getattr(renderable, "__console_width__", None) - if get_console_width is not None: - render_width = get_console_width(max_width).with_maximum(max_width) - return render_width.normalize() - else: - return RenderWidth(1, max_width) - elif isinstance(renderable, Segment): - text, _style = renderable - width = min(max_width, len(text)) - return RenderWidth(width, width) - elif isinstance(renderable, str): - text = renderable.rstrip() - return RenderWidth(len(text), len(text)) - else: - raise errors.NotRenderableError( - f"Unable to get render width for {renderable!r}; " - "a str, Segment, or object with __console__ method is required" - ) - ''', - "python", - theme="monokai", - ) - - logger( - "Hello", path="foo.py", line_no=20, - ) - logger( - "World!", path="foo.py", line_no=20, - ) diff --git a/rich/_render_width.py b/rich/_render_width.py index e65ebb63..1f682472 100644 --- a/rich/_render_width.py +++ b/rich/_render_width.py @@ -1,7 +1,11 @@ -from typing import NamedTuple +from typing import NamedTuple, TYPE_CHECKING +from . import errors from .segment import Segment +if TYPE_CHECKING: + from .console import RenderableType + class RenderWidth(NamedTuple): """Range of widths for a renderable object.""" diff --git a/rich/console.py b/rich/console.py index 3fcf80a3..e300c6d3 100644 --- a/rich/console.py +++ b/rich/console.py @@ -48,7 +48,7 @@ from .segment import Segment if TYPE_CHECKING: # pragma: no cover from .text import Text - HighlighterType = Callable[[Union[str, Text]], Text] +HighlighterType = Callable[[Union[str, "Text"]], "Text"] JustifyValues = Optional[Literal["left", "center", "right", "full"]] @@ -570,14 +570,14 @@ class Console: append_text(renderable) elif isinstance(renderable, (int, float, bool, bytes, type(None))): append_text( - highlight(repr(renderable)) if highlight else repr(renderable) + highlight(repr(renderable)) if highlight else Text(repr(renderable)) ) elif isinstance(renderable, (Mapping, Sequence)): check_text() append(Pretty(renderable)) else: append_text( - highlight(repr(renderable)) if highlight else repr(renderable) + highlight(repr(renderable)) if highlight else Text(repr(renderable)) ) check_text() diff --git a/rich/markdown.py b/rich/markdown.py index 885a0a14..d2e2a26f 100644 --- a/rich/markdown.py +++ b/rich/markdown.py @@ -8,6 +8,7 @@ from .console import ( Console, ConsoleOptions, ConsoleRenderable, + JustifyValues, RenderResult, Segment, ) @@ -77,8 +78,7 @@ class MarkdownElement: def __console__( self, console: "Console", options: "ConsoleOptions" ) -> "RenderResult": - return - yield + return () class UnknownElement(MarkdownElement): @@ -113,12 +113,13 @@ class Paragraph(TextElement): """A Paragraph.""" style_name = "markdown.paragraph" + justify: JustifyValues @classmethod def create(cls, markdown: "Markdown", node) -> "Paragraph": - return cls(justify=markdown.justify) + return cls(justify=markdown.justify or "left") - def __init__(self, justify: str) -> None: + def __init__(self, justify: JustifyValues) -> None: self.justify = justify def __console__(self, console: Console, options: ConsoleOptions) -> RenderResult: @@ -348,7 +349,7 @@ class Markdown: self, markup: str, code_theme: str = "monokai", - justify: str = None, + justify: JustifyValues = None, style: Union[str, Style] = "none", ) -> None: """Parses the markup.""" diff --git a/rich/panel.py b/rich/panel.py index 806169ad..b216c937 100644 --- a/rich/panel.py +++ b/rich/panel.py @@ -71,7 +71,7 @@ class Panel: if self.expand: return RenderWidth(max_width, max_width) width = RenderWidth.get(self.renderable, max_width - 2).maximum + 2 - return RenderWidth.get(width, width) + return RenderWidth(width, width) if __name__ == "__main__": diff --git a/rich/style.py b/rich/style.py index 2a01934b..bdacea23 100644 --- a/rich/style.py +++ b/rich/style.py @@ -23,6 +23,11 @@ class _Bit: class Style: """A terminal style.""" + _color: Optional[Color] + _bgcolor: Optional[Color] + _attributes: int + _set_attributes: int + def __init__( self, *, diff --git a/rich/syntax.py b/rich/syntax.py index 2bc316dc..87807513 100644 --- a/rich/syntax.py +++ b/rich/syntax.py @@ -195,7 +195,7 @@ CODE = r''' yield new_line ''' -if __name__ == "__main__": +if __name__ == "__main__": # pragma: no cover syntax = Syntax(CODE, "python", dedent=True, line_numbers=True, start_line=990) @@ -208,9 +208,9 @@ if __name__ == "__main__": elapsed = int((time() - start) * 1000) print(f"{elapsed}ms") - print(Color.downgrade.cache_info()) - print(Color.parse.cache_info()) - print(Color.get_ansi_codes.cache_info()) + # print(Color.downgrade.cache_info()) + # print(Color.parse.cache_info()) + # print(Color.get_ansi_codes.cache_info()) - print(Style.parse.cache_info()) + # print(Style.parse.cache_info())