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 c59f0d77..38aadb3e 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 72b59ed2..4b0b0da6 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 ed2b89f3..19d1d9c4 100644 --- a/rich/progress.py +++ b/rich/progress.py @@ -1337,7 +1337,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: @@ -1358,7 +1358,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 8e18df83..9532f53b 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 @@ -549,10 +550,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