Skip to content

Commit 579b6ac

Browse files
committed
pyupgrade
1 parent 1641cae commit 579b6ac

File tree

8 files changed

+49
-70
lines changed

8 files changed

+49
-70
lines changed

rich/_inspect.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from __future__ import absolute_import
2-
31
import inspect
42
from inspect import cleandoc, getdoc, getfile, isclass, ismodule, signature
53
from typing import Any, Collection, Iterable, Optional, Tuple, Type, Union

rich/console.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2165,7 +2165,7 @@ def save_text(self, path: str, *, clear: bool = True, styles: bool = False) -> N
21652165
21662166
"""
21672167
text = self.export_text(clear=clear, styles=styles)
2168-
with open(path, "wt", encoding="utf-8") as write_file:
2168+
with open(path, "w", encoding="utf-8") as write_file:
21692169
write_file.write(text)
21702170

21712171
def export_html(
@@ -2271,7 +2271,7 @@ def save_html(
22712271
code_format=code_format,
22722272
inline_styles=inline_styles,
22732273
)
2274-
with open(path, "wt", encoding="utf-8") as write_file:
2274+
with open(path, "w", encoding="utf-8") as write_file:
22752275
write_file.write(html)
22762276

22772277
def export_svg(
@@ -2560,7 +2560,7 @@ def save_svg(
25602560
font_aspect_ratio=font_aspect_ratio,
25612561
unique_id=unique_id,
25622562
)
2563-
with open(path, "wt", encoding="utf-8") as write_file:
2563+
with open(path, "w", encoding="utf-8") as write_file:
25642564
write_file.write(svg)
25652565

25662566

rich/filesize.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# coding: utf-8
21
"""Functions for reporting filesizes. Borrowed from https://github.com/PyFilesystem/pyfilesystem2
32
43
The functions declared in this module should cover the different
@@ -27,7 +26,7 @@ def _to_str(
2726
if size == 1:
2827
return "1 byte"
2928
elif size < base:
30-
return "{:,} bytes".format(size)
29+
return f"{size:,} bytes"
3130

3231
for i, suffix in enumerate(suffixes, 2): # noqa: B007
3332
unit = base**i

rich/markdown.py

Lines changed: 41 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
import sys
4-
from typing import ClassVar, Dict, Iterable, List, Optional, Type, Union
4+
from typing import ClassVar, Iterable
55

66
from markdown_it import MarkdownIt
77
from markdown_it.token import Token
@@ -31,7 +31,7 @@ class MarkdownElement:
3131
new_line: ClassVar[bool] = True
3232

3333
@classmethod
34-
def create(cls, markdown: "Markdown", token: Token) -> "MarkdownElement":
34+
def create(cls, markdown: Markdown, token: Token) -> MarkdownElement:
3535
"""Factory to create markdown element,
3636
3737
Args:
@@ -43,30 +43,28 @@ def create(cls, markdown: "Markdown", token: Token) -> "MarkdownElement":
4343
"""
4444
return cls()
4545

46-
def on_enter(self, context: "MarkdownContext") -> None:
46+
def on_enter(self, context: MarkdownContext) -> None:
4747
"""Called when the node is entered.
4848
4949
Args:
5050
context (MarkdownContext): The markdown context.
5151
"""
5252

53-
def on_text(self, context: "MarkdownContext", text: TextType) -> None:
53+
def on_text(self, context: MarkdownContext, text: TextType) -> None:
5454
"""Called when text is parsed.
5555
5656
Args:
5757
context (MarkdownContext): The markdown context.
5858
"""
5959

60-
def on_leave(self, context: "MarkdownContext") -> None:
60+
def on_leave(self, context: MarkdownContext) -> None:
6161
"""Called when the parser leaves the element.
6262
6363
Args:
6464
context (MarkdownContext): [description]
6565
"""
6666

67-
def on_child_close(
68-
self, context: "MarkdownContext", child: "MarkdownElement"
69-
) -> bool:
67+
def on_child_close(self, context: MarkdownContext, child: MarkdownElement) -> bool:
7068
"""Called when a child element is closed.
7169
7270
This method allows a parent element to take over rendering of its children.
@@ -81,8 +79,8 @@ def on_child_close(
8179
return True
8280

8381
def __rich_console__(
84-
self, console: "Console", options: "ConsoleOptions"
85-
) -> "RenderResult":
82+
self, console: Console, options: ConsoleOptions
83+
) -> RenderResult:
8684
return ()
8785

8886

@@ -100,14 +98,14 @@ class TextElement(MarkdownElement):
10098

10199
style_name = "none"
102100

103-
def on_enter(self, context: "MarkdownContext") -> None:
101+
def on_enter(self, context: MarkdownContext) -> None:
104102
self.style = context.enter_style(self.style_name)
105103
self.text = Text(justify="left")
106104

107-
def on_text(self, context: "MarkdownContext", text: TextType) -> None:
105+
def on_text(self, context: MarkdownContext, text: TextType) -> None:
108106
self.text.append(text, context.current_style if isinstance(text, str) else None)
109107

110-
def on_leave(self, context: "MarkdownContext") -> None:
108+
def on_leave(self, context: MarkdownContext) -> None:
111109
context.leave_style()
112110

113111

@@ -118,7 +116,7 @@ class Paragraph(TextElement):
118116
justify: JustifyMethod
119117

120118
@classmethod
121-
def create(cls, markdown: "Markdown", token: Token) -> "Paragraph":
119+
def create(cls, markdown: Markdown, token: Token) -> Paragraph:
122120
return cls(justify=markdown.justify or "left")
123121

124122
def __init__(self, justify: JustifyMethod) -> None:
@@ -135,10 +133,10 @@ class Heading(TextElement):
135133
"""A heading."""
136134

137135
@classmethod
138-
def create(cls, markdown: "Markdown", token: Token) -> "Heading":
136+
def create(cls, markdown: Markdown, token: Token) -> Heading:
139137
return cls(token.tag)
140138

141-
def on_enter(self, context: "MarkdownContext") -> None:
139+
def on_enter(self, context: MarkdownContext) -> None:
142140
self.text = Text()
143141
context.enter_style(self.style_name)
144142

@@ -172,7 +170,7 @@ class CodeBlock(TextElement):
172170
style_name = "markdown.code_block"
173171

174172
@classmethod
175-
def create(cls, markdown: "Markdown", token: Token) -> "CodeBlock":
173+
def create(cls, markdown: Markdown, token: Token) -> CodeBlock:
176174
node_info = token.info or ""
177175
lexer_name = node_info.partition(" ")[0]
178176
return cls(lexer_name or "text", markdown.code_theme)
@@ -199,9 +197,7 @@ class BlockQuote(TextElement):
199197
def __init__(self) -> None:
200198
self.elements: Renderables = Renderables()
201199

202-
def on_child_close(
203-
self, context: "MarkdownContext", child: "MarkdownElement"
204-
) -> bool:
200+
def on_child_close(self, context: MarkdownContext, child: MarkdownElement) -> bool:
205201
self.elements.append(child)
206202
return False
207203

@@ -238,9 +234,7 @@ def __init__(self) -> None:
238234
self.header: TableHeaderElement | None = None
239235
self.body: TableBodyElement | None = None
240236

241-
def on_child_close(
242-
self, context: "MarkdownContext", child: "MarkdownElement"
243-
) -> bool:
237+
def on_child_close(self, context: MarkdownContext, child: MarkdownElement) -> bool:
244238
if isinstance(child, TableHeaderElement):
245239
self.header = child
246240
elif isinstance(child, TableBodyElement):
@@ -272,9 +266,7 @@ class TableHeaderElement(MarkdownElement):
272266
def __init__(self) -> None:
273267
self.row: TableRowElement | None = None
274268

275-
def on_child_close(
276-
self, context: "MarkdownContext", child: "MarkdownElement"
277-
) -> bool:
269+
def on_child_close(self, context: MarkdownContext, child: MarkdownElement) -> bool:
278270
assert isinstance(child, TableRowElement)
279271
self.row = child
280272
return False
@@ -286,9 +278,7 @@ class TableBodyElement(MarkdownElement):
286278
def __init__(self) -> None:
287279
self.rows: list[TableRowElement] = []
288280

289-
def on_child_close(
290-
self, context: "MarkdownContext", child: "MarkdownElement"
291-
) -> bool:
281+
def on_child_close(self, context: MarkdownContext, child: MarkdownElement) -> bool:
292282
assert isinstance(child, TableRowElement)
293283
self.rows.append(child)
294284
return False
@@ -298,11 +288,9 @@ class TableRowElement(MarkdownElement):
298288
"""MarkdownElement corresponding to `tr_open` and `tr_close`."""
299289

300290
def __init__(self) -> None:
301-
self.cells: List[TableDataElement] = []
291+
self.cells: list[TableDataElement] = []
302292

303-
def on_child_close(
304-
self, context: "MarkdownContext", child: "MarkdownElement"
305-
) -> bool:
293+
def on_child_close(self, context: MarkdownContext, child: MarkdownElement) -> bool:
306294
assert isinstance(child, TableDataElement)
307295
self.cells.append(child)
308296
return False
@@ -313,7 +301,7 @@ class TableDataElement(MarkdownElement):
313301
and `th_open` and `th_close`."""
314302

315303
@classmethod
316-
def create(cls, markdown: "Markdown", token: Token) -> "MarkdownElement":
304+
def create(cls, markdown: Markdown, token: Token) -> MarkdownElement:
317305
style = str(token.attrs.get("style")) or ""
318306

319307
justify: JustifyMethod
@@ -333,7 +321,7 @@ def __init__(self, justify: JustifyMethod) -> None:
333321
self.content: Text = Text("", justify=justify)
334322
self.justify = justify
335323

336-
def on_text(self, context: "MarkdownContext", text: TextType) -> None:
324+
def on_text(self, context: MarkdownContext, text: TextType) -> None:
337325
text = Text(text) if isinstance(text, str) else text
338326
text.stylize(context.current_style)
339327
self.content.append_text(text)
@@ -343,17 +331,15 @@ class ListElement(MarkdownElement):
343331
"""A list element."""
344332

345333
@classmethod
346-
def create(cls, markdown: "Markdown", token: Token) -> "ListElement":
334+
def create(cls, markdown: Markdown, token: Token) -> ListElement:
347335
return cls(token.type, int(token.attrs.get("start", 1)))
348336

349337
def __init__(self, list_type: str, list_start: int | None) -> None:
350-
self.items: List[ListItem] = []
338+
self.items: list[ListItem] = []
351339
self.list_type = list_type
352340
self.list_start = list_start
353341

354-
def on_child_close(
355-
self, context: "MarkdownContext", child: "MarkdownElement"
356-
) -> bool:
342+
def on_child_close(self, context: MarkdownContext, child: MarkdownElement) -> bool:
357343
assert isinstance(child, ListItem)
358344
self.items.append(child)
359345
return False
@@ -381,9 +367,7 @@ class ListItem(TextElement):
381367
def __init__(self) -> None:
382368
self.elements: Renderables = Renderables()
383369

384-
def on_child_close(
385-
self, context: "MarkdownContext", child: "MarkdownElement"
386-
) -> bool:
370+
def on_child_close(self, context: MarkdownContext, child: MarkdownElement) -> bool:
387371
self.elements.append(child)
388372
return False
389373

@@ -419,7 +403,7 @@ def render_number(
419403

420404
class Link(TextElement):
421405
@classmethod
422-
def create(cls, markdown: "Markdown", token: Token) -> "MarkdownElement":
406+
def create(cls, markdown: Markdown, token: Token) -> MarkdownElement:
423407
url = token.attrs.get("href", "#")
424408
return cls(token.content, str(url))
425409

@@ -434,7 +418,7 @@ class ImageItem(TextElement):
434418
new_line = False
435419

436420
@classmethod
437-
def create(cls, markdown: "Markdown", token: Token) -> "MarkdownElement":
421+
def create(cls, markdown: Markdown, token: Token) -> MarkdownElement:
438422
"""Factory to create markdown element,
439423
440424
Args:
@@ -449,10 +433,10 @@ def create(cls, markdown: "Markdown", token: Token) -> "MarkdownElement":
449433
def __init__(self, destination: str, hyperlinks: bool) -> None:
450434
self.destination = destination
451435
self.hyperlinks = hyperlinks
452-
self.link: Optional[str] = None
436+
self.link: str | None = None
453437
super().__init__()
454438

455-
def on_enter(self, context: "MarkdownContext") -> None:
439+
def on_enter(self, context: MarkdownContext) -> None:
456440
self.link = context.current_style.link
457441
self.text = Text(justify="left")
458442
super().on_enter(context)
@@ -476,15 +460,15 @@ def __init__(
476460
console: Console,
477461
options: ConsoleOptions,
478462
style: Style,
479-
inline_code_lexer: Optional[str] = None,
463+
inline_code_lexer: str | None = None,
480464
inline_code_theme: str = "monokai",
481465
) -> None:
482466
self.console = console
483467
self.options = options
484468
self.style_stack: StyleStack = StyleStack(style)
485469
self.stack: Stack[MarkdownElement] = Stack()
486470

487-
self._syntax: Optional[Syntax] = None
471+
self._syntax: Syntax | None = None
488472
if inline_code_lexer is not None:
489473
self._syntax = Syntax("", inline_code_lexer, theme=inline_code_theme)
490474

@@ -504,7 +488,7 @@ def on_text(self, text: str, node_type: str) -> None:
504488
else:
505489
self.stack.top.on_text(self, text)
506490

507-
def enter_style(self, style_name: Union[str, Style]) -> Style:
491+
def enter_style(self, style_name: str | Style) -> Style:
508492
"""Enter a style context."""
509493
style = self.console.get_style(style_name, default="none")
510494
self.style_stack.push(style)
@@ -531,7 +515,7 @@ class Markdown(JupyterMixin):
531515
highlighting, or None for no highlighting. Defaults to None.
532516
"""
533517

534-
elements: ClassVar[Dict[str, Type[MarkdownElement]]] = {
518+
elements: ClassVar[dict[str, type[MarkdownElement]]] = {
535519
"paragraph_open": Paragraph,
536520
"heading_open": Heading,
537521
"fence": CodeBlock,
@@ -556,17 +540,17 @@ def __init__(
556540
self,
557541
markup: str,
558542
code_theme: str = "monokai",
559-
justify: Optional[JustifyMethod] = None,
560-
style: Union[str, Style] = "none",
543+
justify: JustifyMethod | None = None,
544+
style: str | Style = "none",
561545
hyperlinks: bool = True,
562-
inline_code_lexer: Optional[str] = None,
563-
inline_code_theme: Optional[str] = None,
546+
inline_code_lexer: str | None = None,
547+
inline_code_theme: str | None = None,
564548
) -> None:
565549
parser = MarkdownIt().enable("strikethrough").enable("table")
566550
self.markup = markup
567551
self.parsed = parser.parse(markup)
568552
self.code_theme = code_theme
569-
self.justify: Optional[JustifyMethod] = justify
553+
self.justify: JustifyMethod | None = justify
570554
self.style = style
571555
self.hyperlinks = hyperlinks
572556
self.inline_code_lexer = inline_code_lexer
@@ -772,7 +756,7 @@ def __rich_console__(
772756
if args.path == "-":
773757
markdown_body = sys.stdin.read()
774758
else:
775-
with open(args.path, "rt", encoding="utf-8") as markdown_file:
759+
with open(args.path, encoding="utf-8") as markdown_file:
776760
markdown_body = markdown_file.read()
777761

778762
markdown = Markdown(

rich/progress.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1326,7 +1326,7 @@ def open(
13261326
# normalize the mode (always rb, rt)
13271327
_mode = "".join(sorted(mode, reverse=False))
13281328
if _mode not in ("br", "rt", "r"):
1329-
raise ValueError("invalid mode {!r}".format(mode))
1329+
raise ValueError(f"invalid mode {mode!r}")
13301330

13311331
# patch buffering to provide the same behaviour as the builtin `open`
13321332
line_buffering = buffering == 1

rich/progress_bar.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def _get_pulse_segments(
108108

109109
for index in range(PULSE_SIZE):
110110
position = index / PULSE_SIZE
111-
fade = 0.5 + cos((position * pi * 2)) / 2.0
111+
fade = 0.5 + cos(position * pi * 2) / 2.0
112112
color = blend_rgb(fore_color, back_color, cross_fade=fade)
113113
append(_Segment(bar, _Style(color=from_triplet(color))))
114114
return segments

0 commit comments

Comments
 (0)