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

chore: upgrade pyright to 1.1.385 #1438

Merged
merged 1 commit into from
Oct 21, 2024
Merged

chore: upgrade pyright to 1.1.385 #1438

merged 1 commit into from
Oct 21, 2024

Conversation

IronCore864
Copy link
Contributor

@IronCore864 IronCore864 commented Oct 18, 2024

Upgrade pyright version to the latest (1.1.385).

@IronCore864 IronCore864 marked this pull request as ready for review October 18, 2024 04:41
Copy link
Collaborator

@benhoyt benhoyt left a comment

Choose a reason for hiding this comment

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

Wow, much simpler than previous Pyright upgrades! Maybe Python types are stabilising after all.

@@ -1359,7 +1359,7 @@ def _request(
raise resp
if callable(resp):
resp = resp()
return resp
return resp # type: ignore
Copy link
Contributor

@tonyandrewmeyer tonyandrewmeyer Oct 19, 2024

Choose a reason for hiding this comment

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

Do we know why the ignore is needed now? Can we do anything else to avoid it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had done some testings before ignoring this, and I think it's because the self.responses is typing.List[typing.Any].

If a response is an exception, it's raised. If it's a callable, it's called and the returned type is an object. If none of those cases are matched, it's returned.

So the returned result type is object | Any, which doesn't match the method's return type typing.Dict[str, typing.Any].

Casting the final return would help, like return typing.cast(typing.Dict[str, typing.Any], resp). I ignored it because I think it's simpler, since the return type is already annotated with this type.

I'm merging now with the ignore, if the type casting is preferred, I can change it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, so probably the best thing would be to tighten up the annotation for responses (or not overload it as much) to avoid needing the ignore or a cast. I guess the newer pyright found this but the older one didn't for some reason.

Leaving this for another day seems fine - I don't think it's worth opening an issue or PR now.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah, it's in a test file so I don't think worth spending more time on now.

Copy link
Contributor

Choose a reason for hiding this comment

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

When in doubt, do this:

from typing import reveal_type

reveal_type(resp)

I've poked around for a couple of minutes and discovered that:

  • if resp is callable, that callable always raises, and the line above is misleading
  • it's trivial to set the correct type, (below) and even our lambdas are auto-detcted when because they call functions that raise uconditionally
self.responses: list[Callable[[], NoReturn]|dict[str, Any]|Exception] = []

(code above assumes from __future__ import annotations and from typing import ...)

@tonyandrewmeyer
Copy link
Contributor

Wow, much simpler than previous Pyright upgrades! Maybe Python types are stabilising after all.

Its ominous that one of the two features landed for 3.14 is type related though 😂.

@IronCore864 IronCore864 merged commit 262c751 into main Oct 21, 2024
31 checks passed
@IronCore864 IronCore864 deleted the upgrade-pyright branch October 21, 2024 01:21
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.

4 participants