Document how to declare dependant types in method signatures
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
I have several TypedDict:
TerminateHandlerContextType = TypedDict( "TerminateHandlerContextType",{...},)
CrashHandlerContextType = TypedDict( "CrashHandlerContextType", {... },)
QuitHandlerContextType = TypedDict("QuitHandlerContextType", {...})
And then related Callable:
TerminateHandlerType = Callable[[TerminateHandlerContextType], None]
QuitHandlerType = Callable[[QuitHandlerContextType], None]
CrashHandlerType = Callable[[CrashHandlerContextType], None]
If I then declare a method that depends of both:
def _call_handler(
self,
handler: Union[
TerminateHandlerType, QuitHandlerType, CrashHandlerType
],
context: Union[
TerminateHandlerContextType, QuitHandlerContextType, CrashHandlerContextType,
],
):
...
return handler(context) # <---- MYPY COMPLAINS HERE
Then mypy complains, because, while I always ensure logically that the proper handler is called with the proper context, it can't see that.
I couldn't find in the documentation a way to tell it that the context type depends on the handler type. I assume it's either a Generic, or a TypeVar, but didn't find the solution.
mypy 0.770
python 3.6
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
No documentation file, test, or entry point is named in the issue. First determine which mypy documentation section covers dependent relationships between handler and context types, then verify the documented approach against the TypedDict and Callable example. Done means the documentation explains how to express or handle this relationship and addresses the reported type-checking complaint.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Documentation
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100