typing fixes

This commit is contained in:
Will McGugan 2019-12-27 10:07:38 +00:00
parent bee681d359
commit 356bdf8d2a
8 changed files with 26 additions and 62 deletions

1
.gitignore vendored
View File

@ -2,6 +2,7 @@
.vscode
mypy_report
docs/build
docs/source/_build
# Byte-compiled / optimized / DLL files
__pycache__/

View File

@ -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,
)

View File

@ -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."""

View File

@ -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()

View File

@ -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."""

View File

@ -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__":

View File

@ -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,
*,

View File

@ -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())