-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Comments
It is not something specific to dataclasses and in fact may be tightly related to #708. |
Here is a minimal repro: from typing import Callable
class C:
x: Callable[[], None]
x: C
x.x |
Ran into this independently. Confirmed the repro above still fails in |
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] |
also stumbled across this one when checking whether a dataclasses.default_factory was an instance of dataclasses' _MISSING_TYPE value |
Hello I have run into this when using import enum
class TestEnum2(enum.Enum):
A = 1
B = 2
for obj in TestEnum2:
print(obj) Result:
Tested on: |
@pbabics I can't reproduce that error. Your line numbers don't match your code sample, so maybe you were typechecking a different file. |
Yes, you are right, I was testing this with our internal plugin that shadows standard Enum type checking, sorry for confusion |
@JelleZijlstra the error still happens in 0.780 and latest if you try on https://mypy-play.net/?mypy=latest&python=3.8
|
Can confirm this is still happening with a |
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"))()
This type-checks, but it's far from pretty. |
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
I would really rather not add |
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). |
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 |
test_dc.py:
python version:
3.7.1
mypy version:
0.710+dev.95a04913df54d5b1ff78fcb9e7467b798491ec4b
The text was updated successfully, but these errors were encountered: