From b0f730799911a69d4a0a59aefa772af864350273 Mon Sep 17 00:00:00 2001 From: Dmytro Meleshko Date: Fri, 4 Jun 2021 13:51:03 +0300 Subject: [PATCH] [python] enable strict typechecking --- colorschemes/main.py | 28 +++++++------------------ nvim/coc-languages/python.vim | 6 ++++-- script-resources/common_script_utils.py | 15 +++++++++---- script-resources/pycalc_startup.py | 2 +- script-resources/welcome/colors.py | 2 +- script-resources/welcome/system_info.py | 2 +- 6 files changed, 25 insertions(+), 30 deletions(-) diff --git a/colorschemes/main.py b/colorschemes/main.py index 1c236e4..209a460 100755 --- a/colorschemes/main.py +++ b/colorschemes/main.py @@ -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('\n') output.write( - '\n' + '\n', ) output.write('\n') output.write("\n") diff --git a/nvim/coc-languages/python.vim b/nvim/coc-languages/python.vim index 49851ad..c6a7357 100644 --- a/nvim/coc-languages/python.vim +++ b/nvim/coc-languages/python.vim @@ -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', +\ }, \ } diff --git a/script-resources/common_script_utils.py b/script-resources/common_script_utils.py index 4065d99..8a4c6a9 100644 --- a/script-resources/common_script_utils.py +++ b/script-resources/common_script_utils.py @@ -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", diff --git a/script-resources/pycalc_startup.py b/script-resources/pycalc_startup.py index 269a1af..f7b5dd7 100644 --- a/script-resources/pycalc_startup.py +++ b/script-resources/pycalc_startup.py @@ -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) diff --git a/script-resources/welcome/colors.py b/script-resources/welcome/colors.py index 7d4cb76..91b9661 100644 --- a/script-resources/welcome/colors.py +++ b/script-resources/welcome/colors.py @@ -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] diff --git a/script-resources/welcome/system_info.py b/script-resources/welcome/system_info.py index 24cda1b..e1156c5 100644 --- a/script-resources/welcome/system_info.py +++ b/script-resources/welcome/system_info.py @@ -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