Skip to content

How can I provide a type hint for an object returned from the recv_json method? #2062

Closed Answered by minrk
wigging asked this question in Q&A
Discussion options

You must be logged in to vote

Using a TypedDict still causes the pyright error:

You still always need to cast or type:ignore to convert from one type to another, so:

json_data = cast(JSONDict, socket.recv_json())
# or
json_data: JSONDict = socket.recv_json() # type: ignore

but the key and value types are still missing from the type hints.

Yes, this is precisely what TypedDict is for, to annotate a dict and all its keys and values with a single annotation.

What do you mean by "message schema"? How would I use it without having to cast again?

Sorry, I think you understood the concept even if my words weren't clear. I only meant what you did in your example, which is to specify which fields are defined and what typ…

Replies: 2 comments 5 replies

Comment options

You must be logged in to vote
1 reply
@wigging
Comment options

Comment options

You must be logged in to vote
4 replies
@wigging
Comment options

@wigging
Comment options

@minrk
Comment options

@wigging
Comment options

Answer selected by wigging
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants