Narrowing IN with unions of TypedDict. #5070
JanEricNitschke
started this conversation in
General
Replies: 1 comment
-
Type narrowing doesn't apply in this case. The type of I would implement this as follows: def _get_actor_key(actor: Literal["1", "2"], game_action: Test1 | Test2) -> str:
return game_action.get("test" + actor, "") |
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
-
I currently have a situation like below where i have multiple TypedDicts with (overlapping) keys of similar structure and i need to extract values from them. However i would like to avoid writing separate functions for each dict that do the same thing.
So i was trying to do something like this:
If i replace the type hints in the implementation with Any i get the desired behaviour of pyright rejecting invalid function calls like
_get_action_key("1", Test2)
.But i can not managed to type hint the body with anything except any without it getting rejected. If i do the below it complains that "test1" is not a key for Test2 and vice versa. Is there any reasonable chance that something like this could be added to pyright or is there any way i could type hint this better or is just using Any my best bet here?
Beta Was this translation helpful? Give feedback.
All reactions