[python] enable strict typechecking

This commit is contained in:
Dmytro Meleshko 2021-06-04 13:51:03 +03:00
parent 20c3f3b524
commit b0f7307999
6 changed files with 25 additions and 30 deletions

View file

@ -3,7 +3,7 @@
import json
import os
from abc import abstractmethod
from typing import Dict, Iterator, List, Protocol, TextIO, runtime_checkable
from typing import Dict, Iterator, List, Protocol, TextIO
__dir__ = os.path.dirname(__file__)
@ -60,23 +60,10 @@ BASE16_TO_ANSI_MAPPING: List[int] = [
ANSI_TO_BASE16_MAPPING: List[int] = [BASE16_TO_ANSI_MAPPING.index(i) for i in range(16)]
@runtime_checkable
class Theme(Protocol):
@property
@abstractmethod
def base16_name(self) -> str:
raise NotImplementedError()
@property
@abstractmethod
def is_dark(self) -> bool:
raise NotImplementedError()
@property
@abstractmethod
def base16_colors(self) -> List[Color]:
raise NotImplementedError()
base16_name: str
is_dark: bool
base16_colors: List[Color]
@property
def name(self) -> str:
@ -152,7 +139,6 @@ class MyTheme(Theme):
]
@runtime_checkable
class ThemeGenerator(Protocol):
@abstractmethod
@ -248,7 +234,7 @@ class ThemeGeneratorVim(ThemeGenerator):
output.write("let {}base16_colors = [\n".format(namespace))
for gui_color, cterm_color in zip(theme.base16_colors, ANSI_TO_BASE16_MAPPING):
output.write(
"\\ {{'gui': '{}', 'cterm': '{:02}'}},\n".format(gui_color.css_hex, cterm_color)
"\\ {{'gui': '{}', 'cterm': '{:02}'}},\n".format(gui_color.css_hex, cterm_color),
)
output.write("\\ ]\n")
@ -293,7 +279,7 @@ class ThemeGeneratorXfceTerminal(ThemeGenerator):
output.write("TabActivityColor={}\n".format(theme.base16_colors[0x8].css_hex))
output.write("ColorBoldUseDefault=TRUE\n")
output.write(
"ColorPalette={}\n".format(";".join(color.css_hex for color in theme.ansi_colors))
"ColorPalette={}\n".format(";".join(color.css_hex for color in theme.ansi_colors)),
)
@ -340,7 +326,7 @@ class ThemeGeneratorIterm(ThemeGenerator):
def generate(self, theme: Theme, output: TextIO) -> None:
output.write('<?xml version="1.0" encoding="UTF-8"?>\n')
output.write(
'<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">\n'
'<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">\n',
)
output.write('<plist version="1.0">\n')
output.write("<dict>\n")

View file

@ -1,15 +1,17 @@
let g:coc_global_extensions += ['coc-pyright']
let g:coc_filetypes += ['python']
" let g:coc_user_config['python.autocomplete.showAdvancedMembers'] = v:false
let g:coc_user_config['python'] = {
\ 'formatting': {
\ 'provider': 'yapf',
\ 'yapfArgs': ['--style=' . simplify(g:dotfiles_dir.'/python/yapf.ini')]
\ 'yapfArgs': ['--style=' . simplify(g:dotfiles_dir.'/python/yapf.ini')],
\ },
\ 'linting': {
\ 'pylintEnabled': v:false,
\ 'flake8Enabled': v:true,
\ 'flake8Args': ['--config=' . simplify(g:dotfiles_dir.'/python/flake8.ini')],
\ },
\ 'analysis': {
\ 'typeCheckingMode': 'strict',
\ },
\ }

View file

@ -2,7 +2,7 @@ import os
import subprocess
import sys
from pathlib import Path
from typing import Iterable, NoReturn
from typing import Iterable, NoReturn, Optional
if os.name == "posix":
DOTFILES_CONFIG_DIR: Path = Path.home() / ".config" / "dotfiles"
@ -13,7 +13,11 @@ def platform_not_supported_error() -> NoReturn:
raise Exception("platform '{}' is not supported!".format(sys.platform))
def run_chooser(choices: Iterable[str], prompt: str = None, async_read: bool = False) -> int:
def run_chooser(
choices: Iterable[str],
prompt: Optional[str] = None,
async_read: bool = False,
) -> int:
supports_result_index = True
if os.isatty(sys.stderr.fileno()):
process_args = [
@ -36,10 +40,13 @@ def run_chooser(choices: Iterable[str], prompt: str = None, async_read: bool = F
platform_not_supported_error()
chooser_process = subprocess.Popen(process_args, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
assert chooser_process.stdin is not None
assert chooser_process.stdout is not None
with chooser_process.stdin as pipe:
for index, choice in enumerate(choices):
assert "\n" not in choice
if "\n" in choice:
raise Exception("choices can only span a single line")
if not supports_result_index:
pipe.write(str(index).encode())
pipe.write(b" ")
@ -54,7 +61,7 @@ def run_chooser(choices: Iterable[str], prompt: str = None, async_read: bool = F
return chosen_index
def send_notification(title: str, message: str, url: str = None) -> None:
def send_notification(title: str, message: str, url: Optional[str] = None) -> None:
if sys.platform == "darwin":
process_args = [
"terminal-notifier",

View file

@ -3,7 +3,7 @@ from fractions import Fraction
def factors(n: int) -> "set[int]":
result = set()
result: "set[int]" = set()
for i in range(1, int(sqrt(n)) + 1):
if n % i == 0:
result.add(i)

View file

@ -14,7 +14,7 @@ def bright_colored(string: str, *colors: str) -> str:
def colorize_percent(
percent: float, warning: float, critical: float, inverse: bool = False
percent: float, warning: float, critical: float, inverse: bool = False,
) -> str:
colors = [Fore.GREEN, Fore.YELLOW, Fore.RED]

View file

@ -13,7 +13,7 @@ from humanize import humanize_bytes, humanize_timedelta
def get_system_info() -> Tuple[List[str], List[str]]:
info_lines: List[str] = []
def info(name: str, value: str, *format_args) -> None:
def info(name: str, value: str, *format_args: object) -> None:
line = bright_colored(name + ":", Fore.YELLOW) + " " + value
if format_args:
line = line % format_args