You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Access to instance attribute with callable type causes error #6910
changed the title [-]Access to attribute default_factory of dataclass field causes error[/-][+]Access to instance attribute with callable type causes error[/+]on Jun 1, 2019
@dataclassclassThing:
f: Callable[[], int]
Thing(lambda: 123).f()
# error: Attribute function "f" with type "Callable[[], int]" does not accept self argument [misc]
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
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.
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
Activity
ilevkivskyi commentedon Jun 1, 2019
It is not something specific to dataclasses and in fact may be tightly related to #708.
[-]Access to attribute default_factory of dataclass field causes error[/-][+]Access to instance attribute with callable type causes error[/+]ilevkivskyi commentedon Jun 1, 2019
Here is a minimal repro:
Strilanc commentedon Nov 27, 2019
Ran into this independently. Confirmed the repro above still fails in
mypy-0.740.frou commentedon May 10, 2020
I notice that this works:
...and this doesn't:
sinback commentedon May 11, 2020
also stumbled across this one when checking whether a dataclasses.default_factory was an instance of dataclasses' _MISSING_TYPE value
pbabics commentedon May 18, 2020
Hello I have run into this when using
enum.Enum:Result:
Tested on:
mypy 0.770+dev.c90b405934cbf2142b8a575ddd89dbedf762106c(current master)and also
mypy 0.770releaseJelleZijlstra commentedon May 18, 2020
@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 commentedon 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 commentedon Jun 30, 2020
@JelleZijlstra the error still happens in 0.780 and latest if you try on https://mypy-play.net/?mypy=latest&python=3.8
snejus commentedon Jul 6, 2020
Can confirm this is still happening with a
dataclasson0.782.setattrandgetattrcalls do not raise the error, so I'm using them as a workaround.9999years commentedon Nov 12, 2020
Well, if you don't want to wait for a fix in mypy, here's an extremely shitty workaround:
getattr(self, "_x")prevents a "redundant cast toCallable[[], None]" warning, and the cast is required to avoid passing theselfargument toself._x.This type-checks, but it's far from pretty.
dargueta commentedon Mar 12, 2021
Is there a way to work around this for dataclasses? I have the following minimal example:
Running MyPy 0.812 with all default settings gives
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 usingdesertin conjunction with it and that would change the setting's name.hauntsaninja commentedon Mar 28, 2022
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 commentedon Aug 23, 2022
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