doctor: better mypy detection
This commit is contained in:
parent
5eecd8721d
commit
6b548c24c1
1 changed files with 32 additions and 14 deletions
|
@ -2,7 +2,7 @@ import os
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import sys
|
import sys
|
||||||
from subprocess import check_call, run, PIPE
|
from subprocess import check_call, run, PIPE
|
||||||
from typing import Optional
|
from typing import Optional, Sequence
|
||||||
import importlib
|
import importlib
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
|
@ -11,6 +11,23 @@ from . import LazyLogger
|
||||||
log = LazyLogger('HPI cli')
|
log = LazyLogger('HPI cli')
|
||||||
|
|
||||||
|
|
||||||
|
import functools
|
||||||
|
@functools.lru_cache()
|
||||||
|
def mypy_cmd() -> Optional[Sequence[str]]:
|
||||||
|
try:
|
||||||
|
# preferrably, use mypy from current python env
|
||||||
|
import mypy
|
||||||
|
return ['python3', '-m', 'mypy']
|
||||||
|
except ImportError:
|
||||||
|
pass
|
||||||
|
# ok, not ideal but try from PATH
|
||||||
|
import shutil
|
||||||
|
if shutil.which('mypy'):
|
||||||
|
return ['mypy']
|
||||||
|
warning("mypy not found, so can't check config with it. See https://github.com/python/mypy#readme if you want to install it and retry")
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
def run_mypy(pkg):
|
def run_mypy(pkg):
|
||||||
from .preinit import get_mycfg_dir
|
from .preinit import get_mycfg_dir
|
||||||
mycfg_dir = get_mycfg_dir()
|
mycfg_dir = get_mycfg_dir()
|
||||||
|
@ -23,8 +40,12 @@ def run_mypy(pkg):
|
||||||
mpath = str(mycfg_dir) + ('' if mpath is None else f':{mpath}')
|
mpath = str(mycfg_dir) + ('' if mpath is None else f':{mpath}')
|
||||||
env['MYPYPATH'] = mpath
|
env['MYPYPATH'] = mpath
|
||||||
|
|
||||||
|
|
||||||
|
cmd = mypy_cmd()
|
||||||
|
if cmd is None:
|
||||||
|
return None
|
||||||
mres = run([
|
mres = run([
|
||||||
'python3', '-m', 'mypy',
|
*cmd,
|
||||||
'--namespace-packages',
|
'--namespace-packages',
|
||||||
'--color-output', # not sure if works??
|
'--color-output', # not sure if works??
|
||||||
'--pretty',
|
'--pretty',
|
||||||
|
@ -100,19 +121,16 @@ def config_check(args):
|
||||||
|
|
||||||
info(f"config file: {cfg.__file__}")
|
info(f"config file: {cfg.__file__}")
|
||||||
|
|
||||||
try:
|
mres = run_mypy(cfg)
|
||||||
import mypy
|
if mres is None: # no mypy
|
||||||
except ImportError:
|
return
|
||||||
warning("mypy not found, can't check config with it")
|
rc = mres.returncode
|
||||||
|
if rc == 0:
|
||||||
|
info('mypy check: success')
|
||||||
else:
|
else:
|
||||||
mres = run_mypy(cfg)
|
error('mypy check: failed')
|
||||||
rc = mres.returncode
|
sys.stderr.write(indent(mres.stderr.decode('utf8')))
|
||||||
if rc == 0:
|
sys.stderr.write(indent(mres.stdout.decode('utf8')))
|
||||||
info('mypy check: success')
|
|
||||||
else:
|
|
||||||
error('mypy check: failed')
|
|
||||||
sys.stderr.write(indent(mres.stderr.decode('utf8')))
|
|
||||||
sys.stderr.write(indent(mres.stdout.decode('utf8')))
|
|
||||||
|
|
||||||
|
|
||||||
def modules_check(args):
|
def modules_check(args):
|
||||||
|
|
Loading…
Add table
Reference in a new issue