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

Fix mypy warnings in router.py #1038

wants to merge 2 commits into from

Conversation

dave42w
Copy link
Contributor

@dave42w dave42w commented Nov 19, 2024

Description

This PR fixes most of the mypy warnings in router.py

Summary

This PR does not fix #1035 where add_route is inconsistent between the BaseRouter(ABC) and it's subclasses.

The biggest changes were in Router. _format_response

  • fixing re-use of local variable names but with a different type
  • using if "x" in headers replaced with if headers.contains("x")
  • missing type specifiers

Beyond that a few places with a missing dict type: for injected_dependencies x 2 and self.reoutes x 1

PR Checklist

Please ensure that:

  • [x ] The PR contains a descriptive title
  • [x ] The PR contains a descriptive summary of the changes
  • [x ] You build and test your changes before submitting a PR.
  • [x ] You have added relevant documentation
  • [ x] You have added relevant tests. We prefer integration tests wherever possible

Pre-Commit Instructions:

Copy link

vercel bot commented Nov 19, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
robyn ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 20, 2024 10:46pm

Copy link

codspeed-hq bot commented Nov 19, 2024

CodSpeed Performance Report

Merging #1038 will not alter performance

Comparing dave42w:mypy2 (be555e7) with main (3f2e79a)

Summary

✅ 146 untouched benchmarks

Comment on lines +90 to +94
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"))
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?

Comment on lines 58 to -60
headers = Headers({})
if "Content-Type" not in headers:
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.

@dave42w dave42w mentioned this pull request Nov 21, 2024
6 tasks
@dave42w dave42w closed this Nov 22, 2024
@dave42w dave42w deleted the mypy2 branch November 22, 2024 22:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BaseRouter and Router have different signatures for add_route
2 participants