(🎁) Narrow type based on method return type of union
Open
Nobody has claimed this yet.
feature
priority-2-low
topic-type-narrowing
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
I would expect the type of either to be narrowed to Maybe in the if block.
from typing import Literal
class Maybe:
def should(self) -> bool:
...
class No:
def should(self) -> Literal[False]:
...
no: No
if no.should():
reveal_type(no) # error: unreachable
either: Maybe | No
if either.should():
reveal_type(either) # Maybe | No
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 implementation files or tests are named. Start by running the supplied reproducer through mypy and locating existing tests for method-based type narrowing. Done means the if either.should() branch reveals either as Maybe while preserving the demonstrated No behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100