From d91562690402140549490563c54c33d55f851a2e Mon Sep 17 00:00:00 2001 From: Yusuke Miyazaki Date: Sat, 4 Sep 2021 20:29:10 +0900 Subject: [PATCH 1/2] Set up mypy --- setup.cfg | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/setup.cfg b/setup.cfg index 302e711..d77fe25 100644 --- a/setup.cfg +++ b/setup.cfg @@ -55,6 +55,7 @@ tox = testing = black; platform_python_implementation=='CPython' flake8 >=3, <4 + mypy; platform_python_implementation=='CPython' pytest >=4, <6 pytest-cov >=2, <3 pytest-mock >=2, <3 @@ -74,6 +75,7 @@ skip_missing_interpreters = true envlist = black flake8 + mypy {py36,py37,py38,py39,pypy2,pypy3}-tox{312,315,latest} [gh-actions] @@ -81,7 +83,7 @@ python = 3.6: py36 3.7: py37 3.8: py38, black, flake8 - 3.9: py39 + 3.9: py39, mypy pypy-2: pypy2 pypy-3: pypy3 @@ -103,5 +105,13 @@ description = run flake8 under {basepython} commands = flake8 src/ tests/ setup.py extras = testing +[testenv:mypy] +description = run mypy under {basepython} +commands = flake8 src/ tests/ setup.py +extras = testing + [flake8] max-line-length = 88 + +[mypy] +ignore_missing_imports = True \ No newline at end of file From 68af95563ef0af13678f90540e6632c645853947 Mon Sep 17 00:00:00 2001 From: Yusuke Miyazaki Date: Sat, 4 Sep 2021 20:36:26 +0900 Subject: [PATCH 2/2] Use annotations for writing type hints --- src/tox_gh_actions/plugin.py | 38 +++++++++++++++--------------------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/src/tox_gh_actions/plugin.py b/src/tox_gh_actions/plugin.py index d6a22fc..db6712f 100644 --- a/src/tox_gh_actions/plugin.py +++ b/src/tox_gh_actions/plugin.py @@ -13,8 +13,7 @@ @hookimpl -def tox_configure(config): - # type: (Config) -> None +def tox_configure(config: Config) -> None: verbosity1("running tox-gh-actions") if not is_running_on_actions(): verbosity1( @@ -48,10 +47,9 @@ def tox_configure(config): @hookimpl -def tox_runtest_pre(venv): - # type: (VirtualEnv) -> None +def tox_runtest_pre(venv: VirtualEnv) -> None: if is_running_on_actions(): - envconfig = venv.envconfig # type: TestenvConfig + envconfig: TestenvConfig = venv.envconfig message = envconfig.envname if envconfig.description: message += " - " + envconfig.description @@ -59,14 +57,12 @@ def tox_runtest_pre(venv): @hookimpl -def tox_runtest_post(venv): - # type: (VirtualEnv) -> None +def tox_runtest_post(venv: VirtualEnv) -> None: if is_running_on_actions(): print("::endgroup::") -def parse_config(config): - # type: (Dict[str, Dict[str, str]]) -> Dict[str, Dict[str, Any]] +def parse_config(config: Dict[str, Dict[str, str]]) -> Dict[str, Dict[str, Any]]: """Parse gh-actions section in tox.ini""" config_python = parse_dict(config.get("gh-actions", {}).get("python", "")) config_env = { @@ -81,10 +77,11 @@ def parse_config(config): } -def get_factors(gh_actions_config, versions): - # type: (Dict[str, Dict[str, Any]], Iterable[str]) -> List[str] +def get_factors( + gh_actions_config: Dict[str, Dict[str, Any]], versions: Iterable[str] +) -> List[str]: """Get a list of factors""" - factors = [] # type: List[List[str]] + factors: List[List[str]] = [] for version in versions: if version in gh_actions_config["python"]: verbosity2("got factors for Python version: {}".format(version)) @@ -98,8 +95,9 @@ def get_factors(gh_actions_config, versions): return [x for x in map(lambda f: "-".join(f), product(*factors)) if x] -def get_envlist_from_factors(envlist, factors): - # type: (Iterable[str], Iterable[str]) -> List[str] +def get_envlist_from_factors( + envlist: Iterable[str], factors: Iterable[str] +) -> List[str]: """Filter envlist using factors""" result = [] for env in envlist: @@ -111,8 +109,7 @@ def get_envlist_from_factors(envlist, factors): return result -def get_python_version_keys(): - # type: () -> List[str] +def get_python_version_keys() -> List[str]: """Get Python version in string for getting factors from gh-action's config Examples: @@ -137,16 +134,14 @@ def get_python_version_keys(): return [major_minor_version, major_version] -def is_running_on_actions(): - # type: () -> bool +def is_running_on_actions() -> bool: """Returns True when running on GitHub Actions""" # See the following document on which environ to use for this purpose. # https://docs.github.com/en/free-pro-team@latest/actions/reference/environment-variables#default-environment-variables return os.environ.get("GITHUB_ACTIONS") == "true" -def is_env_specified(config): - # type: (Config) -> bool +def is_env_specified(config: Config) -> bool: """Returns True when environments are explicitly given""" if os.environ.get("TOXENV"): # When TOXENV is a non-empty string @@ -163,8 +158,7 @@ def is_env_specified(config): # https://github.com/tox-dev/tox-travis/blob/0.12/LICENSE -def parse_dict(value): - # type: (str) -> Dict[str, str] +def parse_dict(value: str) -> Dict[str, str]: """Parse a dict value from the tox config. .. code-block: ini [travis]