Type issues with Callables
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
Research direction
Start by running the supplied detect.py reproducer with the reported mypy versions and inspect how Callable and TypeVar inference handle the lambda and is_even arguments. Trace the relevant type-checking behavior and add regression coverage where appropriate; done means the example typechecks without the two reported errors.
Written by the indexing model from the issue text.
Description
-
Are you reporting a bug, or opening a feature request?
bug -
Please insert below the code you are checking with mypy
from typing import Optional, Iterable, TypeVar, Callable
T = TypeVar('T')
def is_even(n: int) -> bool:
return n % 2 == 0
# equivalent to https://ruby-doc.org/core-2.5.0/Enumerable.html#method-i-detect
def detect(function: Callable[[T], bool],
iterable: Iterable[T]) -> Optional[T]:
for element in iterable:
if function(element):
return element
return None
numbers = [1, 2, 3, 4, 5]
# using a lambda
print(detect(lambda n: n % 2 == 0, numbers))
# using a function
print(detect(is_even, numbers))
- What is the actual behavior/output?
$ mypy detect.py
detect.py:22: error: Unsupported operand types for % ("object" and "int")
detect.py:25: error: Argument 1 to "detect" has incompatible type "Callable[[int], bool]"; expected "Callable[[object], bool]"
Found 2 errors in 1 file (checked 1 source file)
-
What is the behavior/output you expect?
I was expecting the types to be inferred and the above code to typecheck. -
What are the versions of mypy and Python you are using?
$ mypy --version
mypy 0.770
$ python --version
Python 3.7.7
- Do you see the same issue after installing mypy from Git master?
yes
$ mypy detect.py
detect.py:22: error: Unsupported operand types for % ("object" and "int")
detect.py:25: error: Argument 1 to "detect" has incompatible type "Callable[[int], bool]"; expected "Callable[[object], bool]"
Found 2 errors in 1 file (checked 1 source file)
$ mypy --version
mypy 0.770+dev.22c67daac7d2b28f33d4e9c7bee4f1a06e61a3e5
-
What are the mypy flags you are using? (For example --strict-optional)
None. -
If mypy crashed with a traceback, please paste the full traceback below.
It didn't crash
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- Avg merge
- 1d 18h
- Merged PRs (30d)
- 54
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.
More from python/mypy
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
documentation
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
bug topic-configuration topic-error-reporting
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
bancolombia/sentinel#23 ·
-
test md OpenCI
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
-
integration:quickjs org:external priority:backlog topic:code-interpreter topic:middleware type:feature
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
langchain-ai/deepagents#6450 ·
-
bug client
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100