Skip to content

Commit ffa62d9

Browse files
committed
Replaced decorator that prevents sending Response multiple times with method
IDE was getting confused by decorated method and was not displaying the type hint properly
1 parent 142c89b commit ffa62d9

File tree

1 file changed

+8
-18
lines changed

1 file changed

+8
-18
lines changed

adafruit_httpserver/response.py

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"""
99

1010
try:
11-
from typing import Optional, Dict, Union, Tuple, Callable
11+
from typing import Optional, Dict, Union, Tuple
1212
from socket import socket
1313
from socketpool import SocketPool
1414
except ImportError:
@@ -29,21 +29,6 @@
2929
from .headers import Headers
3030

3131

32-
def _prevent_multiple_send_calls(function: Callable):
33-
"""
34-
Decorator that prevents calling ``send`` or ``send_file`` more than once.
35-
"""
36-
37-
def wrapper(self: "Response", *args, **kwargs):
38-
if self._response_already_sent: # pylint: disable=protected-access
39-
raise ResponseAlreadySentError
40-
41-
result = function(self, *args, **kwargs)
42-
return result
43-
44-
return wrapper
45-
46-
4732
class Response:
4833
"""
4934
Response to a given `Request`. Use in `Server.route` handler functions.
@@ -162,7 +147,11 @@ def _send_headers(
162147
self.request.connection, response_message_header.encode("utf-8")
163148
)
164149

165-
@_prevent_multiple_send_calls
150+
def _check_if_not_already_sent(self) -> None:
151+
"""Prevents calling ``send`` or ``send_file`` more than once."""
152+
if self._response_already_sent:
153+
raise ResponseAlreadySentError
154+
166155
def send(
167156
self,
168157
body: str = "",
@@ -174,6 +163,7 @@ def send(
174163
175164
Should be called **only once** per response.
176165
"""
166+
self._check_if_not_already_sent()
177167

178168
if getattr(body, "encode", None):
179169
encoded_response_message_body = body.encode("utf-8")
@@ -214,7 +204,6 @@ def _get_file_length(file_path: str) -> int:
214204
except OSError:
215205
raise FileNotExistsError(file_path) # pylint: disable=raise-missing-from
216206

217-
@_prevent_multiple_send_calls
218207
def send_file( # pylint: disable=too-many-arguments
219208
self,
220209
filename: str = "index.html",
@@ -230,6 +219,7 @@ def send_file( # pylint: disable=too-many-arguments
230219
231220
Should be called **only once** per response.
232221
"""
222+
self._check_if_not_already_sent()
233223

234224
if safe:
235225
self._check_file_path_is_valid(filename)

0 commit comments

Comments
 (0)