From 7057bc663ec9837e7b0defd5e8638c6120b6b2c0 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 8 Mar 2020 14:39:46 +0530 Subject: [PATCH] Implement proper editor integration for mypy --- kittens/hints/main.py | 15 +++++++-------- kitty/config_data.py | 6 +++--- mypy-editor-integration | 14 ++++++++++++++ session.vim | 3 ++- setup.cfg | 23 +++-------------------- test.py | 18 ++++++++---------- 6 files changed, 37 insertions(+), 42 deletions(-) create mode 100755 mypy-editor-integration diff --git a/kittens/hints/main.py b/kittens/hints/main.py index 701cf59f4..17e75d9af 100644 --- a/kittens/hints/main.py +++ b/kittens/hints/main.py @@ -290,11 +290,10 @@ def escape(chars): def functions_for(args): post_processors = [] if args.type == 'url': - url_prefixes = args.url_prefixes - if url_prefixes == 'default': + if args.url_prefixes == 'default': url_prefixes = kitty_common_opts().get('url_prefixes', ('https', 'http', 'file', 'ftp')) else: - url_prefixes = url_prefixes.split(',') + url_prefixes = args.url_prefixes.split(',') from .url_regex import url_delimiters pattern = '(?:{})://[^{}]{{3,}}'.format( '|'.join(url_prefixes), url_delimiters @@ -523,11 +522,11 @@ help_text = 'Select text from the screen using the keyboard. Defaults to searchi usage = '' -def parse_hints_args(args): +def parse_hints_args(args: List[str]) -> Tuple[HintsCLIOptions, List[str]]: return parse_args(args, OPTIONS, usage, help_text, 'kitty +kitten hints', result_class=HintsCLIOptions) -def main(args): +def main(args: List[str]): text = '' if sys.stdin.isatty(): if '--help' not in args and '-h' not in args: @@ -538,16 +537,16 @@ def main(args): text = sys.stdin.buffer.read().decode('utf-8') sys.stdin = open(os.ctermid()) try: - args, items = parse_hints_args(args[1:]) + opts, items = parse_hints_args(args[1:]) except SystemExit as e: if e.code != 0: print(e.args[0], file=sys.stderr) input(_('Press Enter to quit')) return - if items and not (args.customize_processing or args.type == 'linenum'): + if items and not (opts.customize_processing or opts.type == 'linenum'): print('Extra command line arguments present: {}'.format(' '.join(items)), file=sys.stderr) input(_('Press Enter to quit')) - return run(args, text, items) + return run(opts, text, items) def linenum_handle_result(args, data, target_window_id, boss, extra_cli_args, *a): diff --git a/kitty/config_data.py b/kitty/config_data.py index 3fe17bc85..4b623c114 100644 --- a/kitty/config_data.py +++ b/kitty/config_data.py @@ -6,7 +6,7 @@ import os from gettext import gettext as _ from typing import ( - Any, Dict, FrozenSet, Iterable, List, Optional, Set, Tuple, TypeVar, Union, cast + Any, Dict, FrozenSet, Iterable, List, Optional, Set, Tuple, TypeVar, Union ) from . import fast_data_types as defines @@ -792,7 +792,7 @@ separated by a configurable separator, and the powerline shows the tabs as a con def tab_bar_min_tabs(x: str) -> int: - return cast(int, max(1, positive_int(x))) + return max(1, positive_int(x)) o('tab_bar_min_tabs', 2, option_type=tab_bar_min_tabs, long_text=_(''' @@ -1076,7 +1076,7 @@ def macos_titlebar_color(x: str) -> int: return 0 if x == 'background': return 1 - return cast(int, (color_as_int(to_color(x)) << 8) | 2) + return (color_as_int(to_color(x)) << 8) | 2 o('macos_titlebar_color', 'system', option_type=macos_titlebar_color, long_text=_(''' diff --git a/mypy-editor-integration b/mypy-editor-integration new file mode 100755 index 000000000..5528fcbe9 --- /dev/null +++ b/mypy-editor-integration @@ -0,0 +1,14 @@ +#!/usr/bin/env python + +import subprocess +import sys + +files = [x for x in sys.argv[1:] if not x.startswith('-')] +if not files: + raise SystemExit(subprocess.Popen(['mypy'] + sys.argv[1:]).wait()) + +output = subprocess.run('dmypy run -- --follow-imports=error --show-column-numbers --no-color-output'.split(), stdout=subprocess.PIPE).stdout +q = files[0] + ':' +for line in output.decode('utf-8').splitlines(): + if line.startswith(q): + print(line) diff --git a/session.vim b/session.vim index a18757647..727b996d8 100644 --- a/session.vim +++ b/session.vim @@ -1,6 +1,7 @@ " Scan the following dirs recursively for tags let g:project_tags_dirs = ['kitty', 'kittens'] -let g:syntastic_python_checkers = ['pylama'] +let g:syntastic_python_checkers = ['mypy', 'flake8'] +let g:syntastic_python_mypy_exec = './mypy-editor-integration' let g:ycm_python_binary_path = 'python3' set wildignore+==template.py set wildignore+=tags diff --git a/setup.cfg b/setup.cfg index ea044e589..9013e654b 100644 --- a/setup.cfg +++ b/setup.cfg @@ -13,25 +13,6 @@ blank_line_before_nested_class_or_def = True combine_as_imports = True multi_line_output = 5 -[pylama] -linters=mypy,pycodestyle,pyflakes - -[pylama:pycodestyle] -max_line_length = 120 -exclude==template.py,linux-package - -[pylama:mypy] -files = kitty,kittens,glfw,*.py,docs/conf.py -no_implicit_optional = True -sqlite_cache = True -cache_fine_grained = True -warn_redundant_casts = True -warn_unused_ignores = True -warn_return_any = True -warn_unreachable = True -warn_no_return = False -check_untyped_defs = True - [mypy] files = kitty,kittens,glfw,*.py,docs/conf.py no_implicit_optional = True @@ -42,4 +23,6 @@ warn_unused_ignores = True warn_return_any = True warn_unreachable = True warn_no_return = False -# check_untyped_defs = True +warn_unused_configs = True +check_untyped_defs = True +# disallow_untyped_defs = True diff --git a/test.py b/test.py index 7b4462fa2..5ef8c8468 100755 --- a/test.py +++ b/test.py @@ -2,15 +2,16 @@ # vim:fileencoding=utf-8 # License: GPL v3 Copyright: 2016, Kovid Goyal -import unittest +import importlib import os import sys -import importlib +import unittest +from typing import NoReturn base = os.path.dirname(os.path.abspath(__file__)) -def init_env(): +def init_env() -> None: sys.path.insert(0, base) @@ -65,16 +66,13 @@ def filter_tests_by_module(suite, *names): return filter_tests(suite, q) -def type_check(): +def type_check() -> NoReturn: init_env() - from kitty.cli_stub import generate_stub + from kitty.cli_stub import generate_stub # type:ignore generate_stub() - from kitty.options_stub import generate_stub + from kitty.options_stub import generate_stub # type: ignore generate_stub() - if 'CI' in os.environ: - os.execlp('mypy', 'mypy') - else: - os.execlp('dmypy', 'dmypy', 'run', '--', '--follow-imports=error') + os.execlp('mypy', 'mypy', '--pretty') def run_tests():