Merge pull request #2603 from henryiii/henryiii/chore/refurb

chore: apply some refurb suggestions
This commit is contained in:
Will McGugan 2022-12-04 11:14:17 +07:00 committed by GitHub
commit 5930357761
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 18 additions and 19 deletions

View File

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

View File

@ -758,7 +758,7 @@ class Console:
self._is_alt_screen = False
def __repr__(self) -> str:
return f"<console width={self.width} {str(self._color_system)}>"
return f"<console width={self.width} {self._color_system!s}>"
@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)

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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