From ef6ba804c10caacb9bed17d49abf6d095dee216b Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Sun, 3 Nov 2024 07:14:20 -0500 Subject: [PATCH] Remove unused mypy ignores. (#1361) --- .github/workflows/mypy.yml | 2 +- cmd2/ansi.py | 2 +- cmd2/argparse_completer.py | 6 +++--- cmd2/argparse_custom.py | 18 +++++++++--------- cmd2/clipboard.py | 2 +- cmd2/cmd2.py | 30 +++++++++++++++--------------- cmd2/decorators.py | 4 ++-- cmd2/parsing.py | 2 +- cmd2/table_creator.py | 2 +- cmd2/utils.py | 4 ++-- 10 files changed, 36 insertions(+), 36 deletions(-) diff --git a/.github/workflows/mypy.yml b/.github/workflows/mypy.yml index 04332408..99fa843d 100644 --- a/.github/workflows/mypy.yml +++ b/.github/workflows/mypy.yml @@ -19,5 +19,5 @@ jobs: # Only a single commit is fetched by default, for the ref/SHA that triggered the workflow. # Set fetch-depth: 0 to fetch all history for all branches and tags. fetch-depth: 0 # Needed for setuptools_scm to work correctly - - run: pip install -U --user pip mypy rich rich-argparse + - run: pip install -U --user pip mypy pyperclip rich rich-argparse wcwidth - run: mypy . diff --git a/cmd2/ansi.py b/cmd2/ansi.py index 52bf382a..c0a3a2f1 100644 --- a/cmd2/ansi.py +++ b/cmd2/ansi.py @@ -17,7 +17,7 @@ cast, ) -from wcwidth import ( # type: ignore[import] +from wcwidth import ( # type: ignore[import-untyped] wcswidth, ) diff --git a/cmd2/argparse_completer.py b/cmd2/argparse_completer.py index 9bb86e9f..1e4cd8b4 100644 --- a/cmd2/argparse_completer.py +++ b/cmd2/argparse_completer.py @@ -557,7 +557,7 @@ def _format_completions(self, arg_state: _ArgumentState, completions: Union[List if not self._cmd2_app.matches_sorted: # If all orig_value types are numbers, then sort by that value if all_nums: - completion_items.sort(key=lambda c: c.orig_value) # type: ignore[no-any-return] + completion_items.sort(key=lambda c: c.orig_value) # Otherwise sort as strings else: @@ -744,12 +744,12 @@ def _complete_arg( if not arg_choices.is_completer: choices_func = arg_choices.choices_provider if isinstance(choices_func, ChoicesProviderFuncWithTokens): - completion_items = choices_func(*args, **kwargs) # type: ignore[arg-type] + completion_items = choices_func(*args, **kwargs) else: # pragma: no cover # This won't hit because runtime checking doesn't check function argument types and will always # resolve true above. Mypy, however, does see the difference and gives an error that can't be # ignored. Mypy issue #5485 discusses this problem - completion_items = choices_func(*args) # type: ignore[arg-type] + completion_items = choices_func(*args) # else case is already covered above else: completion_items = arg_choices diff --git a/cmd2/argparse_custom.py b/cmd2/argparse_custom.py index 2a49d8ea..010776c5 100644 --- a/cmd2/argparse_custom.py +++ b/cmd2/argparse_custom.py @@ -807,11 +807,11 @@ def _add_argument_wrapper( # Validate nargs tuple if ( len(nargs) != 2 - or not isinstance(nargs[0], int) # type: ignore[unreachable] - or not (isinstance(nargs[1], int) or nargs[1] == constants.INFINITY) # type: ignore[misc] + or not isinstance(nargs[0], int) + or not (isinstance(nargs[1], int) or nargs[1] == constants.INFINITY) ): raise ValueError('Ranged values for nargs must be a tuple of 1 or 2 integers') - if nargs[0] >= nargs[1]: # type: ignore[misc] + if nargs[0] >= nargs[1]: raise ValueError('Invalid nargs range. The first value must be less than the second') if nargs[0] < 0: raise ValueError('Negative numbers are invalid for nargs range') @@ -819,7 +819,7 @@ def _add_argument_wrapper( # Save the nargs tuple as our range setting nargs_range = nargs range_min = nargs_range[0] - range_max = nargs_range[1] # type: ignore[misc] + range_max = nargs_range[1] # Convert nargs into a format argparse recognizes if range_min == 0: @@ -858,7 +858,7 @@ def _add_argument_wrapper( new_arg = orig_actions_container_add_argument(self, *args, **kwargs) # Set the custom attributes - new_arg.set_nargs_range(nargs_range) # type: ignore[arg-type, attr-defined] + new_arg.set_nargs_range(nargs_range) # type: ignore[attr-defined] if choices_provider: new_arg.set_choices_provider(choices_provider) # type: ignore[attr-defined] @@ -894,7 +894,7 @@ def _get_nargs_pattern_wrapper(self: argparse.ArgumentParser, action: argparse.A if nargs_range[1] == constants.INFINITY: range_max = '' else: - range_max = nargs_range[1] # type: ignore[assignment] + range_max = nargs_range[1] nargs_pattern = f'(-*A{{{nargs_range[0]},{range_max}}}-*)' @@ -1429,10 +1429,10 @@ def format_help(self) -> str: formatter = self._get_formatter() # usage - formatter.add_usage(self.usage, self._actions, self._mutually_exclusive_groups) # type: ignore[arg-type] + formatter.add_usage(self.usage, self._actions, self._mutually_exclusive_groups) # description - formatter.add_text(self.description) # type: ignore[arg-type] + formatter.add_text(self.description) # Begin cmd2 customization (separate required and optional arguments) @@ -1473,7 +1473,7 @@ def format_help(self) -> str: # End cmd2 customization # epilog - formatter.add_text(self.epilog) # type: ignore[arg-type] + formatter.add_text(self.epilog) # determine help from format above return formatter.format_help() + '\n' diff --git a/cmd2/clipboard.py b/cmd2/clipboard.py index 454e3484..376b0015 100644 --- a/cmd2/clipboard.py +++ b/cmd2/clipboard.py @@ -5,7 +5,7 @@ import typing -import pyperclip # type: ignore[import] +import pyperclip # type: ignore[import-untyped] def get_paste_buffer() -> str: diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index 036b1419..1dc599de 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -987,7 +987,7 @@ def _unregister_subcommands(self, cmdset: Union[CommandSet, 'Cmd']) -> None: for action in command_parser._actions: if isinstance(action, argparse._SubParsersAction): - action.remove_parser(subcommand_name) # type: ignore[arg-type,attr-defined] + action.remove_parser(subcommand_name) # type: ignore[attr-defined] break @property @@ -2095,7 +2095,7 @@ def _perform_completion( completer.complete, tokens=raw_tokens[1:] if preserve_quotes else tokens[1:], cmd_set=cmd_set ) else: - completer_func = self.completedefault # type: ignore[assignment] + completer_func = self.completedefault # Not a recognized command else: @@ -2103,7 +2103,7 @@ def _perform_completion( if self.default_to_shell and command in utils.get_exes_in_path(command): completer_func = self.path_complete else: - completer_func = self.completedefault # type: ignore[assignment] + completer_func = self.completedefault # Otherwise we are completing the command token or performing custom completion else: @@ -2805,11 +2805,11 @@ def _redirect_output(self, statement: Statement) -> utils.RedirectionSavedState: kwargs['executable'] = shell # For any stream that is a StdSim, we will use a pipe so we can capture its output - proc = subprocess.Popen( # type: ignore[call-overload] + proc = subprocess.Popen( statement.pipe_to, stdin=subproc_stdin, stdout=subprocess.PIPE if isinstance(self.stdout, utils.StdSim) else self.stdout, # type: ignore[unreachable] - stderr=subprocess.PIPE if isinstance(sys.stderr, utils.StdSim) else sys.stderr, # type: ignore[unreachable] + stderr=subprocess.PIPE if isinstance(sys.stderr, utils.StdSim) else sys.stderr, shell=True, **kwargs, ) @@ -2829,7 +2829,7 @@ def _redirect_output(self, statement: Statement) -> utils.RedirectionSavedState: new_stdout.close() raise RedirectionError(f'Pipe process exited with code {proc.returncode} before command could run') else: - redir_saved_state.redirecting = True # type: ignore[unreachable] + redir_saved_state.redirecting = True cmd_pipe_proc_reader = utils.ProcReader(proc, cast(TextIO, self.stdout), sys.stderr) sys.stdout = self.stdout = new_stdout @@ -3068,7 +3068,7 @@ def complete_none(text: str, state: int) -> Optional[str]: # pragma: no cover parser.add_argument( 'arg', suppress_tab_hint=True, - choices=choices, # type: ignore[arg-type] + choices=choices, choices_provider=choices_provider, completer=completer, ) @@ -3846,7 +3846,7 @@ def complete_set_value( arg_name, metavar=arg_name, help=settable.description, - choices=settable.choices, # type: ignore[arg-type] + choices=settable.choices, choices_provider=settable.choices_provider, completer=settable.completer, ) @@ -4001,15 +4001,15 @@ def do_shell(self, args: argparse.Namespace) -> None: # still receive the SIGINT since it is in the same process group as us. with self.sigint_protection: # For any stream that is a StdSim, we will use a pipe so we can capture its output - proc = subprocess.Popen( # type: ignore[call-overload] + proc = subprocess.Popen( expanded_command, stdout=subprocess.PIPE if isinstance(self.stdout, utils.StdSim) else self.stdout, # type: ignore[unreachable] - stderr=subprocess.PIPE if isinstance(sys.stderr, utils.StdSim) else sys.stderr, # type: ignore[unreachable] + stderr=subprocess.PIPE if isinstance(sys.stderr, utils.StdSim) else sys.stderr, shell=True, **kwargs, ) - proc_reader = utils.ProcReader(proc, cast(TextIO, self.stdout), sys.stderr) # type: ignore[arg-type] + proc_reader = utils.ProcReader(proc, cast(TextIO, self.stdout), sys.stderr) proc_reader.wait() # Save the return code of the application for use in a pyscript @@ -4101,10 +4101,10 @@ def _set_up_py_shell_env(self, interp: InteractiveConsole) -> _SavedCmd2Env: self._reset_py_display() cmd2_env.sys_stdout = sys.stdout - sys.stdout = self.stdout # type: ignore[assignment] + sys.stdout = self.stdout cmd2_env.sys_stdin = sys.stdin - sys.stdin = self.stdin # type: ignore[assignment] + sys.stdin = self.stdin return cmd2_env @@ -4114,8 +4114,8 @@ def _restore_cmd2_env(self, cmd2_env: _SavedCmd2Env) -> None: :param cmd2_env: the environment settings to restore """ - sys.stdout = cmd2_env.sys_stdout # type: ignore[assignment] - sys.stdin = cmd2_env.sys_stdin # type: ignore[assignment] + sys.stdout = cmd2_env.sys_stdout + sys.stdin = cmd2_env.sys_stdin # Set up readline for cmd2 if rl_type != RlType.NONE: diff --git a/cmd2/decorators.py b/cmd2/decorators.py index d3ff8026..d210627e 100644 --- a/cmd2/decorators.py +++ b/cmd2/decorators.py @@ -184,7 +184,7 @@ def cmd_wrapper(*args: Any, **kwargs: Any) -> Optional[bool]: cmd2_app, statement = _parse_positionals(args) _, parsed_arglist = cmd2_app.statement_parser.get_command_arg_list(command_name, statement, preserve_quotes) args_list = _arg_swap(args, statement, parsed_arglist) - return func(*args_list, **kwargs) # type: ignore[call-arg] + return func(*args_list, **kwargs) command_name = func.__name__[len(constants.COMMAND_FUNC_PREFIX) :] cmd_wrapper.__doc__ = func.__doc__ @@ -383,7 +383,7 @@ def cmd_wrapper(*args: Any, **kwargs: Dict[str, Any]) -> Optional[bool]: delattr(ns, constants.NS_ATTR_SUBCMD_HANDLER) args_list = _arg_swap(args, statement_arg, *new_args) - return func(*args_list, **kwargs) # type: ignore[call-arg] + return func(*args_list, **kwargs) command_name = func.__name__[len(constants.COMMAND_FUNC_PREFIX) :] diff --git a/cmd2/parsing.py b/cmd2/parsing.py index 7ef1cc3d..7e59a558 100644 --- a/cmd2/parsing.py +++ b/cmd2/parsing.py @@ -39,7 +39,7 @@ def shlex_split(str_to_split: str) -> List[str]: @dataclass(frozen=True) -class Statement(str): # type: ignore[override] +class Statement(str): """String subclass with additional attributes to store the results of parsing. The ``cmd`` module in the standard library passes commands around as a diff --git a/cmd2/table_creator.py b/cmd2/table_creator.py index 409e7a99..8abe6626 100644 --- a/cmd2/table_creator.py +++ b/cmd2/table_creator.py @@ -24,7 +24,7 @@ Union, ) -from wcwidth import ( # type: ignore[import] +from wcwidth import ( # type: ignore[import-untyped] wcwidth, ) diff --git a/cmd2/utils.py b/cmd2/utils.py index f718d5f1..c55b721e 100644 --- a/cmd2/utils.py +++ b/cmd2/utils.py @@ -1172,7 +1172,7 @@ def categorize(func: Union[Callable[..., Any], Iterable[Callable[..., Any]]], ca setattr(item, constants.CMD_ATTR_HELP_CATEGORY, category) else: if inspect.ismethod(func): - setattr(func.__func__, constants.CMD_ATTR_HELP_CATEGORY, category) # type: ignore[attr-defined] + setattr(func.__func__, constants.CMD_ATTR_HELP_CATEGORY, category) else: setattr(func, constants.CMD_ATTR_HELP_CATEGORY, category) @@ -1192,7 +1192,7 @@ def get_defining_class(meth: Callable[..., Any]) -> Optional[Type[Any]]: if inspect.ismethod(meth) or ( inspect.isbuiltin(meth) and getattr(meth, '__self__') is not None and getattr(meth.__self__, '__class__') ): - for cls in inspect.getmro(meth.__self__.__class__): # type: ignore[attr-defined] + for cls in inspect.getmro(meth.__self__.__class__): if meth.__name__ in cls.__dict__: return cls meth = getattr(meth, '__func__', meth) # fallback to __qualname__ parsing