Covariant generic type context results in confusing message with __add__
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
This generates a false positive on the last line:
from typing import TypeVar, Generic, Union, Any
T = TypeVar('T')
Tcov = TypeVar('Tcov', covariant=True)
S = TypeVar('S')
class A(Generic[Tcov]): pass
class B(A[T]):
def __add__(self, x: B[S]) -> B[Union[T, S]]: ...
a: A[object]
b1: B[Any]
b2: B[int]
a = b1 + b2 # Unsupported operand types for + ("B[Any]" and "B[int]")
The error message is confusing, since B[Any] and B[int] are compatible.
We perhaps shouldn't use the type context from a for type inference in this case.
This issue affects built-in list and Sequence type context with the latest typeshed master.
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 Python example and tracing type inference for the add call when the target has a covariant generic context. Compare the behavior with built-in list and Sequence cases mentioned in the issue. Done means compatible operands no longer produce the false-positive error or confusing message, with the reported cases covered by tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100