mypy doesn't work nicely with lambdas returning named tuples
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
Research direction
Start by running the minimal repro in a.py with mypy --strict and compare the reported error with the expected behavior for lambda bidirectional type inference described in the issue. Trace how the lambda returning NamedTuple A is inferred, then add or update a focused regression test showing that greet is typed without an unknown-function error.
Written by the indexing model from the issue text.
Description
Note: if you are reporting a wrong signature of a function or a class in
the standard library, then the typeshed tracker is better suited
for this report: https://github.com/python/typeshed/issues
Please provide more information to help us understand the issue:
- Are you reporting a bug, or opening a feature request? bug
- Please insert below the code you are checking with mypy,
or a mock-up repro if the source is private. We would appreciate
if you try to simplify your case to a minimal repro.
from typing import NamedTuple
class A(NamedTuple):
'''
value class combining origin coordinate with list of destination coordinates
'''
a: str
def foo() -> None:
A("david")
greet = lambda a: A("hi " + a)
greet('moshe')
- What is the actual behavior/output?
a.py:14: error: Call to untyped function (unknown) in typed context - What is the behavior/output you expect?
mypy should actually be smart enough to see that i am returning a named tuple: http://mypy.readthedocs.io/en/latest/kinds_of_types.html#callable-types-and-lambdas
Lambdas are also supported. The lambda argument and return value types cannot be given explicitly; they are always inferred based on context using bidirectional type inference:
- What are the versions of mypy and Python you are using?
the latest and greatest: mypy 0.600 and python 3.6.5
Do you see the same issue after installing mypy from Git master? Didn't try - What are the mypy flags you are using? (For example --strict-optional)
--strict
- 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 1/5 Under an hour Newbie friendliness 90/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
zostera/django-bootstrap4#894 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
use-agent-os/agent-os#3276 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
zephyrproject-rtos/zephyr#119726 ·
-
area/auth bug comp/agent P3 platform/discord type/security
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
NousResearch/hermes-agent#117848 ·