Skip to content

Commit

Permalink
misc: Add initial support for Mypy checks (#469)
Browse files Browse the repository at this point in the history
This is the initial change to start adding typing hints to the project.
It includes:

* Adding `mypy` checks to GitHub actions, and Tox.
* Adding `mypy` command to `run.sh` script.
* Initial `mypy` configuration options in `setup.cfg`.
* Small fixes for existing typing issues.
  • Loading branch information
adamantike authored Sep 17, 2022
1 parent b035ac6 commit 25dbbe2
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 5 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,27 @@ jobs:
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
typecheck:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
- uses: actions/cache@v3
with:
path: ~/.cache/pip
key: ${{ matrix.python-version }}-pip-${{ hashFiles('**/requirements.txt') }}
restore-keys: |
${{ matrix.python-version }}-pip-
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install tox-gh-actions
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Type-check
run: |
tox -e typecheck
i18n:
runs-on: ubuntu-latest

Expand Down
4 changes: 3 additions & 1 deletion CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,17 @@ To be mergable, patches must:
- add tests for new code (bug fixes should include regression tests, new
features should have relevant tests),
- not introduce any new flake8_ errors (run ``./run.sh lint``),
- not introduce any new mypy_ errors (run ``./run.sh typecheck``),
- include updated source translations (run ``./run.sh makemessages`` and ``./run.sh compilemessages``),
- document any new features, and
- have a `good commit message`_.

Regressions tests should fail without the rest of the patch and pass
with it.
with it.


.. _open a new issue: https://github.com/django-waffle/django-waffle/issues/new
.. _Fork: https://github.com/django-waffle/django-waffle/fork
.. _flake8: https://pypi.python.org/pypi/flake8
.. _mypy: https://www.mypy-lang.org/
.. _good commit message: http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ transifex-client

flake8
mock==1.3.0
mypy
tox
3 changes: 3 additions & 0 deletions run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ usage() {
echo "USAGE: $0 [command]"
echo " test - run the waffle tests"
echo " lint - run flake8"
echo " typecheck - run mypy"
echo " shell - open the Django shell"
echo " makemigrations - create a schema migration"
exit 1
Expand All @@ -20,6 +21,8 @@ case "$CMD" in
DJANGO_SETTINGS_MODULE=test_settings django-admin test waffle $@ ;;
"lint" )
flake8 waffle $@ ;;
"typecheck" )
mypy waffle $@ ;;
"shell" )
django-admin shell $@ ;;
"makemigrations" )
Expand Down
8 changes: 8 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,11 @@ max-line-length = 120
# - docs: contains autogenerated code that doesn't need a check
exclude = */migrations/*,docs
ignore = E731

[mypy]
python_version = 3.7
disallow_incomplete_defs = True
disallow_untyped_decorators = True
strict_equality = True
[mypy-django.*]
ignore_missing_imports = True
7 changes: 7 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,10 @@ commands =
./run.sh makemessages
./run.sh compilemessages
./run.sh find_uncommitted_translations

[testenv:typecheck]
deps =
Django>=3.2,<4.2
-r{toxinidir}/requirements.txt
commands =
./run.sh typecheck
2 changes: 1 addition & 1 deletion waffle/jinja.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from jinja2 import pass_context
except ImportError:
# NOTE: We can get rid of this when we stop supporting Jinja2 < 3.
from jinja2 import contextfunction as pass_context
from jinja2 import contextfunction as pass_context # type: ignore


@pass_context
Expand Down
7 changes: 4 additions & 3 deletions waffle/mixins.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from functools import partial
from typing import Optional

from django.http import Http404

Expand All @@ -24,7 +25,7 @@ class WaffleFlagMixin(BaseWaffleMixin):
waffle_flag
"""

waffle_flag = None
waffle_flag: Optional[str] = None

def dispatch(self, request, *args, **kwargs):
func = partial(flag_is_active, request)
Expand All @@ -42,7 +43,7 @@ class WaffleSampleMixin(BaseWaffleMixin):
waffle_sample.
"""

waffle_sample = None
waffle_sample: Optional[str] = None

def dispatch(self, request, *args, **kwargs):
active = self.validate_waffle(self.waffle_sample, sample_is_active)
Expand All @@ -59,7 +60,7 @@ class WaffleSwitchMixin(BaseWaffleMixin):
waffle_switch.
"""

waffle_switch = None
waffle_switch: Optional[str] = None

def dispatch(self, request, *args, **kwargs):
active = self.validate_waffle(self.waffle_switch, switch_is_active)
Expand Down

0 comments on commit 25dbbe2

Please sign in to comment.