Skip to content

Commit 7aed8ec

Browse files
committed
Add type hints using basedpyright
1 parent e242994 commit 7aed8ec

File tree

8 files changed

+42
-27
lines changed

8 files changed

+42
-27
lines changed

inertia/http.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from functools import wraps
22
from http import HTTPStatus
33
from json import dumps as json_encode
4+
from typing import Any, Callable
45

56
from django.core.exceptions import ImproperlyConfigured
67
from django.http import HttpRequest, HttpResponse
@@ -65,6 +66,11 @@ def should_encrypt_history(self):
6566

6667

6768
class BaseInertiaResponseMixin:
69+
request: InertiaRequest
70+
component: str
71+
props: dict[str, Any]
72+
template_data: dict[str, Any]
73+
6874
def page_data(self):
6975
clear_history = validate_type(
7076
self.request.session.pop(INERTIA_SESSION_CLEAR_HISTORY, False),
@@ -147,7 +153,7 @@ def build_first_load(self, data):
147153
"inertia_layout": layout,
148154
**context,
149155
},
150-
self.request,
156+
self.request.request,
151157
using=None,
152158
)
153159

@@ -238,8 +244,8 @@ def clear_history(request):
238244
request.session[INERTIA_SESSION_CLEAR_HISTORY] = True
239245

240246

241-
def inertia(component):
242-
def decorator(func):
247+
def inertia(component: str):
248+
def decorator(func: Callable[..., HttpResponse | InertiaResponse | dict[str, Any]]):
243249
@wraps(func)
244250
def process_inertia_response(request, *args, **kwargs):
245251
props = func(request, *args, **kwargs)

inertia/middleware.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,9 @@ def is_stale_inertia_get(self, request):
5050
return request.method == "GET" and self.is_stale(request)
5151

5252
def force_refresh(self, request):
53-
messages.get_messages(request).used = False
53+
# If the storage middleware is not defined, get_messages returns an empty list
54+
storage = messages.get_messages(request)
55+
if not isinstance(storage, list):
56+
storage.used = False
57+
5458
return location(request.build_absolute_uri())

inertia/prop_classes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def __call__(self):
1111

1212
class MergeableProp(ABC):
1313
@abstractmethod
14-
def should_merge(self):
14+
def should_merge(self) -> bool:
1515
pass
1616

1717

inertia/test.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010

1111
class ClientWithLastResponse:
12-
def __init__(self, client):
12+
def __init__(self, client: Client):
1313
self.client = client
1414
self.last_response = None
1515

@@ -29,10 +29,6 @@ def setUp(self):
2929
def last_response(self):
3030
return self.inertia.last_response or self.client.last_response
3131

32-
def assertJSONResponse(self, response, json_obj):
33-
self.assertEqual(response.headers["Content-Type"], "application/json")
34-
self.assertEqual(response.json(), json_obj)
35-
3632

3733
class InertiaTestCase(BaseInertiaTestCase, TestCase):
3834
def setUp(self):
@@ -47,11 +43,12 @@ def tearDown(self):
4743
self.mock_inertia.stop()
4844

4945
def page(self):
50-
page_data = (
51-
self.mock_render.call_args[0][1]["page"]
52-
if self.mock_render.call_args
53-
else self.last_response().content
54-
)
46+
if self.mock_render.call_args:
47+
page_data = self.mock_render.call_args[0][1]["page"]
48+
elif response := self.last_response():
49+
page_data = response.content
50+
else:
51+
page_data = ""
5552

5653
return loads(page_data)
5754

@@ -75,6 +72,10 @@ def template_data(self):
7572
def component(self):
7673
return self.page()["component"]
7774

75+
def assertJSONResponse(self, response, json_obj):
76+
self.assertEqual(response.headers["Content-Type"], "application/json")
77+
self.assertEqual(response.json(), json_obj)
78+
7879
def assertIncludesProps(self, props):
7980
self.assertDictEqual(self.props(), {**self.props(), **props})
8081

inertia/tests/testapp/urls.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
path("defer-group/", views.defer_group_test),
1515
path("merge/", views.merge_test),
1616
path("complex-props/", views.complex_props_test),
17-
path("share/", views.share_test),
17+
path("share/", views.share_test), # type: ignore
1818
path("inertia-redirect/", views.inertia_redirect_test),
1919
path("external-redirect/", views.external_redirect_test),
2020
path("encrypt-history/", views.encrypt_history_test),

inertia/tests/testapp/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def encrypt_history_false_test(request):
132132

133133
@inertia("TestComponent")
134134
def encrypt_history_type_error_test(request):
135-
encrypt_history(request, "foo")
135+
encrypt_history(request, "foo") # pyright: ignore[reportArgumentType]
136136
return {}
137137

138138

inertia/utils.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,22 @@ def model_to_dict(model):
1313

1414

1515
class InertiaJsonEncoder(DjangoJSONEncoder):
16-
def default(self, value):
17-
if hasattr(value.__class__, "InertiaMeta"):
16+
def default(self, o):
17+
if hasattr(o.__class__, "InertiaMeta"):
1818
return {
19-
field: getattr(value, field)
20-
for field in value.__class__.InertiaMeta.fields
19+
field: getattr(o, field) for field in o.__class__.InertiaMeta.fields
2120
}
2221

23-
if isinstance(value, models.Model):
24-
return model_to_dict(value)
22+
if isinstance(o, models.Model):
23+
return model_to_dict(o)
2524

26-
if isinstance(value, QuerySet):
25+
if isinstance(o, QuerySet):
2726
return [
28-
(model_to_dict(obj) if isinstance(value.model, models.Model) else obj)
29-
for obj in value
27+
(model_to_dict(obj) if isinstance(o.model, models.Model) else obj)
28+
for obj in o
3029
]
3130

32-
return super().default(value)
31+
return super().default(o)
3332

3433

3534
def lazy(prop):

pyproject.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ pytest = "^8.3.5"
3838
pytest-cov = "^6.0.0"
3939
pytest-django = "^4.10.0"
4040
ruff = "^0.11.2"
41+
django-stubs = "^5.2.1"
42+
basedpyright = "^1.30.1"
4143

4244
[tool.ruff.lint]
4345
select = [
@@ -54,3 +56,6 @@ unfixable = ["B", "SIM"]
5456
[tool.pytest.ini_options]
5557
DJANGO_SETTINGS_MODULE = "inertia.tests.settings"
5658
django_find_project = false
59+
60+
[tool.basedpyright]
61+
typeCheckingMode = "standard"

0 commit comments

Comments
 (0)