From 6b548c24c1765cc29c0908742ee0cb81c2f97135 Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Mon, 13 Jul 2020 20:35:48 +0100 Subject: [PATCH] doctor: better mypy detection --- my/core/__main__.py | 46 +++++++++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/my/core/__main__.py b/my/core/__main__.py index 6e107ba..09a4c45 100644 --- a/my/core/__main__.py +++ b/my/core/__main__.py @@ -2,7 +2,7 @@ import os from pathlib import Path import sys from subprocess import check_call, run, PIPE -from typing import Optional +from typing import Optional, Sequence import importlib import traceback @@ -11,6 +11,23 @@ from . import LazyLogger 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): from .preinit import 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}') env['MYPYPATH'] = mpath + + cmd = mypy_cmd() + if cmd is None: + return None mres = run([ - 'python3', '-m', 'mypy', + *cmd, '--namespace-packages', '--color-output', # not sure if works?? '--pretty', @@ -100,19 +121,16 @@ def config_check(args): info(f"config file: {cfg.__file__}") - try: - import mypy - except ImportError: - warning("mypy not found, can't check config with it") + mres = run_mypy(cfg) + if mres is None: # no mypy + return + rc = mres.returncode + if rc == 0: + info('mypy check: success') else: - mres = run_mypy(cfg) - rc = mres.returncode - if rc == 0: - 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'))) + error('mypy check: failed') + sys.stderr.write(indent(mres.stderr.decode('utf8'))) + sys.stderr.write(indent(mres.stdout.decode('utf8'))) def modules_check(args):