Run pyupgrade to update the codebase to python 3.9

Gets rid of a lot of typing ugliness by using type annotations on
builtin types
This commit is contained in:
Kovid Goyal
2024-07-31 07:55:27 +05:30
parent 3aac62f6c7
commit f1d0d0949b
48 changed files with 593 additions and 602 deletions

View File

@@ -3,17 +3,14 @@
import os
import re
from collections.abc import Sequence
from functools import lru_cache, partial, wraps
from string import Formatter as StringFormatter
from typing import (
Any,
Callable,
Dict,
List,
NamedTuple,
Optional,
Sequence,
Tuple,
)
from .borders import Border, BorderColor
@@ -93,12 +90,12 @@ def as_rgb(x: int) -> int:
return (x << 8) | 2
@lru_cache()
@lru_cache
def report_template_failure(template: str, e: str) -> None:
log_error(f'Invalid tab title template: "{template}" with error: {e}')
@lru_cache()
@lru_cache
def compile_template(template: str) -> Any:
try:
return compile('f"""' + template + '"""', '<template>', 'eval')
@@ -142,7 +139,7 @@ class Formatter:
@run_once
def super_sub_maps() -> Tuple[Dict[int, int], Dict[int, int]]:
def super_sub_maps() -> tuple[dict[int, int], dict[int, int]]:
import string
sup_table = str.maketrans(
string.ascii_lowercase + string.ascii_uppercase + string.digits + '+-=()',
@@ -155,7 +152,7 @@ def super_sub_maps() -> Tuple[Dict[int, int], Dict[int, int]]:
class SupSub:
def __init__(self, data: Dict[str, Any], is_subscript: bool = False):
def __init__(self, data: dict[str, Any], is_subscript: bool = False):
self.__data = data
self.__is_subscript = is_subscript
@@ -384,7 +381,7 @@ def draw_tab_with_fade(
return end
powerline_symbols: Dict[PowerlineStyle, Tuple[str, str]] = {
powerline_symbols: dict[PowerlineStyle, tuple[str, str]] = {
'slanted': ('', ''),
'round': ('', '')
}
@@ -480,8 +477,8 @@ class TabBar:
self.os_window_id = os_window_id
self.num_tabs = 1
self.data_buffer_size = 0
self.blank_rects: Tuple[Border, ...] = ()
self.cell_ranges: List[Tuple[int, int]] = []
self.blank_rects: tuple[Border, ...] = ()
self.cell_ranges: list[tuple[int, int]] = []
self.laid_out_once = False
self.apply_options()
@@ -539,7 +536,7 @@ class TabBar:
else:
self.align = lambda: None
def patch_colors(self, spec: Dict[str, Optional[int]]) -> None:
def patch_colors(self, spec: dict[str, Optional[int]]) -> None:
opts = get_options()
atf = spec.get('active_tab_foreground')
if isinstance(atf, int):
@@ -576,7 +573,7 @@ class TabBar:
self.screen.color_profile.default_bg = color_from_int(bg)
@property
def current_colors(self) -> Dict[str, Color]:
def current_colors(self) -> dict[str, Color]:
return {
'active_tab_foreground': self.draw_data.active_fg,
'inactive_tab_foreground': self.draw_data.inactive_fg,
@@ -587,7 +584,7 @@ class TabBar:
def update_blank_rects(self, central: Region, tab_bar: Region, vw: int, vh: int) -> None:
opts = get_options()
blank_rects: List[Border] = []
blank_rects: list[Border] = []
bg = BorderColor.tab_bar_margin_color if opts.tab_bar_margin_color is not None else BorderColor.default_bg
if opts.tab_bar_margin_height:
if opts.tab_bar_edge == 3: # bottom
@@ -635,7 +632,7 @@ class TabBar:
last_tab = data[-1] if data else None
ed = ExtraData()
def draw_tab(i: int, tab: TabBarData, cell_ranges: List[Tuple[int, int]], max_tab_length: int) -> None:
def draw_tab(i: int, tab: TabBarData, cell_ranges: list[tuple[int, int]], max_tab_length: int) -> None:
ed.prev_tab = data[i - 1] if i > 0 else None
ed.next_tab = data[i + 1] if i + 1 < len(data) else None
s.cursor.bg = as_rgb(self.draw_data.tab_bg(t))
@@ -684,7 +681,7 @@ class TabBar:
s.cursor.x = 0
s.erase_in_line(2, False)
cr: List[Tuple[int, int]] = []
cr: list[tuple[int, int]] = []
ed.for_layout = False
for i, t in enumerate(data):
try: