Skip to content
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

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions robyn/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Comment on lines 58 to -60
Copy link
Member

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

Copy link
Contributor Author

@dave42w dave42w Nov 21, 2024

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.

if not headers.contains("Content-Type"):
headers.set("Content-Type", "application/json")

description = jsonify(res)
Expand Down Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @dave42w 👋

Thank you for the PR :D
One question -
why are we using description2 or headers2 here? Is it something that mypy suggests?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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

if x:
  y: int = 5
else:
  y: str = "five"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sansyrox are we ok to consider this point resolved?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @dave42w 👋

Apologies for the late review.

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

We can disable mypy for these lines then.

mypy objects to reusing a variable (even if it has gone out of scope) with a different type

This is not a good reason IMO. If the code makes sense in our repo, we should not alter it according to mypy.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.
I would like to follow this with a more significant refactor anyway to reduce the levels of indentation/nested if statements. At that point this will not be a problem.

Copy link
Member

Choose a reason for hiding this comment

The 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(
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
Loading