Skip to content

Attribute functions in stubs and dataclasses.Field #10750

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
hauntsaninja opened this issue Jul 1, 2021 · 3 comments
Closed

Attribute functions in stubs and dataclasses.Field #10750

hauntsaninja opened this issue Jul 1, 2021 · 3 comments
Labels
bug mypy got something wrong topic-dataclasses

Comments

@hauntsaninja
Copy link
Collaborator

So something like the following gives me main.py:4: error: Attribute function "default_factory" with type "Callable[[], _T]" does not accept self argument:

import dataclasses

def foo(x: dataclasses.Field):
    if x.default_factory is not dataclasses.MISSING:
        ...

Okay, hmm. Let's try to repro:

from typing import Callable
        
class X:
    # f: Callable[[], str]
    def __init__(self) -> None:
        self.f = lambda: "whatever"

x = X()
reveal_type(x.f)
reveal_type(x.f())

This works fine! But if you uncomment the commented line, then you're back to the error, which is problematic if you want to express this in stubs. I can get things checking again by using a callback protocol — is that the best one can do?

@hauntsaninja hauntsaninja added the bug mypy got something wrong label Jul 1, 2021
@hauntsaninja
Copy link
Collaborator Author

Opened python/typeshed#5718 to help with this

@hauntsaninja
Copy link
Collaborator Author

hauntsaninja commented Jul 1, 2021

Okay, also found #6910

@AlexWaygood
Copy link
Member

@hauntsaninja, are you happy for this to be closed, given the immediate problem has been resolved, and given that it's the same bug as #6910? :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-dataclasses
Projects
None yet
Development

No branches or pull requests

3 participants