Implement proper editor integration for mypy

This commit is contained in:
Kovid Goyal 2020-03-08 14:39:46 +05:30
parent d84cff0baf
commit 7057bc663e
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
6 changed files with 37 additions and 42 deletions

View File

@ -290,11 +290,10 @@ def escape(chars):
def functions_for(args): def functions_for(args):
post_processors = [] post_processors = []
if args.type == 'url': if args.type == 'url':
url_prefixes = args.url_prefixes if args.url_prefixes == 'default':
if url_prefixes == 'default':
url_prefixes = kitty_common_opts().get('url_prefixes', ('https', 'http', 'file', 'ftp')) url_prefixes = kitty_common_opts().get('url_prefixes', ('https', 'http', 'file', 'ftp'))
else: else:
url_prefixes = url_prefixes.split(',') url_prefixes = args.url_prefixes.split(',')
from .url_regex import url_delimiters from .url_regex import url_delimiters
pattern = '(?:{})://[^{}]{{3,}}'.format( pattern = '(?:{})://[^{}]{{3,}}'.format(
'|'.join(url_prefixes), url_delimiters '|'.join(url_prefixes), url_delimiters
@ -523,11 +522,11 @@ help_text = 'Select text from the screen using the keyboard. Defaults to searchi
usage = '' 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) return parse_args(args, OPTIONS, usage, help_text, 'kitty +kitten hints', result_class=HintsCLIOptions)
def main(args): def main(args: List[str]):
text = '' text = ''
if sys.stdin.isatty(): if sys.stdin.isatty():
if '--help' not in args and '-h' not in args: 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') text = sys.stdin.buffer.read().decode('utf-8')
sys.stdin = open(os.ctermid()) sys.stdin = open(os.ctermid())
try: try:
args, items = parse_hints_args(args[1:]) opts, items = parse_hints_args(args[1:])
except SystemExit as e: except SystemExit as e:
if e.code != 0: if e.code != 0:
print(e.args[0], file=sys.stderr) print(e.args[0], file=sys.stderr)
input(_('Press Enter to quit')) input(_('Press Enter to quit'))
return 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) print('Extra command line arguments present: {}'.format(' '.join(items)), file=sys.stderr)
input(_('Press Enter to quit')) 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): def linenum_handle_result(args, data, target_window_id, boss, extra_cli_args, *a):

View File

@ -6,7 +6,7 @@
import os import os
from gettext import gettext as _ from gettext import gettext as _
from typing import ( 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 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: 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=_(''' 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 return 0
if x == 'background': if x == 'background':
return 1 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=_(''' o('macos_titlebar_color', 'system', option_type=macos_titlebar_color, long_text=_('''

14
mypy-editor-integration Executable file
View File

@ -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)

View File

@ -1,6 +1,7 @@
" Scan the following dirs recursively for tags " Scan the following dirs recursively for tags
let g:project_tags_dirs = ['kitty', 'kittens'] 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' let g:ycm_python_binary_path = 'python3'
set wildignore+==template.py set wildignore+==template.py
set wildignore+=tags set wildignore+=tags

View File

@ -13,25 +13,6 @@ blank_line_before_nested_class_or_def = True
combine_as_imports = True combine_as_imports = True
multi_line_output = 5 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] [mypy]
files = kitty,kittens,glfw,*.py,docs/conf.py files = kitty,kittens,glfw,*.py,docs/conf.py
no_implicit_optional = True no_implicit_optional = True
@ -42,4 +23,6 @@ warn_unused_ignores = True
warn_return_any = True warn_return_any = True
warn_unreachable = True warn_unreachable = True
warn_no_return = False warn_no_return = False
# check_untyped_defs = True warn_unused_configs = True
check_untyped_defs = True
# disallow_untyped_defs = True

18
test.py
View File

@ -2,15 +2,16 @@
# vim:fileencoding=utf-8 # vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net> # License: GPL v3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
import unittest import importlib
import os import os
import sys import sys
import importlib import unittest
from typing import NoReturn
base = os.path.dirname(os.path.abspath(__file__)) base = os.path.dirname(os.path.abspath(__file__))
def init_env(): def init_env() -> None:
sys.path.insert(0, base) sys.path.insert(0, base)
@ -65,16 +66,13 @@ def filter_tests_by_module(suite, *names):
return filter_tests(suite, q) return filter_tests(suite, q)
def type_check(): def type_check() -> NoReturn:
init_env() init_env()
from kitty.cli_stub import generate_stub from kitty.cli_stub import generate_stub # type:ignore
generate_stub() generate_stub()
from kitty.options_stub import generate_stub from kitty.options_stub import generate_stub # type: ignore
generate_stub() generate_stub()
if 'CI' in os.environ: os.execlp('mypy', 'mypy', '--pretty')
os.execlp('mypy', 'mypy')
else:
os.execlp('dmypy', 'dmypy', 'run', '--', '--follow-imports=error')
def run_tests(): def run_tests():