From a9344d22ba91a89abe06e3e58a5b9940181a38a3 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Thu, 20 Oct 2022 16:40:49 -0400 Subject: [PATCH 1/2] chore: apply some refurb suggestions Signed-off-by: Henry Schreiner --- rich/color.py | 2 +- rich/console.py | 4 ++-- rich/file_proxy.py | 2 +- rich/json.py | 4 ++-- rich/markdown.py | 5 +++-- rich/progress.py | 4 ++-- rich/repr.py | 2 +- rich/segment.py | 2 +- rich/syntax.py | 4 ++-- rich/text.py | 2 +- rich/traceback.py | 6 ++---- 11 files changed, 18 insertions(+), 19 deletions(-) diff --git a/rich/color.py b/rich/color.py index ef2e895d..35281eb9 100644 --- a/rich/color.py +++ b/rich/color.py @@ -513,7 +513,7 @@ class Color(NamedTuple): def downgrade(self, system: ColorSystem) -> "Color": """Downgrade a color system to a system with fewer colors.""" - if self.type in [ColorType.DEFAULT, system]: + if self.type in (ColorType.DEFAULT, system): return self # Convert to 8-bit color from truecolor color if system == ColorSystem.EIGHT_BIT and self.system == ColorSystem.TRUECOLOR: diff --git a/rich/console.py b/rich/console.py index 36537216..08f758cd 100644 --- a/rich/console.py +++ b/rich/console.py @@ -758,7 +758,7 @@ class Console: self._is_alt_screen = False def __repr__(self) -> str: - return f"" + return f"" @property def file(self) -> IO[str]: @@ -1523,7 +1523,7 @@ class Console: if text: sep_text = Text(sep, justify=justify, end=end) append(sep_text.join(text)) - del text[:] + text.clear() for renderable in objects: renderable = rich_cast(renderable) diff --git a/rich/file_proxy.py b/rich/file_proxy.py index cc69f22f..d1640221 100644 --- a/rich/file_proxy.py +++ b/rich/file_proxy.py @@ -34,7 +34,7 @@ class FileProxy(io.TextIOBase): line, new_line, text = text.partition("\n") if new_line: lines.append("".join(buffer) + line) - del buffer[:] + buffer.clear() else: buffer.append(line) break diff --git a/rich/json.py b/rich/json.py index ed9ca1ea..24dc5e60 100644 --- a/rich/json.py +++ b/rich/json.py @@ -1,3 +1,4 @@ +from pathlib import Path from json import loads, dumps from typing import Any, Callable, Optional, Union @@ -131,8 +132,7 @@ if __name__ == "__main__": if args.path == "-": json_data = sys.stdin.read() else: - with open(args.path, "rt") as json_file: - json_data = json_file.read() + json_data = Path(args.path).read_text() except Exception as error: error_console.print(f"Unable to read {args.path!r}; {error}") sys.exit(-1) diff --git a/rich/markdown.py b/rich/markdown.py index 22d80b53..ff8e7d0e 100644 --- a/rich/markdown.py +++ b/rich/markdown.py @@ -1,3 +1,4 @@ +from pathlib import Path from typing import Any, ClassVar, Dict, List, Optional, Type, Union from commonmark.blocks import Parser @@ -598,8 +599,8 @@ if __name__ == "__main__": # pragma: no cover if args.path == "-": markdown_body = sys.stdin.read() else: - with open(args.path, "rt", encoding="utf-8") as markdown_file: - markdown_body = markdown_file.read() + markdown_body = Path(args.path).read_text(encoding="utf-8") + markdown = Markdown( markdown_body, justify="full" if args.justify else "left", diff --git a/rich/progress.py b/rich/progress.py index 9873e1a5..f426c72a 100644 --- a/rich/progress.py +++ b/rich/progress.py @@ -1342,7 +1342,7 @@ class Progress(JupyterMixin): RuntimeWarning, ) buffering = -1 - elif _mode == "rt" or _mode == "r": + elif _mode in ("rt", "r"): if buffering == 0: raise ValueError("can't have unbuffered text I/O") elif buffering == 1: @@ -1363,7 +1363,7 @@ class Progress(JupyterMixin): reader = _Reader(handle, self, task_id, close_handle=True) # wrap the reader in a `TextIOWrapper` if text mode - if mode == "r" or mode == "rt": + if mode in ("r", "rt"): return io.TextIOWrapper( reader, encoding=encoding, diff --git a/rich/repr.py b/rich/repr.py index 99e92732..9dd65cde 100644 --- a/rich/repr.py +++ b/rich/repr.py @@ -55,7 +55,7 @@ def auto( if key is None: append(repr(value)) else: - if len(default) and default[0] == value: + if default and default[0] == value: continue append(f"{key}={value!r}") else: diff --git a/rich/segment.py b/rich/segment.py index 20ccedf8..6c87a791 100644 --- a/rich/segment.py +++ b/rich/segment.py @@ -303,7 +303,7 @@ class Segment(NamedTuple): if include_new_lines: cropped_line.append(new_line_segment) yield cropped_line - del line[:] + line.clear() else: append(segment) if line: diff --git a/rich/syntax.py b/rich/syntax.py index aed041f3..d732a2bf 100644 --- a/rich/syntax.py +++ b/rich/syntax.py @@ -4,6 +4,7 @@ import re import sys import textwrap from abc import ABC, abstractmethod +from pathlib import Path from typing import ( Any, Dict, @@ -338,8 +339,7 @@ class Syntax(JupyterMixin): Returns: [Syntax]: A Syntax object that may be printed to the console """ - with open(path, "rt", encoding=encoding) as code_file: - code = code_file.read() + code = Path(path).read_text(encoding=encoding) if not lexer: lexer = cls.guess_lexer(path, code=code) diff --git a/rich/text.py b/rich/text.py index 5b5ead0f..9c6bda26 100644 --- a/rich/text.py +++ b/rich/text.py @@ -56,7 +56,7 @@ class Span(NamedTuple): return ( f"Span({self.start}, {self.end}, {self.style!r})" if (isinstance(self.style, Style) and self.style._meta) - else f"Span({self.start}, {self.end}, {repr(self.style)})" + else f"Span({self.start}, {self.end}, {self.style!r})" ) def __bool__(self) -> bool: diff --git a/rich/traceback.py b/rich/traceback.py index 712cbaf5..6e36fbd9 100644 --- a/rich/traceback.py +++ b/rich/traceback.py @@ -4,6 +4,7 @@ import os import platform import sys from dataclasses import dataclass, field +from pathlib import Path from traceback import walk_tb from types import ModuleType, TracebackType from typing import Any, Callable, Dict, Iterable, List, Optional, Sequence, Type, Union @@ -544,10 +545,7 @@ class Traceback: """ code = code_cache.get(filename) if code is None: - with open( - filename, "rt", encoding="utf-8", errors="replace" - ) as code_file: - code = code_file.read() + code = Path(filename).read_text(encoding="utf-8", errors="replace") code_cache[filename] = code return code From 6337b108233d3dcd8c94688fbae5b940d62dcbcb Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Thu, 20 Oct 2022 16:58:16 -0400 Subject: [PATCH 2/2] Update rich/console.py --- rich/console.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rich/console.py b/rich/console.py index 08f758cd..bdc0b0ce 100644 --- a/rich/console.py +++ b/rich/console.py @@ -758,7 +758,7 @@ class Console: self._is_alt_screen = False def __repr__(self) -> str: - return f"" + return f"" @property def file(self) -> IO[str]: