NoReturn return type with singledispatch causing weird errors
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
[This was originally in a comment by @Dreamsorcerer]
...
This issue has mostly talked about the redefinition error. But, we should also have singledispatch typing working. e.g. With this example:
@singledispatch
def foo(data: object) -> NoReturn:
raise ValueError(f"Unhandled type {type(data)}")
@foo.register
def _(data: str) -> str:
return data
@foo.register
def _(data: int) -> str:
return str(data)
a = foo(7)
reveal_type(a)
You also get errors due to not recognising the overloads:
src/service/transaction_status_service.py:72: error: Need type annotation for "a" [var-annotated]
src/service/transaction_status_service.py:73: note: Revealed type is "<nothing>"
It would be good for mypy to treat the @foo.register like an @overload.
I was also getting unreachable code errors after every call to the defined function. However, I was unable to reproduce it with a minimal example, so not sure exactly what is causing that.
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
Start with the singledispatch example in the issue and review the linked original comment and discussion for the existing redefinition behavior. Reproduce the reported errors, then verify that registered implementations are treated like overloads, with the call result inferred as str and the spurious unreachable-code and variable-annotation errors addressed.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100