Changing just the return type annotation causes incompatible argument error
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
When a return type annotation changes from Iterable of Optional typevars into Iterable of Tuples of Optional typevars mypy starts to signal an error in function call arguments (invalid callback argument type).
To Reproduce
from typing import Callable, Iterable, Optional, Tuple, TypeVar
SourceType = TypeVar("SourceType")
TransformedType = TypeVar("TransformedType")
ProcessedType = TypeVar("ProcessedType")
def simple_transform_process(
source: Iterable[SourceType],
transform_callback: Callable[[SourceType], Optional[TransformedType]],
process_callback: Callable[[TransformedType], ProcessedType],
) -> Iterable[Optional[ProcessedType]]:
for item in source:
transformed = transform_callback(item)
if transformed is None:
yield None
else:
yield process_callback(transformed)
def transform_process(
source: Iterable[SourceType],
transform_callback: Callable[[SourceType], Optional[TransformedType]],
process_callback: Callable[[TransformedType], ProcessedType],
) -> Iterable[Tuple[Optional[TransformedType], Optional[ProcessedType]]]:
for item in source:
transformed = transform_callback(item)
if transformed is None:
yield None, None
else:
yield transformed, process_callback(transformed)
def transform(x: str) -> Optional[int]:
if x:
return int(x)
else:
return None
def process(x: int) -> float:
return x / 2
def test_simple_transform_process() -> None:
assert list(simple_transform_process(["", "2", "3"], transform, process)) == [
None,
1,
1.5,
]
def test_transform_process() -> None:
assert list(transform_process(["", "2", "3"], transform, process)) == [
(None, None),
(2, 1),
(3, 1.5),
]
(Write your steps here:)
- Run
mypyon the above codemypy test_mypy_problem.py
Expected Behavior
The code should have passed the type checker for both simple_transform_process call and transform_process call as both of those have the same exact signatures of input parameters.
Actual Behavior
mypy reports the following error on transform_process call:
test_mypy_problem.py:54: error: Argument 3 to "transform_process" has incompatible type "Callable[[int], float]"; expected "Callable[[Optional[int]], Optional[float]]"
The expected type of argument should be Callable[[int], float], just like for simple_transform_process and the code should pass the type checker.
Your Environment
- Mypy version used:
0.790,0.800,mypy 0.820+dev.dc4f0af163ed3748311115fd896494262c0dc644 - Mypy command-line flags:
mypy test_mypy_problem.py - Mypy configuration options from
mypy.ini(and other config files): no config file used - Python version used:
Python 3.8.5 - Operating system and version:
Ubuntu 20.04.2 LTS
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 reproducer in test_mypy_problem.py and run mypy test_mypy_problem.py using the reported Python and mypy versions. Trace how generic inference handles the transform_process callback versus simple_transform_process, then add a regression test showing both calls type-check successfully.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100