Literal typeguard
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Feature
Infer str is an instance of Literal based on code path:
from typing import Literal
def f(x: Literal["a", "b"]):
...
def g(x: str):
if x == "a":
...
elif x == "b":
...
else:
raise ValueError()
f(x) # ERROR: "f" has incompatible type "str"; expected "Union[Literal['a'], Literal['b']]"
Mypy reports error for the above code, but x is actually an instance of Literal["a", "b"], because all values other than "a" or "b" raises ValueError.
Pyright, one of type checkers, can infer this code has no problem.
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 Python reproducer in the issue and compare mypy's result with the stated Pyright behavior. No source files or tests are named, so locating the relevant type-narrowing implementation and existing test conventions is part of the work. Done means the call to f(x) is accepted after the exhaustive checks while other paths remain correctly handled.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100