From 6a980bd46567a626cc5215eef6cd7c9f1b4be97e Mon Sep 17 00:00:00 2001 From: Hugo Osvaldo Barrera Date: Thu, 19 Oct 2023 10:12:06 +0200 Subject: [PATCH] Configure mypy with django-stubs This allows mypy to do a lot of type-inference round django-related code. Should improve how we handle typing errors. --- .pre-commit-config.yaml | 7 ------- payments/models.py | 4 ++-- payments/stripe/__init__.py | 6 +++++- pyproject.toml | 13 +++++++++++++ tox.ini | 6 ++++++ 5 files changed, 26 insertions(+), 10 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 3edcb2679..0e3ef9ff7 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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: diff --git a/payments/models.py b/payments/models.py index 922156bf3..52e7e76da 100644 --- a/payments/models.py +++ b/payments/models.py @@ -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) @@ -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] self.fraud_message = message if commit: self.save() diff --git a/payments/stripe/__init__.py b/payments/stripe/__init__.py index 05930f30e..fbe1c7699 100644 --- a/payments/stripe/__init__.py +++ b/payments/stripe/__init__.py @@ -3,6 +3,7 @@ import json import warnings from decimal import Decimal +from typing import TYPE_CHECKING import stripe @@ -15,6 +16,9 @@ from .forms import PaymentForm from .providers import StripeProviderV3 +if TYPE_CHECKING: + from django import forms + class StripeProvider(BasicProvider): """Provider backend using `Stripe `_. @@ -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 diff --git a/pyproject.toml b/pyproject.toml index 2f86f577c..eae703a18 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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"] @@ -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", diff --git a/tox.ini b/tox.ini index 689a1ee73..48df1c7de 100644 --- a/tox.ini +++ b/tox.ini @@ -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