Skip to content

Commit e95af81

Browse files
authored
Use latest version of mypy and fix type hinting accordingly (#1239)
* Use latest version of mypy and fix type hinting accordingly Also: - Update Pipfile to never require mock since we only support Python 3.6+ - Remove Azure Pipelines badge from Readme and fix section links there - Added an "inv format" task to run black and isort to auto-format all code before a commit * Try to fix type errors on versions prior to 3.8 * Restored correct types in argparse_custom.py
1 parent f381948 commit e95af81

File tree

7 files changed

+26
-14
lines changed

7 files changed

+26
-14
lines changed

Pipfile

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ gnureadline = {version = "*",sys_platform = "== 'darwin'"}
2020
invoke = "*"
2121
ipython = "*"
2222
isort = "*"
23-
mock = {version = "*",markers = "python_version < '3.6'"}
2423
mypy = "*"
2524
pyreadline3 = {version = ">=3.4",sys_platform = "== 'win32'"}
2625
pytest = "*"

README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@
22

33
[![Latest Version](https://img.shields.io/pypi/v/cmd2.svg?style=flat-square&label=latest%20stable%20version)](https://pypi.python.org/pypi/cmd2/)
44
[![GitHub Actions](https://github.com/python-cmd2/cmd2/workflows/CI/badge.svg)](https://github.com/python-cmd2/cmd2/actions?query=workflow%3ACI)
5-
[![Azure Build status](https://python-cmd2.visualstudio.com/cmd2/_apis/build/status/python-cmd2.cmd2?branch=master)](https://python-cmd2.visualstudio.com/cmd2/_build/latest?definitionId=1&branch=master)
65
[![codecov](https://codecov.io/gh/python-cmd2/cmd2/branch/master/graph/badge.svg)](https://codecov.io/gh/python-cmd2/cmd2)
76
[![Documentation Status](https://readthedocs.org/projects/cmd2/badge/?version=latest)](http://cmd2.readthedocs.io/en/latest/?badge=latest)
87
<a href="https://discord.gg/RpVG6tk"><img src="https://img.shields.io/badge/chat-on%20discord-7289da.svg" alt="Chat"></a>
98

109

1110
<p align="center">
12-
<a href="#main-features">Main Features</a> •
11+
<a href="#the-developers-toolbox">Develper's Toolbox</a> •
12+
<a href="#philosophy">Philosophy</a> •
1313
<a href="#installation">Installation</a> •
14+
<a href="#documentation">Documentation</a> •
1415
<a href="#tutorials">Tutorials</a> •
16+
<a href="#hello-world">Hello World</a> •
1517
<a href="#projects-using-cmd2">Projects using cmd2</a> •
16-
<a href="#version-two-notes">Version 2.0 Notes</a>
1718
</p>
1819

1920
[![Screenshot](https://raw.githubusercontent.com/python-cmd2/cmd2/master/cmd2.png)](https://youtu.be/DDU_JH6cFsA)

cmd2/argparse_custom.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1033,7 +1033,7 @@ def _ArgumentParser_check_value(self: argparse.ArgumentParser, action: argparse.
10331033
############################################################################################################
10341034

10351035
# noinspection PyPep8Naming,PyProtectedMember
1036-
def _SubParsersAction_remove_parser(self: argparse._SubParsersAction, name: str) -> None:
1036+
def _SubParsersAction_remove_parser(self: argparse._SubParsersAction, name: str) -> None: # type: ignore
10371037
"""
10381038
Removes a sub-parser from a sub-parsers group. Used to remove subcommands from a parser.
10391039
@@ -1333,7 +1333,7 @@ def __init__(
13331333
self.set_ap_completer_type(ap_completer_type) # type: ignore[attr-defined]
13341334

13351335
# noinspection PyProtectedMember
1336-
def add_subparsers(self, **kwargs: Any) -> argparse._SubParsersAction:
1336+
def add_subparsers(self, **kwargs: Any) -> argparse._SubParsersAction: # type: ignore
13371337
"""
13381338
Custom override. Sets a default title if one was not given.
13391339

cmd2/cmd2.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -4426,7 +4426,7 @@ def do_ipy(self, _: argparse.Namespace) -> Optional[bool]: # pragma: no cover
44264426
local_vars['self'] = self
44274427

44284428
# Configure IPython
4429-
config = TraitletsLoader.Config()
4429+
config = TraitletsLoader.Config() # type: ignore
44304430
config.InteractiveShell.banner2 = (
44314431
'Entering an IPython shell. Type exit, quit, or Ctrl-D to exit.\n'
44324432
f'Run CLI commands with: {self.py_bridge_name}("command ...")\n'
@@ -5256,7 +5256,7 @@ def cmdloop(self, intro: Optional[str] = None) -> int: # type: ignore[override]
52565256
import signal
52575257

52585258
original_sigint_handler = signal.getsignal(signal.SIGINT)
5259-
signal.signal(signal.SIGINT, self.sigint_handler)
5259+
signal.signal(signal.SIGINT, self.sigint_handler) # type: ignore
52605260

52615261
# Grab terminal lock before the command line prompt has been drawn by readline
52625262
self.terminal_lock.acquire()

cmd2/utils.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,10 @@
4141
if TYPE_CHECKING: # pragma: no cover
4242
import cmd2 # noqa: F401
4343

44-
PopenTextIO = subprocess.Popen[bytes]
45-
44+
PopenTextIO = subprocess.Popen[str]
4645
else:
4746
PopenTextIO = subprocess.Popen
4847

49-
5048
_T = TypeVar('_T')
5149

5250

@@ -670,12 +668,15 @@ def _reader_thread_func(self, read_stdout: bool) -> None:
670668
self._write_bytes(write_stream, available)
671669

672670
@staticmethod
673-
def _write_bytes(stream: Union[StdSim, TextIO], to_write: bytes) -> None:
671+
def _write_bytes(stream: Union[StdSim, TextIO], to_write: Union[bytes, str]) -> None:
674672
"""
675673
Write bytes to a stream
676674
:param stream: the stream being written to
677675
:param to_write: the bytes being written
678676
"""
677+
if isinstance(to_write, str):
678+
to_write = to_write.encode()
679+
679680
try:
680681
stream.buffer.write(to_write)
681682
except BrokenPipeError:

setup.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
'doc8',
6969
'flake8',
7070
'invoke',
71-
'mypy==0.902',
71+
'mypy',
7272
'nox',
7373
"pytest>=4.6",
7474
'pytest-cov',
@@ -80,7 +80,7 @@
8080
],
8181
'validate': [
8282
'flake8',
83-
'mypy==0.902',
83+
'mypy',
8484
'types-pkg-resources',
8585
],
8686
}

tasks.py

+11
Original file line numberDiff line numberDiff line change
@@ -353,3 +353,14 @@ def flake8(context):
353353

354354

355355
namespace.add_task(flake8)
356+
357+
358+
# Black and isort auto-formatting
359+
@invoke.task()
360+
def format(context):
361+
"""Run black and isort auto-formatting for code style enforcement"""
362+
with context.cd(TASK_ROOT_STR):
363+
context.run("black . && isort .")
364+
365+
366+
namespace.add_task(format)

0 commit comments

Comments
 (0)