Skip to content

Commit

Permalink
Configure mypy with django-stubs
Browse files Browse the repository at this point in the history
This allows mypy to do a lot of type-inference round django-related
code. Should improve how we handle typing errors.
  • Loading branch information
WhyNotHugo committed Oct 19, 2023
1 parent 93dc588 commit 6a980bd
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 10 deletions.
7 changes: 0 additions & 7 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,6 @@ repos:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
exclude: docs\/.*
- repo: https://github.com/pre-commit/mirrors-mypy
rev: "v1.5.1"
hooks:
- id: mypy
additional_dependencies:
- types-requests
exclude: testapp
- repo: https://github.com/psf/black
rev: "23.9.1"
hooks:
Expand Down
4 changes: 2 additions & 2 deletions payments/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def change_status(self, status: PaymentStatus | str, message=""):
"""
from .signals import status_changed

self.status = status
self.status = status # type: ignore[assignment]
self.message = message
self.save(update_fields=["status", "message"])
status_changed.send(sender=type(self), instance=self)
Expand All @@ -107,7 +107,7 @@ def change_fraud_status(self, status: PaymentStatus, message="", commit=True):
status, ", ".join(available_statuses)
)
)
self.fraud_status = status
self.fraud_status = status # type: ignore[assignment]

Check warning on line 110 in payments/models.py

View check run for this annotation

Codecov / codecov/patch

payments/models.py#L110

Added line #L110 was not covered by tests
self.fraud_message = message
if commit:
self.save()
Expand Down
6 changes: 5 additions & 1 deletion payments/stripe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import json
import warnings
from decimal import Decimal
from typing import TYPE_CHECKING

import stripe

Expand All @@ -15,6 +16,9 @@
from .forms import PaymentForm
from .providers import StripeProviderV3

if TYPE_CHECKING:
from django import forms

Check warning on line 20 in payments/stripe/__init__.py

View check run for this annotation

Codecov / codecov/patch

payments/stripe/__init__.py#L20

Added line #L20 was not covered by tests


class StripeProvider(BasicProvider):
"""Provider backend using `Stripe <https://stripe.com/>`_.
Expand All @@ -27,7 +31,7 @@ class StripeProvider(BasicProvider):
:param image: Your logo.
"""

form_class = ModalPaymentForm
form_class: type[forms.Form] = ModalPaymentForm

def __init__(self, public_key, secret_key, image="", name="", **kwargs):
stripe.api_key = secret_key
Expand Down
13 changes: 13 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,16 @@ braintree = ["braintree>=3.14.0"]
cybersource = ["suds-community>=0.6"]
dev = [
"coverage",
"django-stubs[compatible-mypy]",
"mock",
"pytest",
"pytest-cov",
"pytest-django",
"types-braintree",
"types-dj-database-url",
"types-requests",
"types-stripe",
"types-xmltodict",
]
docs = ["sphinx_rtd_theme"]
mercadopago = ["mercadopago>=2.0.0,<3.0.0"]
Expand All @@ -62,6 +68,13 @@ exclude_lines = [
"if TYPE_CHECKING:",
]

[tool.mypy]
ignore_missing_imports = true
plugins = ["mypy_django_plugin.main"]

[tool.django-stubs]
django_settings_module = "test_settings"

[tool.pytest.ini_options]
addopts =[
"--cov=payments",
Expand Down
6 changes: 6 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ python =
3.11: py311
3.12-dev: py312

[testenv:mypy]
setenv =
PYTHONPATH = {env:PATH}/testapp
extras = {[testenv]extras}
commands = mypy .

[gh-actions:env]
DJANGO =
3.2: dj32
Expand Down

0 comments on commit 6a980bd

Please sign in to comment.