Skip to content

Commit

Permalink
Merge pull request #78 from ymyzk/mypy
Browse files Browse the repository at this point in the history
tox4: Set up mypy and use annotations for type hints
  • Loading branch information
ymyzk committed Sep 4, 2021
2 parents cb09894 + 68af955 commit aea6ea5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
12 changes: 11 additions & 1 deletion setup.cfg
Expand Up @@ -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
Expand All @@ -74,14 +75,15 @@ skip_missing_interpreters = true
envlist =
black
flake8
mypy
{py36,py37,py38,py39,pypy2,pypy3}-tox{312,315,latest}

[gh-actions]
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

Expand All @@ -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
38 changes: 16 additions & 22 deletions src/tox_gh_actions/plugin.py
Expand Up @@ -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(
Expand Down Expand Up @@ -48,25 +47,22 @@ 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
print("::group::tox: " + message)


@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 = {
Expand All @@ -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))
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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]
Expand Down

0 comments on commit aea6ea5

Please sign in to comment.