From f7a222f22c8f4a8054434bce5142854569d79b2d Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Tue, 2 May 2023 17:41:56 +0100 Subject: [PATCH 1/2] Add mypy config; run mypy in CI --- .github/workflows/mypy.yml | 29 +++++++++++++++++++++++++++++ pyperformance/_benchmark.py | 2 +- pyperformance/_pyproject_toml.py | 2 +- pyproject.toml | 22 ++++++++++++++++++++++ tox.ini | 9 ++++++++- 5 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/mypy.yml diff --git a/.github/workflows/mypy.yml b/.github/workflows/mypy.yml new file mode 100644 index 00000000..d9fb4a66 --- /dev/null +++ b/.github/workflows/mypy.yml @@ -0,0 +1,29 @@ +name: mypy + +on: + push: + branches: [main] + pull_request: + workflow_dispatch: + +permissions: + contents: read + +env: + FORCE_COLOR: 1 + TERM: xterm-256color # needed for FORCE_COLOR to work on mypy on Ubuntu, see https://github.com/python/mypy/issues/13817 + +jobs: + mypy: + name: Check code with mypy + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-python@v4 + with: + cache: "pip" + cache-dependency-path: "pyproject.toml" + python-version: "3.11" + - run: pip install -e .[dev] + - run: pip freeze --all + - run: mypy diff --git a/pyperformance/_benchmark.py b/pyperformance/_benchmark.py index 0cf8f2c7..8ca5eaac 100644 --- a/pyperformance/_benchmark.py +++ b/pyperformance/_benchmark.py @@ -182,7 +182,7 @@ def run(self, python, runid=None, pyperf_opts=None, *, python = venv.python if not runid: - from ..run import get_run_id + from .run import get_run_id runid = get_run_id(python, self) runscript = self.runscript diff --git a/pyperformance/_pyproject_toml.py b/pyperformance/_pyproject_toml.py index ab6f6f85..d06a2072 100644 --- a/pyperformance/_pyproject_toml.py +++ b/pyperformance/_pyproject_toml.py @@ -20,7 +20,7 @@ import packaging.version try: - import tomllib + import tomllib # type: ignore[import] # tomllib doesn't exist on 3.7-3.10 except ImportError: import tomli as tomllib diff --git a/pyproject.toml b/pyproject.toml index 369ea0b9..7e0c5f0f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -68,6 +68,8 @@ dependencies = [ [project.optional-dependencies] dev = [ 'tox', + 'mypy==1.2.0', + 'tomli', # Needed even on 3.11+ for typechecking with mypy ] [project.scripts] @@ -81,3 +83,23 @@ find = {} # Scanning implicit namespaces is active by default [tool.setuptools.dynamic] version = {attr = "pyperformance.__version__"} + +[tool.mypy] +python_version = "3.7" +pretty = true +enable_error_code = "ignore-without-code" +disallow_any_generics = true +warn_redundant_casts = true +warn_unused_ignores = true +warn_unused_configs = true +files = [ + 'pyperformance/', +] +exclude = [ + 'pyperformance/data-files/', + 'pyperformance/tests/' +] + +[[tool.mypy.overrides]] +module = "pyperf" +ignore_missing_imports = true diff --git a/tox.ini b/tox.ini index a38f48b2..f9030b3b 100644 --- a/tox.ini +++ b/tox.ini @@ -1,5 +1,5 @@ [tox] -envlist = py3, pypy3, doc, pep8 +envlist = py3, pypy3, doc, pep8, mypy isolated_build = True [testenv] @@ -27,3 +27,10 @@ commands = flake8 pyperformance runtests.py setup.py # E741 ambiguous variable name 'l' (don't modify benhcmarks just for that) # W503 line break before binary operator ignore = E501,E741,W503 + +[testenv:mypy] +basepython = python3 +deps= + mypy + tomli +commands = mypy From 7d9b5f7bbc8257408be77fec63c86597810307af Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Fri, 5 May 2023 13:25:39 +0100 Subject: [PATCH 2/2] Add `--strict-concatenate` --- pyproject.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/pyproject.toml b/pyproject.toml index 7e0c5f0f..e5ea4dd0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -89,6 +89,7 @@ python_version = "3.7" pretty = true enable_error_code = "ignore-without-code" disallow_any_generics = true +strict_concatenate = true warn_redundant_casts = true warn_unused_ignores = true warn_unused_configs = true