mypy doesn't work nicely with lambdas returning named tuples

Open
#5,069 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
python

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

documentation topic-usability

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')

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from python/mypy

All issues in python/mypy

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.