Skip to content

Commit 07b81eb

Browse files
Remove Type Hints from CspRenderingTestCase
1 parent c217334 commit 07b81eb

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

tests/test_csp_rendering.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
MIDDLEWARE_CSP_LAST = settings.MIDDLEWARE + ["csp.middleware.CSPMiddleware"]
2222

2323

24-
def get_namespaces(element: Element) -> dict[str, str]:
24+
def get_namespaces(element):
2525
"""
2626
Return the default `xmlns`. See
2727
https://docs.python.org/3/library/xml.etree.elementtree.html#parsing-xml-with-namespaces
@@ -39,9 +39,8 @@ def setUp(self):
3939
super().setUp()
4040
self.parser = HTMLParser()
4141

42-
def _fail_if_missing(
43-
self, root: Element, path: str, namespaces: dict[str, str], nonce: str
44-
):
42+
def _fail_if_missing(self, root, path, namespaces, nonce):
43+
4544
"""
4645
Search elements, fail if a `nonce` attribute is missing on them.
4746
"""
@@ -50,7 +49,7 @@ def _fail_if_missing(
5049
if item.attrib.get("nonce") != nonce:
5150
raise self.failureException(f"{item} has no nonce attribute.")
5251

53-
def _fail_if_found(self, root: Element, path: str, namespaces: dict[str, str]):
52+
def _fail_if_found(self, root, path, namespaces):
5453
"""
5554
Search elements, fail if a `nonce` attribute is found on them.
5655
"""
@@ -59,7 +58,7 @@ def _fail_if_found(self, root: Element, path: str, namespaces: dict[str, str]):
5958
if "nonce" in item.attrib:
6059
raise self.failureException(f"{item} has a nonce attribute.")
6160

62-
def _fail_on_invalid_html(self, content: bytes, parser: HTMLParser):
61+
def _fail_on_invalid_html(self, content, parser):
6362
"""Fail if the passed HTML is invalid."""
6463
if parser.errors:
6564
default_msg = ["Content is invalid HTML:"]
@@ -74,10 +73,10 @@ def test_exists(self):
7473
"""A `nonce` should exist when using the `CSPMiddleware`."""
7574
for middleware in [MIDDLEWARE_CSP_BEFORE, MIDDLEWARE_CSP_LAST]:
7675
with self.settings(MIDDLEWARE=middleware):
77-
response = cast(HttpResponse, self.client.get(path="/csp_view/"))
76+
response = self.client.get(path="/csp_view/")
7877
self.assertEqual(response.status_code, 200)
7978

80-
html_root: Element = self.parser.parse(stream=response.content)
79+
html_root = self.parser.parse(stream=response.content)
8180
self._fail_on_invalid_html(content=response.content, parser=self.parser)
8281
self.assertContains(response, "djDebug")
8382

@@ -98,10 +97,10 @@ def test_does_not_exist_nonce_wasnt_used(self):
9897
"""
9998
for middleware in [MIDDLEWARE_CSP_BEFORE, MIDDLEWARE_CSP_LAST]:
10099
with self.settings(MIDDLEWARE=middleware):
101-
response = cast(HttpResponse, self.client.get(path="/regular/basic/"))
100+
response = self.client.get(path="/regular/basic/")
102101
self.assertEqual(response.status_code, 200)
103102

104-
html_root: Element = self.parser.parse(stream=response.content)
103+
html_root = self.parser.parse(stream=response.content)
105104
self._fail_on_invalid_html(content=response.content, parser=self.parser)
106105
self.assertContains(response, "djDebug")
107106

@@ -119,15 +118,15 @@ def test_does_not_exist_nonce_wasnt_used(self):
119118
def test_redirects_exists(self):
120119
for middleware in [MIDDLEWARE_CSP_BEFORE, MIDDLEWARE_CSP_LAST]:
121120
with self.settings(MIDDLEWARE=middleware):
122-
response = cast(HttpResponse, self.client.get(path="/csp_view/"))
121+
response = self.client.get(path="/csp_view/")
123122
self.assertEqual(response.status_code, 200)
124123

125-
html_root: Element = self.parser.parse(stream=response.content)
124+
html_root = self.parser.parse(stream=response.content)
126125
self._fail_on_invalid_html(content=response.content, parser=self.parser)
127126
self.assertContains(response, "djDebug")
128127

129128
namespaces = get_namespaces(element=html_root)
130-
context: ContextList = response.context # pyright: ignore[reportAttributeAccessIssue]
129+
context = response.context
131130
nonce = str(context["toolbar"].csp_nonce)
132131
self._fail_if_missing(
133132
root=html_root, path=".//link", namespaces=namespaces, nonce=nonce
@@ -139,14 +138,14 @@ def test_redirects_exists(self):
139138
def test_panel_content_nonce_exists(self):
140139
for middleware in [MIDDLEWARE_CSP_BEFORE, MIDDLEWARE_CSP_LAST]:
141140
with self.settings(MIDDLEWARE=middleware):
142-
response = cast(HttpResponse, self.client.get(path="/csp_view/"))
141+
response = self.client.get(path="/csp_view/")
143142
self.assertEqual(response.status_code, 200)
144143

145144
toolbar = list(DebugToolbar._store.values())[-1]
146145
panels_to_check = ["HistoryPanel", "TimerPanel"]
147146
for panel in panels_to_check:
148147
content = toolbar.get_panel_by_id(panel).content
149-
html_root: Element = self.parser.parse(stream=content)
148+
html_root = self.parser.parse(stream=content)
150149
namespaces = get_namespaces(element=html_root)
151150
nonce = str(toolbar.csp_nonce)
152151
self._fail_if_missing(
@@ -164,10 +163,10 @@ def test_panel_content_nonce_exists(self):
164163

165164
def test_missing(self):
166165
"""A `nonce` should not exist when not using the `CSPMiddleware`."""
167-
response = cast(HttpResponse, self.client.get(path="/regular/basic/"))
166+
response = self.client.get(path="/regular/basic/")
168167
self.assertEqual(response.status_code, 200)
169168

170-
html_root: Element = self.parser.parse(stream=response.content)
169+
html_root = self.parser.parse(stream=response.content)
171170
self._fail_on_invalid_html(content=response.content, parser=self.parser)
172171
self.assertContains(response, "djDebug")
173172

0 commit comments

Comments
 (0)