From 202a877cc231634a4ff263d4b1e98a0815bd7e09 Mon Sep 17 00:00:00 2001 From: Richard Ebeling Date: Mon, 5 Feb 2024 19:29:32 +0100 Subject: [PATCH 1/5] Make django-stubs a non-dev-requirement --- requirements-dev.txt | 1 - requirements.txt | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements-dev.txt b/requirements-dev.txt index c147c2c792..5476387146 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -2,7 +2,6 @@ black~=23.12.0 coverage[toml] django-debug-toolbar~=4.0 -django-stubs==4.2.6 django-webtest~=1.9.10 isort~=5.13.1 model-bakery~=1.17.0 diff --git a/requirements.txt b/requirements.txt index 543be352f1..7bf4545a56 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,6 @@ django-extensions==3.2.3 django-fsm==2.8.1 +django-stubs==4.2.6 django~=5.0 mozilla-django-oidc==4.0.0 openpyxl==3.1.2 From d3b18d04b0c67da83a8106e07f4c4567a39d3632 Mon Sep 17 00:00:00 2001 From: Richard Ebeling Date: Mon, 5 Feb 2024 21:45:37 +0100 Subject: [PATCH 2/5] Revert "Make django-stubs a non-dev-requirement" This reverts commit 202a877cc231634a4ff263d4b1e98a0815bd7e09. --- requirements-dev.txt | 1 + requirements.txt | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements-dev.txt b/requirements-dev.txt index 5476387146..c147c2c792 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -2,6 +2,7 @@ black~=23.12.0 coverage[toml] django-debug-toolbar~=4.0 +django-stubs==4.2.6 django-webtest~=1.9.10 isort~=5.13.1 model-bakery~=1.17.0 diff --git a/requirements.txt b/requirements.txt index 7bf4545a56..543be352f1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,5 @@ django-extensions==3.2.3 django-fsm==2.8.1 -django-stubs==4.2.6 django~=5.0 mozilla-django-oidc==4.0.0 openpyxl==3.1.2 From bc363032e68ffc8498ab9dfb436a6c4de714947d Mon Sep 17 00:00:00 2001 From: Richard Ebeling Date: Mon, 5 Feb 2024 22:09:11 +0100 Subject: [PATCH 3/5] Just make StrOrPromise Any if we're not typechecking --- evap/evaluation/models.py | 2 +- evap/evaluation/tools.py | 8 ++++++-- evap/staff/views.py | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/evap/evaluation/models.py b/evap/evaluation/models.py index 4591ac612c..f7f9e7119c 100644 --- a/evap/evaluation/models.py +++ b/evap/evaluation/models.py @@ -30,10 +30,10 @@ from django.utils.translation import gettext_lazy as _ from django_fsm import FSMIntegerField, transition from django_fsm.signals import post_transition -from django_stubs_ext import StrOrPromise from evap.evaluation.models_logging import FieldAction, LoggedModel from evap.evaluation.tools import ( + StrOrPromise, clean_email, date_to_datetime, is_external_email, diff --git a/evap/evaluation/tools.py b/evap/evaluation/tools.py index ad9aa51a7f..0a1d56ead2 100644 --- a/evap/evaluation/tools.py +++ b/evap/evaluation/tools.py @@ -3,7 +3,7 @@ from abc import ABC, abstractmethod from collections import defaultdict from collections.abc import Iterable, Mapping -from typing import Any, Protocol, TypeVar +from typing import TYPE_CHECKING, Any, Protocol, TypeVar from urllib.parse import quote import xlwt @@ -16,7 +16,11 @@ from django.utils.datastructures import MultiValueDict from django.utils.translation import get_language from django.views.generic import FormView -from django_stubs_ext import StrOrPromise + +if TYPE_CHECKING: + from django_stubs_ext import StrOrPromise +else: + StrOrPromise = Any M = TypeVar("M", bound=Model) T = TypeVar("T") diff --git a/evap/staff/views.py b/evap/staff/views.py index c9a8f7ddb1..df1b5e3be2 100644 --- a/evap/staff/views.py +++ b/evap/staff/views.py @@ -37,7 +37,6 @@ from django.utils.translation import gettext_lazy, ngettext from django.views.decorators.http import require_POST from django.views.generic import CreateView, FormView, UpdateView -from django_stubs_ext import StrOrPromise from evap.contributor.views import export_contributor_results from evap.evaluation.auth import manager_required, reviewer_required, staff_permission_required @@ -65,6 +64,7 @@ FormsetView, HttpResponseNoContent, SaveValidFormMixin, + StrOrPromise, get_object_from_dict_pk_entry_or_logged_40x, get_parameter_from_url_or_session, sort_formset, From 3ff3cd693130f0b8aae03362f08db6830c329ff2 Mon Sep 17 00:00:00 2001 From: Richard Ebeling Date: Tue, 6 Feb 2024 16:07:22 +0100 Subject: [PATCH 4/5] Use strict StrOrPromise with typeguard --- evap/evaluation/tools.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/evap/evaluation/tools.py b/evap/evaluation/tools.py index 0a1d56ead2..4c64fef336 100644 --- a/evap/evaluation/tools.py +++ b/evap/evaluation/tools.py @@ -17,9 +17,10 @@ from django.utils.translation import get_language from django.views.generic import FormView -if TYPE_CHECKING: +try: from django_stubs_ext import StrOrPromise -else: +except ImportError: + assert not TYPE_CHECKING StrOrPromise = Any M = TypeVar("M", bound=Model) From 82176b27891cf5850f78f0ea9641d65483558f0a Mon Sep 17 00:00:00 2001 From: Richard Ebeling Date: Mon, 12 Feb 2024 18:58:54 +0100 Subject: [PATCH 5/5] Third time is a charm --- evap/evaluation/tools.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/evap/evaluation/tools.py b/evap/evaluation/tools.py index 4c64fef336..9012ccedce 100644 --- a/evap/evaluation/tools.py +++ b/evap/evaluation/tools.py @@ -17,11 +17,13 @@ from django.utils.translation import get_language from django.views.generic import FormView -try: - from django_stubs_ext import StrOrPromise -except ImportError: - assert not TYPE_CHECKING - StrOrPromise = Any +if TYPE_CHECKING: + from django_stubs_ext import StrOrPromise # use proper definition with mypy +else: + try: + from django_stubs_ext import StrOrPromise # if installed, use proper definition for typeguard + except ImportError: + StrOrPromise = Any # on production setups, type alias to Any M = TypeVar("M", bound=Model) T = TypeVar("T")