Better error message for incompatible type assignment when using Generics
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
- Are you reporting a bug, or opening a feature request?
Feature Request
- Please insert below the code you are checking with mypy.
from typing import Generic, TypeVar
T = TypeVar("T", int, float)
class Foo(Generic[T]):
def __init__(self, val: T):
self.data: T = val
def bar(x: T) -> Foo[T]:
return Foo(x)
x = bar(1)
x = bar(2.0) # error thrown by mypy
- What is the actual behavior/output?
mypy throws this error:
error: Argument 1 to "bar" has incompatible type "float"; expected "int"
- What is the behavior/output you expect?
This error message is thrown because of the combination of assigning a different type to x and using Generic. The error message is unclear because it indicates a type error on the argument passed to bar and can be interpreted as bar not accepting arguments of type float.
Having a different error message that mentions "Incompatible types in assignment" would be clearer.
A message to make it clearer that expression has type Foo[float], variable has type Foo[int]
- What are the versions of mypy and Python you are using?
mypy 0.770 and Python 3.7.6
- What are the mypy flags you are using? (For example --strict-optional)
None
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 Generic example with mypy 0.770 and Python 3.7.6, focusing on the assignment from Foo[int] to Foo[float]. Trace the diagnostic produced for the call to bar and assignment to x; done means the message clearly identifies the incompatible variable and expression types rather than implying that bar rejects float arguments.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100