Skip to content

Commit f51f547

Browse files
committed
Improve type hints
1 parent fbb782c commit f51f547

24 files changed

Lines changed: 88 additions & 58 deletions

File tree

backend/donations/admin/ngos.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def has_delete_permission(self, request, obj=...):
8888

8989
class NgoPartnerInline(TabularInline):
9090
# noinspection PyUnresolvedReferences
91-
model = Ngo.partners.through
91+
model = Ngo.partners.through # type: ignore
9292
extra = 1
9393
tab = True
9494

@@ -515,7 +515,7 @@ def update_from_ngohub_async(self, request: HttpRequest, queryset: QuerySet[Ngo]
515515
def check_cult_registry_sync(self, request: HttpRequest, queryset: QuerySet[Ngo]):
516516
show_errors: bool = True
517517

518-
registration_numbers = queryset.values_list("registration_number", flat=True)
518+
registration_numbers: list[str] = queryset.values_list("registration_number", flat=True) # type: ignore
519519
task_result = cult_registry_check_organizations(registration_numbers, update_method="sync")
520520

521521
message = "ANAF Registry Results: | "
@@ -533,7 +533,7 @@ def check_cult_registry_sync(self, request: HttpRequest, queryset: QuerySet[Ngo]
533533

534534
@action(description=_("Check in ANAF Cult Registry asynchronously"))
535535
def check_cult_registry_async(self, request, queryset: QuerySet[Ngo]):
536-
registration_numbers = queryset.values_list("registration_number", flat=True)
536+
registration_numbers: list[str] = queryset.values_list("registration_number", flat=True) # type: ignore
537537
cult_registry_check_organizations(registration_numbers, update_method="async")
538538
self.message_user(request, _("NGOs are being searched in ANAF Cult Registry."))
539539

backend/donations/forms/ngo_account.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def save(self, commit=True):
139139
return cause
140140

141141
def clean_slug(self):
142-
slug = self.cleaned_data.get("slug").lower()
142+
slug = self.cleaned_data.get("slug", "").lower()
143143

144144
ngo_slug_validator(slug)
145145

@@ -155,7 +155,7 @@ def clean_slug(self):
155155
return slug
156156

157157
def clean_description(self):
158-
return self.cleaned_data.get("description").strip()
158+
return self.cleaned_data.get("description", "").strip()
159159

160160
def clean_bank_account(self):
161161
bank_account = self.cleaned_data.get("bank_account")

backend/donations/management/commands/generate_orgs.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import random
2-
from typing import Any
32

43
from django.contrib.auth import get_user_model
54
from django.core.management import BaseCommand
@@ -803,7 +802,7 @@ def handle(self, *args, **options):
803802
create_user_only = options.get("user_only", None)
804803
create_ngohub_id = options.get("ngohub", None)
805804

806-
organizations: list[dict[str, Any]] = []
805+
organizations: list[Ngo] = []
807806
generated_organization_names: list[str] = []
808807

809808
user_model = get_user_model()

backend/donations/management/commands/generate_other_causes.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,10 @@ def handle(self, *args, **options):
258258
consecutive_identical_names: int = 0
259259
while len(causes) < total_causes:
260260
if target_org:
261-
ngo: Ngo = target_org
261+
ngo: Ngo | None = target_org
262262
else:
263-
ngo: Ngo = Ngo.active.order_by("?").first()
264-
if not ngo.can_create_causes:
263+
ngo: Ngo | None = Ngo.active.order_by("?").first()
264+
if not ngo or not ngo.can_create_causes:
265265
continue
266266

267267
cause_title = MOCK_CAUSE_NAMES["titles"][random.randint(0, len(MOCK_CAUSE_NAMES["titles"]) - 1)]

backend/donations/management/commands/generate_stats.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def add_arguments(self, parser):
2323

2424
def handle(self, *args, **options):
2525
statistic_type: str = options["statistic"]
26-
for_date_str: str = options.get("date")
26+
for_date_str: str = options.get("date", "")
2727

2828
if statistic_type == StatsChoices.REDIRECTIONS_PER_DAY and not for_date_str:
2929
self.stderr.write("Error: --date argument is required for REDIRECTIONS_PER_DAY statistic.")

backend/donations/models/downloads.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class RedirectionsDownloadJob(AsyncJob):
2323
null=True,
2424
)
2525

26-
class Meta:
26+
class Meta: # type: ignore
2727
verbose_name = _("download job")
2828
verbose_name_plural = _("download jobs")
2929

backend/donations/models/jobs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class Job(AsyncJob):
3737
def __str__(self):
3838
return f"{self.cause} {self.status}"
3939

40-
class Meta:
40+
class Meta: # type: ignore
4141
verbose_name = _("job")
4242
verbose_name_plural = _("jobs")
4343

backend/donations/models/ngos.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import logging
22
import re
33
from functools import partial
4-
from typing import Any
4+
from typing import TYPE_CHECKING, Any
55

66
from auditlog.registry import auditlog
77
from django.conf import settings
@@ -25,6 +25,11 @@
2525
ngo_id_number_validator,
2626
)
2727

28+
if TYPE_CHECKING:
29+
from partners.models import Partner
30+
from donations.models import Job, RedirectionsDownloadJob
31+
32+
2833
ALL_NGOS_CACHE_KEY = "ALL_NGOS"
2934
ALL_NGO_IDS_CACHE_KEY = "ALL_NGO_IDS"
3035
FRONTPAGE_NGOS_KEY = "FRONTPAGE_NGOS"
@@ -282,6 +287,10 @@ class Ngo(CommonFilenameCacheModel):
282287

283288
# Type hinting for related models
284289
causes: "models.manager.RelatedManager[Cause]"
290+
partners: "models.manager.RelatedManager[Partner]"
291+
jobs: "models.manager.RelatedManager[Job]"
292+
download_jobs: "models.manager.RelatedManager[RedirectionsDownloadJob]"
293+
donor_set: "models.manager.RelatedManager[Donor]"
285294

286295
# Model managers
287296
objects = models.Manager()
@@ -392,7 +401,7 @@ def has_ngo_hub(self):
392401
@classmethod
393402
def mandatory_fields(cls):
394403
# noinspection PyTypeChecker
395-
field_names: list[DeferredAttribute] = [
404+
field_names = [
396405
Ngo.name,
397406
Ngo.registration_number,
398407
]

backend/donations/tests/builder.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,37 +53,37 @@ def with_all_fields(self) -> "DonorTestBuilder":
5353
.with_misc()
5454
)
5555

56-
def with_first_name(self, first_name: str = None) -> "DonorTestBuilder":
56+
def with_first_name(self, first_name: str | None = None) -> "DonorTestBuilder":
5757
self.donor_data["f_name"] = first_name if first_name else faker.first_name()
5858

5959
return self
6060

61-
def with_last_name(self, last_name: str = None) -> "DonorTestBuilder":
61+
def with_last_name(self, last_name: str | None = None) -> "DonorTestBuilder":
6262
self.donor_data["l_name"] = last_name if last_name else faker.last_name()
6363

6464
return self
6565

66-
def with_initial(self, initial: str = None) -> "DonorTestBuilder":
66+
def with_initial(self, initial: str | None = None) -> "DonorTestBuilder":
6767
self.donor_data["initial"] = initial if initial else faker.random_uppercase_letter()
6868

6969
return self
7070

71-
def with_cnp(self, cnp: str = None) -> "DonorTestBuilder":
71+
def with_cnp(self, cnp: str | None = None) -> "DonorTestBuilder":
7272
self.donor_data["cnp"] = cnp if cnp else faker.ssn()
7373

7474
return self
7575

76-
def with_city(self, city: str = None) -> "DonorTestBuilder":
76+
def with_city(self, city: str | None = None) -> "DonorTestBuilder":
7777
self.donor_data["city"] = city if city else faker.city()
7878

7979
return self
8080

81-
def with_county(self, county: str = None) -> "DonorTestBuilder":
81+
def with_county(self, county: str | None = None) -> "DonorTestBuilder":
8282
self.donor_data["county"] = county if county else faker.state()
8383

8484
return self
8585

86-
def with_address(self, address: dict[str, str] = None) -> "DonorTestBuilder":
86+
def with_address(self, address: dict[str, str] | None = None) -> "DonorTestBuilder":
8787
if address is None:
8888
address = {
8989
"street_name": faker.street_name(),
@@ -102,17 +102,17 @@ def with_address(self, address: dict[str, str] = None) -> "DonorTestBuilder":
102102

103103
return self
104104

105-
def with_phone(self, phone: str = None) -> "DonorTestBuilder":
105+
def with_phone(self, phone: str | None = None) -> "DonorTestBuilder":
106106
self.donor_data["phone"] = phone if phone else faker.phone_number()
107107

108108
return self
109109

110-
def with_email(self, email: str = None) -> "DonorTestBuilder":
110+
def with_email(self, email: str | None = None) -> "DonorTestBuilder":
111111
self.donor_data["email"] = email if email else faker.email()
112112

113113
return self
114114

115-
def with_geoip(self, geoip: dict[str, Any] = None) -> "DonorTestBuilder":
115+
def with_geoip(self, geoip: dict[str, Any] | None = None) -> "DonorTestBuilder":
116116
if geoip is None:
117117
geoip = {
118118
"ip": faker.ipv4(),
@@ -129,11 +129,11 @@ def with_geoip(self, geoip: dict[str, Any] = None) -> "DonorTestBuilder":
129129
def with_misc(
130130
self,
131131
*,
132-
is_anonymous: bool = None,
133-
anaf_gdpr: bool = None,
134-
two_years: bool = None,
135-
has_signed: bool = None,
136-
income_type: str = None,
132+
is_anonymous: bool | None = None,
133+
anaf_gdpr: bool | None = None,
134+
two_years: bool | None = None,
135+
has_signed: bool | None = None,
136+
income_type: str | None = None,
137137
):
138138
if is_anonymous is None:
139139
is_anonymous = random.choice([True, False])

backend/donations/views/dashboard/helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def generate_donations_per_month_chart(
6767
"datasets": [
6868
{
6969
"label": str(data["year"]),
70-
"data": donations_per_month[data["year"]],
70+
"data": donations_per_month[int(data["year"])],
7171
"borderColor": data["border_color"],
7272
"backgroundColor": data["background_color"],
7373
"borderWidth": data.get("border_width", default_border_width),

0 commit comments

Comments
 (0)