Replies: 1 comment
-
I haven't tried this but your link #685 has another link to python/mypy#7856 which has an example that might help. Here's an excerpt: @overload
def __getitem__(self, key: Literal["a"]) -> str:
...
@overload
def __getitem__(self, key: Literal["b", "c"]) -> int:
... This is like what you're asking for, mapping arbitrary constants like Does this help? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
When typing REST API responses, I usually use this pattern in TypeScript:
This is really handy because now I just need to specify the API endpoint and it uses the corresponding return type for me.
I want to reproduce this in Python with the
httpx
library, so I have:The issue here is that the return type is
Any
which is not ideal.I didn't find how to reproduce this example from TypeScript with generic function return type (which is not in any arguments):
https://stackoverflow.com/questions/72793247/specify-generic-return-type-without-relying-on-arguments-in-python
So I found this hack with a callable generic class:
I think this isn't the best way by far to achieve this and even more if I want the response type to depend on the route value (
keyof
behavior).Linked an issue about Keyof type: #685
Is is still relevant for Python 3.11?
Any feedback and input would be greatly appreciated, thanks! ;)
Beta Was this translation helpful? Give feedback.
All reactions