Skip to content

Error when accessing dataclasses.Field.default_factory #3246

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
sloria opened this issue Sep 19, 2019 · 2 comments
Closed

Error when accessing dataclasses.Field.default_factory #3246

sloria opened this issue Sep 19, 2019 · 2 comments

Comments

@sloria
Copy link

sloria commented Sep 19, 2019

Typechecking with mypy produces a spurious error when accessing default_factory of a dataclass field.

# t.py
import dataclasses


@dataclasses.dataclass
class Foo:
    bar: int


field = dataclasses.fields(Foo)[0]
assert field.default_factory == dataclasses.MISSING
mypy t.py

Output:

t.py:10: error: Attribute function "default_factory" with type "Callable[[], Any]" does not accept self argument

Using mypy==0.720 on Python 3.7.4.

@srittau
Copy link
Collaborator

srittau commented Sep 19, 2019

This looks like a mypy problem to me. The following does not type check either:

from typing import Callable

class Foo:
    bar: Callable[[], None]

assert Foo().bar == 1

@sloria
Copy link
Author

sloria commented Sep 19, 2019

Ah, found it: python/mypy#6910 . Sorry for the noise.

@sloria sloria closed this as completed Sep 19, 2019
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