Skip to content

Lambda properties on objects don't typecheck correctly (mypy assumes self is passed) #7020

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ezyang opened this issue Jun 18, 2019 · 1 comment

Comments

@ezyang
Copy link

ezyang commented Jun 18, 2019

I wanted to store a lambda in an object and then call it. Mypy doesn't like it.

from typing import Callable

class A:
    f: Callable[[bool], bool]
    def __init__(self, f):
        self.f = f

def foo(b: bool) -> bool:
    return b

a = A(foo)
print(a.f(True))

Running:

dhcp-172-31-207-247:~ ezyang$ python3.7 bb.py 
True
dhcp-172-31-207-247:~ ezyang$ mypy --version
mypy 0.660
dhcp-172-31-207-247:~ ezyang$ mypy bb.py
bb.py:12: error: Invalid self argument "A" to attribute function "f" with type "Callable[[bool], bool]"
bb.py:12: error: Too many arguments

(The reason I want to do this is because the object is a dataclass and I'm not exactly sure how well inheritance works with dataclasses, so I thought, maybe I can keep things simple by just storing callables on the object.)

@ilevkivskyi
Copy link
Member

This is actually a duplicate of #708 (although this may be non-obvious), see also #6910

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants