Mypy is still too eager about outer context
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Consider this example:
T = TypeVar('T')
S = TypeVar('S')
def add(x: List[T], y: List[S]) -> List[Union[T, S]]: ...
a: List[int]
b: List[str]
e: List[Union[int, str]] = add(a, b)
It fails on the last line with the following (I skip invariance notes):
error: Argument 1 to "add" has incompatible type "List[int]"; expected "List[Union[int, str]]"
error: Argument 2 to "add" has incompatible type "List[str]"; expected "List[Union[int, str]]"
If I remove the annotation for e, everything works fine.
When previously working on the external context issues, I found that I don't like how constrains for unions against unions are inferred (not that they are wrong, just counterintuitive). For example, Union[T, S] <: Union[str, int] infers T < : Union[str, int] and S <: Union[str, int], which is technically fine, but leads to errors like above.
This blocks https://github.com/python/mypy/pull/5873
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 by reproducing the annotated add(a, b) example with mypy and read the discussion around the referenced pull request 5873. Determine the intended constraint inference for unions against unions, then verify that the example succeeds without introducing regressions.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100