cli: detect config properly in mypy check
This commit is contained in:
parent
2ede5b3a5c
commit
dab29a44b5
2 changed files with 41 additions and 21 deletions
|
@ -1,4 +1,5 @@
|
||||||
import os
|
import os
|
||||||
|
from pathlib import Path
|
||||||
import sys
|
import sys
|
||||||
from subprocess import check_call, run, PIPE
|
from subprocess import check_call, run, PIPE
|
||||||
import traceback
|
import traceback
|
||||||
|
@ -12,6 +13,31 @@ class Modes:
|
||||||
CONFIG = 'config'
|
CONFIG = 'config'
|
||||||
|
|
||||||
|
|
||||||
|
def run_mypy(pkg):
|
||||||
|
from my.core.init import get_mycfg_dir
|
||||||
|
mycfg_dir = get_mycfg_dir()
|
||||||
|
# todo ugh. not sure how to extract it from pkg?
|
||||||
|
|
||||||
|
# todo dunno maybe use the same mypy config in repository?
|
||||||
|
# I'd need to install mypy.ini then??
|
||||||
|
env = {**os.environ}
|
||||||
|
mpath = env.get('MYPYPATH')
|
||||||
|
mpath = str(mycfg_dir) + ('' if mpath is None else f':{mpath}')
|
||||||
|
env['MYPYPATH'] = mpath
|
||||||
|
|
||||||
|
mres = run([
|
||||||
|
'python3', '-m', 'mypy',
|
||||||
|
'--namespace-packages',
|
||||||
|
'--color-output', # not sure if works??
|
||||||
|
'--pretty',
|
||||||
|
'--show-error-codes',
|
||||||
|
'--show-error-context',
|
||||||
|
'--check-untyped-defs',
|
||||||
|
'-p', pkg.__name__,
|
||||||
|
], stderr=PIPE, stdout=PIPE, env=env)
|
||||||
|
return mres
|
||||||
|
|
||||||
|
|
||||||
def config_check(args):
|
def config_check(args):
|
||||||
def eprint(x: str):
|
def eprint(x: str):
|
||||||
print(x, file=sys.stderr)
|
print(x, file=sys.stderr)
|
||||||
|
@ -43,19 +69,7 @@ def config_check(args):
|
||||||
except ImportError:
|
except ImportError:
|
||||||
warning("mypy not found, can't check config with it")
|
warning("mypy not found, can't check config with it")
|
||||||
else:
|
else:
|
||||||
# todo dunno maybe use the same mypy config in repository?
|
mres = run_mypy(cfg)
|
||||||
# I'd need to install mypy.ini then??
|
|
||||||
# todo how to bring it into mypypath? cooperate with core, maybe?
|
|
||||||
mres = run([
|
|
||||||
'python3', '-m', 'mypy',
|
|
||||||
'--namespace-packages',
|
|
||||||
'--color-output', # not sure if works??
|
|
||||||
'--pretty',
|
|
||||||
'--show-error-codes',
|
|
||||||
'--show-error-context',
|
|
||||||
'--check-untyped-defs',
|
|
||||||
'-p', 'my.config'
|
|
||||||
], stderr=PIPE, stdout=PIPE)
|
|
||||||
rc = mres.returncode
|
rc = mres.returncode
|
||||||
if rc == 0:
|
if rc == 0:
|
||||||
info('mypy check: success')
|
info('mypy check: success')
|
||||||
|
|
|
@ -23,15 +23,11 @@ def assign_module(parent: str, name: str, module: ModuleType) -> None:
|
||||||
|
|
||||||
del ModuleType
|
del ModuleType
|
||||||
|
|
||||||
# separate function to present namespace pollution
|
|
||||||
def setup_config() -> None:
|
|
||||||
from pathlib import Path
|
|
||||||
import sys
|
|
||||||
import os
|
|
||||||
import warnings
|
|
||||||
from typing import Optional
|
|
||||||
import appdirs # type: ignore[import]
|
|
||||||
|
|
||||||
|
def get_mycfg_dir():
|
||||||
|
import appdirs # type: ignore[import]
|
||||||
|
from pathlib import Path
|
||||||
|
import os
|
||||||
# not sure if that's necessary, i.e. could rely on PYTHONPATH instead
|
# not sure if that's necessary, i.e. could rely on PYTHONPATH instead
|
||||||
# on the other hand, by using MY_CONFIG we are guaranteed to load it from the desired path?
|
# on the other hand, by using MY_CONFIG we are guaranteed to load it from the desired path?
|
||||||
mvar = os.environ.get('MY_CONFIG')
|
mvar = os.environ.get('MY_CONFIG')
|
||||||
|
@ -39,6 +35,16 @@ def setup_config() -> None:
|
||||||
mycfg_dir = Path(mvar)
|
mycfg_dir = Path(mvar)
|
||||||
else:
|
else:
|
||||||
mycfg_dir = Path(appdirs.user_config_dir('my'))
|
mycfg_dir = Path(appdirs.user_config_dir('my'))
|
||||||
|
return mycfg_dir
|
||||||
|
|
||||||
|
|
||||||
|
# separate function to present namespace pollution
|
||||||
|
def setup_config() -> None:
|
||||||
|
import sys
|
||||||
|
import warnings
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
mycfg_dir = get_mycfg_dir()
|
||||||
|
|
||||||
if not mycfg_dir.exists():
|
if not mycfg_dir.exists():
|
||||||
warnings.warn(f"""
|
warnings.warn(f"""
|
||||||
|
|
Loading…
Add table
Reference in a new issue