python / python/mypy

type checking of member assigned with lambda

Open
#13,818 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

What this https://github.com/python/mypy/pull/13400 looks like it's supposed to fix is not working here.

To Reproduce

from typing import Callable


class C:
    f: Callable[[int], bool] = staticmethod(lambda x: print(type(x)) or True)


c = C()
c.f(3)


class D:
    f: Callable[[int], bool] = lambda x: print(type(x)) or True


d = D()
d.f(3)

Expected Behavior

The one that works correctly should not have a mypy error.
The one that doesn't work correctly should be found by mypy.

Actual Behavior

The first one, class C, works correctly, but mypy says it's wrong.
output:

<class 'int'>

mypy:
Incompatible types in assignment (expression has type "staticmethod[bool]", variable has type "Callable[[int], bool]") [assignment]

The second one, class D, fails, but mypy doesn't see any problem with it.
output:

Traceback (most recent call last):
  File ...
    d.f(3)
TypeError: <lambda>() takes 1 positional argument but 2 were given

Your Environment

  • Mypy version used: 0.990+dev
  • Python version used: 3.8

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.

Research direction

Start by running the supplied Python 3.8 reproducer with the reported mypy version and compare the diagnostics for classes C and D. Trace mypy's handling of annotated class-member assignments involving lambda expressions and staticmethod; done means the valid C assignment is accepted and the invalid D binding is reported, with regression coverage for both cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.