Skip to content

Commit 4d744ca

Browse files
Mypy types for autocomplete.py, args.py, config.py and curtsies.py (#929)
1 parent 4ffa123 commit 4d744ca

31 files changed

+427
-167
lines changed

.github/workflows/lint.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,20 @@ jobs:
2626
with:
2727
skip: '*.po'
2828
ignore_words_list: ba,te,deltion
29+
30+
mypy:
31+
runs-on: ubuntu-latest
32+
steps:
33+
- uses: actions/checkout@v2
34+
- name: Set up Python
35+
uses: actions/setup-python@v2
36+
- name: Install dependencies
37+
run: |
38+
python -m pip install --upgrade pip
39+
pip install mypy
40+
pip install -r requirements.txt
41+
pip install urwid twisted watchdog "jedi >=0.16" babel "sphinx >=1.5" numpy
42+
pip install types-backports types-requests types-setuptools types-toml types-pygments
43+
- name: Check with mypy
44+
# for now only run on a few files to avoid slipping backward
45+
run: mypy

bpython/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import os.path
2424

2525
try:
26-
from ._version import __version__ as version
26+
from ._version import __version__ as version # type: ignore
2727
except ImportError:
2828
version = "unknown"
2929

bpython/args.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,17 @@
2121
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2222
# THE SOFTWARE.
2323

24+
# To gradually migrate to mypy we aren't setting these globally yet
25+
# mypy: disallow_untyped_defs=True
26+
# mypy: disallow_untyped_calls=True
27+
2428
"""
2529
Module to handle command line argument parsing, for all front-ends.
2630
"""
2731

2832
import argparse
29-
from typing import Tuple
33+
from typing import Tuple, List, Optional, NoReturn, Callable
34+
import code
3035
import curtsies
3136
import cwcwidth
3237
import greenlet
@@ -50,11 +55,11 @@ class ArgumentParserFailed(ValueError):
5055

5156

5257
class RaisingArgumentParser(argparse.ArgumentParser):
53-
def error(self, msg):
58+
def error(self, msg: str) -> NoReturn:
5459
raise ArgumentParserFailed()
5560

5661

57-
def version_banner(base="bpython") -> str:
62+
def version_banner(base: str = "bpython") -> str:
5863
return _("{} version {} on top of Python {} {}").format(
5964
base,
6065
__version__,
@@ -67,7 +72,14 @@ def copyright_banner() -> str:
6772
return _("{} See AUTHORS.rst for details.").format(__copyright__)
6873

6974

70-
def parse(args, extras=None, ignore_stdin=False) -> Tuple:
75+
Options = Tuple[str, str, Callable[[argparse._ArgumentGroup], None]]
76+
77+
78+
def parse(
79+
args: Optional[List[str]],
80+
extras: Options = None,
81+
ignore_stdin: bool = False,
82+
) -> Tuple:
7183
"""Receive an argument list - if None, use sys.argv - parse all args and
7284
take appropriate action. Also receive optional extra argument: this should
7385
be a tuple of (title, description, callback)
@@ -197,7 +209,7 @@ def callback(group):
197209
logger.info(f"curtsies: {curtsies.__version__}")
198210
logger.info(f"cwcwidth: {cwcwidth.__version__}")
199211
logger.info(f"greenlet: {greenlet.__version__}")
200-
logger.info(f"pygments: {pygments.__version__}")
212+
logger.info(f"pygments: {pygments.__version__}") # type: ignore
201213
logger.info(f"requests: {requests.__version__}")
202214
logger.info(
203215
"environment:\n{}".format(
@@ -214,7 +226,9 @@ def callback(group):
214226
return Config(options.config), options, options.args
215227

216228

217-
def exec_code(interpreter, args):
229+
def exec_code(
230+
interpreter: code.InteractiveInterpreter, args: List[str]
231+
) -> None:
218232
"""
219233
Helper to execute code in a given interpreter, e.g. to implement the behavior of python3 [-i] file.py
220234
@@ -230,9 +244,10 @@ def exec_code(interpreter, args):
230244
old_argv, sys.argv = sys.argv, args
231245
sys.path.insert(0, os.path.abspath(os.path.dirname(args[0])))
232246
spec = importlib.util.spec_from_loader("__console__", loader=None)
247+
assert spec
233248
mod = importlib.util.module_from_spec(spec)
234249
sys.modules["__console__"] = mod
235-
interpreter.locals.update(mod.__dict__)
236-
interpreter.locals["__file__"] = args[0]
250+
interpreter.locals.update(mod.__dict__) # type: ignore # TODO use a more specific type that has a .locals attribute
251+
interpreter.locals["__file__"] = args[0] # type: ignore # TODO use a more specific type that has a .locals attribute
237252
interpreter.runsource(source, args[0], "exec")
238253
sys.argv = old_argv

0 commit comments

Comments
 (0)