Skip to content

Commit

Permalink
Remove unused mypy ignores. (#1361)
Browse files Browse the repository at this point in the history
  • Loading branch information
kmvanbrunt authored Nov 3, 2024
1 parent aa95d05 commit ef6ba80
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/mypy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 .
2 changes: 1 addition & 1 deletion cmd2/ansi.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
cast,
)

from wcwidth import ( # type: ignore[import]
from wcwidth import ( # type: ignore[import-untyped]
wcswidth,
)

Expand Down
6 changes: 3 additions & 3 deletions cmd2/argparse_completer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions cmd2/argparse_custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -807,19 +807,19 @@ 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')

# 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:
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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}}}-*)'

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion cmd2/clipboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import typing

import pyperclip # type: ignore[import]
import pyperclip # type: ignore[import-untyped]


def get_paste_buffer() -> str:
Expand Down
30 changes: 15 additions & 15 deletions cmd2/cmd2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -2095,15 +2095,15 @@ 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:
# Check if this command should be run as a shell command
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:
Expand Down Expand Up @@ -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,
)
Expand All @@ -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

Expand Down Expand Up @@ -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,
)
Expand Down Expand Up @@ -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,
)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions cmd2/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__
Expand Down Expand Up @@ -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) :]

Expand Down
2 changes: 1 addition & 1 deletion cmd2/parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cmd2/table_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
Union,
)

from wcwidth import ( # type: ignore[import]
from wcwidth import ( # type: ignore[import-untyped]
wcwidth,
)

Expand Down
4 changes: 2 additions & 2 deletions cmd2/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand Down

0 comments on commit ef6ba80

Please sign in to comment.