-
-
Notifications
You must be signed in to change notification settings - Fork 530
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
Overload return type of recv
#1578
Comments
Yes, this makes sense to me. |
Worth checking for other instances of the same problem throughout the library. |
I tried it but mypy doesn't survive the combination of async iteration and type overloads, as you can see in the CI run:
This looks like python/mypy#10301. |
Using the workaround suggested in that mypy issue appears to work. |
Would you be able to confirm that #1579 does what you want, please? Especially if you're using recv_streaming as I'm somewhat puzzled by the mypy workaround... |
From what I understand, this isn't so much a Mypy bug as a weird side-effect of the way I played around in the Mypy playground, as well as with Pyright, and removing Also note that I removed the default values from the overloads, otherwise Pyright complains about overlapping overloads. |
When the default values aren't included in the overloads — i.e. when I follow your original suggestion — I get this error:
|
Weird, I'm not seeing that behavior in the playground. What if you only have a default for the |
Never mind, I was missing an annotation so Mypy wasn't checking the function call. I was able to reproduce your error, and adding a default to the |
I'm still running in trouble with this one: def get(self, timeout: float | None = None, decode: bool | None = None) -> Data:
|
I tried running pyright to come up with overloads that work well in mypy + pyright. Unfortunately pyright failed at the most basic step:
I spent some time exploring but I don't understand what's failing there. |
I'm not sure about the With regards to the
It's not pretty, but it's probably a good idea to deprecate passing |
Good catch 🤦 I'll take more time to setup pyright properly and play with it. |
I still don't understand how to setup pyright. It doesn't appear to behave like it's documented to behave. I filed microsoft/pyright#9752. |
Okay. Anyway I don't think it necessarily makes sense to get the library to pass both Mypy and Pyright, since they have very different configuration systems. I would just check that downstream projects using |
I agree about the goal. I was just thinking that "type checking properly within websockets" was a good step towards "type checking properly in downstream projects". |
Getting websockets to pass pyright appears to a be a significant endeavor. I'm not taking this route. The other problem is python/mypy#7333 Indeed, the only countermeasure for this problem is using a keyword-only argument. Looking into this now. |
EDIT: we can do it only in the overload as shown in a comment above 👍 |
Currently
recv
andrecv_streaming
are typed as always returningData
, even ifdecode
is set. This means that to satisfy type checkers, we need to useisinstance
,cast
, etc.I propose using
typing.overload
to narrow the return type whendecode
is notNone
:Alternatively, new
recv_str
/recv_bytes
methods can automatically raise an Exception if the received data is not of the expected type.The text was updated successfully, but these errors were encountered: