diff --git a/run-tests.xsh b/run-tests.xsh index 0dff1e377..0df509363 100755 --- a/run-tests.xsh +++ b/run-tests.xsh @@ -66,12 +66,13 @@ def qa(): $XONSH_TRACE_SUBPROC_FUNC = colored_tracer $XONSH_TRACE_SUBPROC = True - black --check xonsh xontrib tests + black --check xonsh xontrib tests xompletions python -m flake8 - # todo: add xontrib folder here mypy xonsh + mypy xontrib --namespace-packages --explicit-package-bases + mypy xompletions --namespace-packages --explicit-package-bases pytest -m news diff --git a/setup.cfg b/setup.cfg index 214d6ab51..040da4c48 100644 --- a/setup.cfg +++ b/setup.cfg @@ -100,8 +100,12 @@ cache_dir = .cache/mypy/ # warn_unused_ignores = True warn_unused_configs = True warn_no_return = False + ; a regex to exclude certain directories -exclude = ((xonsh/ply)|(__amalgam__.py)) +exclude = ((xonsh/ply)|(__amalgam__.py)|(xontrib/(mpl.*py|distributed.py|jedi.py))) + +;match dmypy semantics - https://github.com/python/mypy/issues/8046 +local_partial_types = True # report show_error_context = True diff --git a/xonsh/cli_utils.py b/xonsh/cli_utils.py index cad81dbb0..6013deb03 100644 --- a/xonsh/cli_utils.py +++ b/xonsh/cli_utils.py @@ -604,6 +604,7 @@ class ArgParserAlias: def hook_post_add_argument( self, + *, parser: "ArgParser|ap.ArgumentParser", action: "ap.Action", param: str, diff --git a/xonsh/parsers/completion_context.py b/xonsh/parsers/completion_context.py index b2674fe4c..fd8fbcaee 100644 --- a/xonsh/parsers/completion_context.py +++ b/xonsh/parsers/completion_context.py @@ -154,7 +154,7 @@ class CompletionContext(NamedTuple): class ExpansionOperation(enum.Enum): NEVER_EXPAND = object() - SIMPLE_ARG_EXPANSION = None # the default + SIMPLE_ARG_EXPANSION: "Any" = None # the default class Missing(enum.Enum): diff --git a/xonsh/proc.py b/xonsh/proc.py index 496b1d8a6..c9e9b46a6 100644 --- a/xonsh/proc.py +++ b/xonsh/proc.py @@ -3,10 +3,10 @@ from xonsh.lazyasd import lazyobject from xonsh.tools import print_warning -_WARNINGS_PRINTED = set() +_WARNINGS_PRINTED: "set[str]" = set() -def _print_proc_warning(msg): +def _print_proc_warning(msg: str): global _WARNINGS_PRINTED if msg not in _WARNINGS_PRINTED: print_warning(msg) diff --git a/xonsh/pygments_cache.py b/xonsh/pygments_cache.py index 9eb37ab83..cb61a9e18 100644 --- a/xonsh/pygments_cache.py +++ b/xonsh/pygments_cache.py @@ -15,12 +15,16 @@ The cache file is created on first use, if it does not already exist. """ import os import importlib +import typing as tp + +if tp.TYPE_CHECKING: + from pygments.style import Style # Global storage variables __version__ = "0.1.1" -CACHE = None -CUSTOM_STYLES = {} +CACHE: "dict[str, tp.Any] | None" = None +CUSTOM_STYLES: "dict[str, Style]" = {} DEBUG = False @@ -267,14 +271,14 @@ def cache_filename(): ) -def add_custom_style(name, style): +def add_custom_style(name: str, style: "Style"): """Register custom style to be able to retrieve it by ``get_style_by_name``. Parameters ---------- - name : str + name Style name. - style : pygments.Style + style Custom style to add. """ CUSTOM_STYLES[name] = style diff --git a/xonsh/readline_shell.py b/xonsh/readline_shell.py index fe8a69891..c801b8007 100644 --- a/xonsh/readline_shell.py +++ b/xonsh/readline_shell.py @@ -17,6 +17,8 @@ import shutil import importlib import threading import collections +import typing as tp + import xonsh.completers.tools as xct from xonsh.built_ins import XSH @@ -46,12 +48,14 @@ from xonsh.platform import ( from xonsh.lazyimps import pygments, pyghooks, winutils from xonsh.events import events -readline = None -RL_COMPLETION_SUPPRESS_APPEND = RL_LIB = RL_STATE = None -RL_COMPLETION_QUERY_ITEMS = None +if tp.TYPE_CHECKING: + from types import ModuleType + +readline: "ModuleType|None" = None +RL_COMPLETION_SUPPRESS_APPEND = RL_LIB = RL_STATE = None # type: tp.Any +RL_COMPLETION_QUERY_ITEMS: "tp.Any" = None RL_CAN_RESIZE = False -RL_DONE = None -RL_VARIABLE_VALUE = None +RL_VARIABLE_VALUE: "tp.Callable[..., tp.Any]|None" = None _RL_STATE_DONE = 0x1000000 _RL_STATE_ISEARCH = 0x0000080 diff --git a/xonsh/style_tools.py b/xonsh/style_tools.py index 328e85b57..002f9dbe1 100644 --- a/xonsh/style_tools.py +++ b/xonsh/style_tools.py @@ -15,7 +15,7 @@ class _TokenType(tuple): See https://bitbucket.org/birkenfeld/pygments-main/raw/05818a4ef9891d9ac22c851f7b3ea4b4fce460ab/AUTHORS """ - parent = None + parent: "_TokenType|None" = None def split(self): buf = [] diff --git a/xontrib/abbrevs.py b/xontrib/abbrevs.py index 87f75f557..8d57c2e4e 100644 --- a/xontrib/abbrevs.py +++ b/xontrib/abbrevs.py @@ -37,10 +37,22 @@ from xonsh.tools import check_for_partial_string __all__ = () -# todo: do not assign .abbrevs and directly use abbrevs as mutable const. -XSH.abbrevs = abbrevs = dict() + +if tp.TYPE_CHECKING: + + class AbbrCallType(tp.Protocol): + def __call__(self, word: str, buffer: Buffer) -> str: + ... + + AbbrValType = tp.Union[str, AbbrCallType] + +abbrevs: "dict[str, AbbrValType]" = dict() + +# XSH.builtins is a namespace and extendable +XSH.builtins.abbrevs = abbrevs + proxy = DynamicAccessProxy("abbrevs", "__xonsh__.abbrevs") -builtins.abbrevs = proxy +builtins.abbrevs = proxy # type: ignore class _LastExpanded(tp.NamedTuple): diff --git a/xontrib/autovox.py b/xontrib/autovox.py index cc5d3e705..c36232a90 100644 --- a/xontrib/autovox.py +++ b/xontrib/autovox.py @@ -17,8 +17,6 @@ from xonsh.built_ins import XSH __all__ = () -_policies = [] - XSH.builtins.events.doc( "autovox_policy", """ diff --git a/xontrib/vox.py b/xontrib/vox.py index 05cf202ba..3d5dac128 100644 --- a/xontrib/vox.py +++ b/xontrib/vox.py @@ -229,7 +229,7 @@ class VoxHandler(xcli.ArgParserAlias): def remove( self, names: xcli.Annotated[ - list, + tp.List[str], xcli.Arg(metavar="ENV", nargs="+", completer=venv_names_completer), ], force=False, @@ -274,7 +274,7 @@ class VoxHandler(xcli.ArgParserAlias): try: return subprocess.check_call( - [command] + list(args), shell=ON_WINDOWS, env=env, **kwargs + [command] + list(args), shell=bool(ON_WINDOWS), env=env, **kwargs ) # need to have shell=True on windows, otherwise the PYTHONPATH # won't inherit the PATH @@ -289,7 +289,7 @@ class VoxHandler(xcli.ArgParserAlias): str, xcli.Arg(completer=venv_names_completer), ], - args: xcli.Annotated[tp.List[str], xcli.Arg(nargs="...")], + args: xcli.Annotated[tp.Sequence[str], xcli.Arg(nargs="...")], ): """Run the command in the given environment @@ -311,7 +311,7 @@ class VoxHandler(xcli.ArgParserAlias): def runin_all( self, - args: xcli.Annotated[tp.List[str], xcli.Arg(nargs="...")], + args: xcli.Annotated[tp.Sequence[str], xcli.Arg(nargs="...")], ): """Run the command in all environments found under $VIRTUALENV_HOME