Skip to content

Commit

Permalink
west: initial support for type hints
Browse files Browse the repository at this point in the history
These changes introduce initial support for type hints. Support for mypy
static analysis on GitHub workflows has also been enabled.

Signed-off-by: Gerard Marull-Paretas <gerard@teslabs.com>
  • Loading branch information
gmarull authored and mbolivar-nordic committed Jun 2, 2020
1 parent b93ad74 commit 26b69f9
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ __pycache__/
shippable/
.tox/
.coverage*
.mypy_cache/

22 changes: 12 additions & 10 deletions src/west/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import itertools
import os
import sys
from types import ModuleType
from typing import Dict

import pykwalify
import yaml
Expand All @@ -26,6 +28,16 @@

__all__ = ['CommandContextError', 'CommandError', 'WestCommand']

_EXT_SCHEMA_PATH = os.path.join(os.path.dirname(__file__),
'west-commands-schema.yml')

# Cache which maps files implementing extension commands to their
# imported modules.
_EXT_MODULES_CACHE: Dict[str, ModuleType] = {}
# Infinite iterator of "fresh" extension command module names.
_EXT_MODULES_NAME_IT = (f'west.commands.ext.cmd_{i}'
for i in itertools.count(1))

class CommandError(RuntimeError):
'''Indicates that a command failed.'''

Expand Down Expand Up @@ -346,16 +358,6 @@ def _commands_module_from_file(file):

return mod

_EXT_SCHEMA_PATH = os.path.join(os.path.dirname(__file__),
'west-commands-schema.yml')

# Cache which maps files implementing extension commands to their
# imported modules.
_EXT_MODULES_CACHE = {}
# Infinite iterator of "fresh" extension command module names.
_EXT_MODULES_NAME_IT = (f'west.commands.ext.cmd_{i}'
for i in itertools.count(1))

class _ExtFactory:

def __init__(self, py_file, name, attr):
Expand Down
3 changes: 2 additions & 1 deletion src/west/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import colorama
import sys
from typing import NoReturn

VERBOSE_NONE = 0
'''Default verbosity level, no dbg() messages printed.'''
Expand Down Expand Up @@ -134,7 +135,7 @@ def err(*args, fatal=False):
if _use_colors():
_reset_colors(sys.stderr)

def die(*args, exit_code=1):
def die(*args, exit_code=1) -> NoReturn:
'''Print a fatal error, and abort the program.
:param args: sequence of arguments to print.
Expand Down
5 changes: 5 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ ignore = E126,E261,E302,E305,W504
# Don't lint setup.py, the .tox virtualenv directory, or the build directory
exclude = setup.py,.tox,build

[mypy]
mypy_path=src
ignore_missing_imports=True

[testenv]
deps = -rtox_deps.txt
platform = posix: (linux|darwin)
Expand All @@ -25,4 +29,5 @@ setenv =
passenv = WEST_SKIP_SLOW_TESTS
commands =
flake8 --config={toxinidir}/tox.ini {toxinidir}
mypy --config-file={toxinidir}/tox.ini --package=west
py.test --cov=west {posargs:tests}
1 change: 1 addition & 0 deletions tox_deps.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ setuptools-scm
pytest
pytest-cov
flake8
mypy

0 comments on commit 26b69f9

Please sign in to comment.