Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mypy config; run mypy in CI #286

Merged
merged 2 commits into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 29 additions & 0 deletions .github/workflows/mypy.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion pyperformance/_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pyperformance/_pyproject_toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 22 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's generally a good idea to run mypy on the lowest Python version you support; this means it will flag if you accidentally use a feature that only exists in a newer Python version

pretty = true
enable_error_code = "ignore-without-code"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means that mypy will emit an error if you do a bare # type: ignore without a specific error code (e.g. you should use # type: ignore[import] to suppress mypy's import-related errors)

disallow_any_generics = true
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means that mypy will forbid using, for example, list or typing.List as an annotation. You should use list[int] or typing.List[int] instead, which gives the type checker much more information to work with.

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
Comment on lines +104 to +106
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pyperf doesn't have type annotations, so without this section, mypy would emit an error at every location pyperf was imported, which was quite annoying.

9 changes: 8 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tox]
envlist = py3, pypy3, doc, pep8
envlist = py3, pypy3, doc, pep8, mypy
isolated_build = True

[testenv]
Expand Down Expand Up @@ -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