Skip to content
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

Access to instance attribute with callable type causes error #6910

Closed
unknownlighter opened this issue May 29, 2019 · 14 comments
Closed

Access to instance attribute with callable type causes error #6910

unknownlighter opened this issue May 29, 2019 · 14 comments
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal

Comments

@unknownlighter
Copy link

unknownlighter commented May 29, 2019

test_dc.py:

from dataclasses import dataclass, fields


@dataclass
class Foo:
    value: str


fields(Foo)[0].default_factory
$ mypy test_dc.py 
test_dc.py:9: error: Attribute function "default_factory" with type "Callable[[], Any]" does not accept self argument

python version: 3.7.1
mypy version: 0.710+dev.95a04913df54d5b1ff78fcb9e7467b798491ec4b

@ilevkivskyi
Copy link
Member

It is not something specific to dataclasses and in fact may be tightly related to #708.

@ilevkivskyi ilevkivskyi added bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal labels Jun 1, 2019
@ilevkivskyi ilevkivskyi changed the title Access to attribute default_factory of dataclass field causes error Access to instance attribute with callable type causes error Jun 1, 2019
@ilevkivskyi
Copy link
Member

Here is a minimal repro:

from typing import Callable

class C:
    x: Callable[[], None]

x: C
x.x

@Strilanc
Copy link

Strilanc commented Nov 27, 2019

Ran into this independently. Confirmed the repro above still fails in mypy-0.740.

@frou
Copy link

frou commented May 10, 2020

I notice that this works:

class Thing(NamedTuple):
    f: Callable[[], int]

Thing(lambda: 123).f()

...and this doesn't:

@dataclass
class Thing:
    f: Callable[[], int]

Thing(lambda: 123).f()

# error: Attribute function "f" with type "Callable[[], int]" does not accept self argument  [misc]

@sinback
Copy link
Contributor

sinback commented May 11, 2020

also stumbled across this one when checking whether a dataclasses.default_factory was an instance of dataclasses' _MISSING_TYPE value

@pbabics
Copy link

pbabics commented May 18, 2020

Hello I have run into this when using enum.Enum:

import enum
class TestEnum2(enum.Enum):
	A = 1
	B = 2

for obj in TestEnum2:
	print(obj)

Result:

test.py:9: error: Attribute function "__iter__" with type "Callable[[], _EnumMetaType]" does not accept self argument
test.py:9: error: Attribute function "__next__" with type "Callable[[], TestEnum2]" does not accept self argument

Tested on:
mypy 0.770+dev.c90b405934cbf2142b8a575ddd89dbedf762106c (current master)
and also mypy 0.770 release

@JelleZijlstra
Copy link
Member

@pbabics I can't reproduce that error. Your line numbers don't match your code sample, so maybe you were typechecking a different file.

@pbabics
Copy link

pbabics commented May 18, 2020

Yes, you are right, I was testing this with our internal plugin that shadows standard Enum type checking, sorry for confusion

@staticdev
Copy link

@JelleZijlstra the error still happens in 0.780 and latest if you try on https://mypy-play.net/?mypy=latest&python=3.8


class C:
    x: Callable[[], None]

x: C
x.x

@snejus
Copy link

snejus commented Jul 6, 2020

Can confirm this is still happening with a dataclass on 0.782. setattr and getattr calls do not raise the error, so I'm using them as a workaround.

@9999years
Copy link

Well, if you don't want to wait for a fix in mypy, here's an extremely shitty workaround:

from typing import Callable, cast

class C:
    _x: Callable[[], None]

    def x(self) -> None:
        return cast(Callable[[], None], getattr(self, "_x"))()

getattr(self, "_x") prevents a "redundant cast to Callable[[], None]" warning, and the cast is required to avoid passing the self argument to self._x.

This type-checks, but it's far from pretty.

@dargueta
Copy link

dargueta commented Mar 12, 2021

Is there a way to work around this for dataclasses? I have the following minimal example:

import dataclasses


@dataclasses.dataclass
class A:
    x: float = dataclasses.field(default_factory=float)

fields = dataclasses.fields(A)
assert fields[0].default_factory is not dataclasses.MISSING

Running MyPy 0.812 with all default settings gives

test.py:9: error: Attribute function "default_factory" with type "Callable[[], _T]" does not accept self argument  [misc]
Found 1 error in 1 file (checked 1 source file)

I would really rather not add # type: ignore[misc] everywhere we do something like this, and @9999years' fix won't work for us because we're using desert in conjunction with it and that would change the setting's name.

@hauntsaninja
Copy link
Collaborator

The issue with default_factory was fixed by a typeshed change (#10750 , python/typeshed#5718). The underlying logic still needs fixing though, i.e., the min repro here still reproes #6910 (comment)

If you're running into this, a callback protocol is a good workaround (see python/typeshed#5718 for an example).

@ilevkivskyi
Copy link
Member

As @hauntsaninja mentioned original example was already fixed, the other example is also working now. Probably was fixed by the same PR that fixed #708

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal
Projects
None yet
Development

No branches or pull requests