-
-
Notifications
You must be signed in to change notification settings - Fork 260
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix mypy warnings in router.py #1038
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,11 +53,10 @@ def _format_response( | |
) -> Response: | ||
headers = Headers({"Content-Type": "text/plain"}) | ||
|
||
response = {} | ||
if isinstance(res, dict): | ||
# this should change | ||
headers = Headers({}) | ||
if "Content-Type" not in headers: | ||
if not headers.contains("Content-Type"): | ||
headers.set("Content-Type", "application/json") | ||
|
||
description = jsonify(res) | ||
|
@@ -88,16 +87,16 @@ def _format_response( | |
if len(res) != 3: | ||
raise ValueError("Tuple should have 3 elements") | ||
else: | ||
description, headers, status_code = res | ||
description = self._format_response(description).description | ||
new_headers = Headers(headers) | ||
if "Content-Type" in new_headers: | ||
headers.set("Content-Type", new_headers.get("Content-Type")) | ||
description2, headers2, status_code2 = res | ||
description2 = self._format_response(description2).description | ||
new_headers: Headers = Headers(headers2) | ||
if new_headers.contains("Content-Type"): | ||
headers2.set("Content-Type", new_headers.get("Content-Type")) | ||
Comment on lines
+90
to
+94
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hey @dave42w 👋 Thank you for the PR :D There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. They just have to be different names to the ones earlier in _format_response. mypy objects to reusing a variable (even if it has gone out of scope) with a different type. eg this is unacceptable
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sansyrox are we ok to consider this point resolved? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hey @dave42w 👋 Apologies for the late review.
We can disable mypy for these lines then.
This is not a good reason IMO. If the code makes sense in our repo, we should not alter it according to mypy. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. H'mm, it seems risky to me. We are deep in lots of nesting at this point. It would be easy for a code change to end up trying to use these variables later in the function (especially because we don't return early) and depending on the data have 2 different types. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alright then. but can we get rid of numbers in the variable names. I get a massive ick by that(I know and I am sorry) but something else without involving numbers? |
||
|
||
response = Response( | ||
status_code=status_code, | ||
headers=headers, | ||
description=description, | ||
status_code=status_code2, | ||
headers=headers2, | ||
description=description2, | ||
) | ||
else: | ||
response = Response( | ||
|
@@ -291,7 +290,7 @@ def add_auth_middleware(self, endpoint: str): | |
This method adds an authentication middleware to the specified endpoint. | ||
""" | ||
|
||
injected_dependencies = {} | ||
injected_dependencies: dict = {} | ||
|
||
def decorator(handler): | ||
@wraps(handler) | ||
|
@@ -320,7 +319,7 @@ def inner_handler(request: Request, *args): | |
# Arguments are returned as they could be modified by the middlewares. | ||
def add_middleware(self, middleware_type: MiddlewareType, endpoint: Optional[str]) -> Callable[..., None]: | ||
# no dependency injection here | ||
injected_dependencies = {} | ||
injected_dependencies: dict = {} | ||
|
||
def inner(handler): | ||
@wraps(handler) | ||
|
@@ -383,7 +382,7 @@ def get_global_middlewares(self) -> List[GlobalMiddleware]: | |
class WebSocketRouter(BaseRouter): | ||
def __init__(self) -> None: | ||
super().__init__() | ||
self.routes = {} | ||
self.routes: dict = {} | ||
|
||
def add_route(self, endpoint: str, web_socket: WebSocket) -> None: | ||
self.routes[endpoint] = web_socket | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @dave42w , would this not be false always? I think we have a logical bug in Robyn
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree. I was just fixing the mypy first. I think this is ripe for a refactor to clear up the logic, reduce complexity and add unit tests.