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
trial.py: In member "__init__" of class "A":
trial.py, line 7: Cannot assign to a method
trial.py, line 7: Invalid method type
trial.py, line 7: Incompatible types in assignment (expression has type Callable[[int], int], variable has type Callable[[], int])
while I'd expect this to work.
Note that there is a second error in the reporting of the type of the variable.
Hmm.. actually not sure what's the right thing to do here. Based on a quick experiment, the desired behavior seems to depend on what kind of a callable g is in your original example.
/tmp/assign_callable.py:8: error: Cannot assign to a method
/tmp/assign_callable.py:8: error: Incompatible types in assignment (expression has type Callable[[], None], variable has type Callable[..., None])
Having Callable[..., None] or Callable[[], None] do not resolve it, having Callable[..., Any] remove the incompatible types line.
It's possible these two errors are different issues. The second looks like it may be a case of the Void/NoneTyp distinction (recently eliminated in our --strict-optional future: #1975). Would you try reproducing it on master and with --strict-optional?
The first error looks like the same thing as the original issue.
@tharvik, would you also link more specifically to the bit in ctypes that refers to this? It's good to know about that use case, and it'd be interesting to see it more concretely.
Seems that I'm unable to reproduce 'Incompatible types in assignment' on master with or without --strict-optional.
@gnprice in ctypes, when declaring a foreign function, one can redefine errcheck, which should be a callable. It allows processing of the return value (and raising on failure).
To be clear, the example still produces "Cannot assign to a method".
The problem seems to be that mypy disallows assigning to a method (which I
think is reasonable) but thinks that a class variable declared with a
Callable type (and initialized with a lambda) is also a method. The latter
is wrong, I think.
Reacted by Paul Garner, S.Lott, Jasha Sommer-Simpson and Michael Lefkowitz
There was issue #5062, which was closed as a duplicate in favor of this issue, which is shown as fixed. However, I am currently using Python 3.11.4 with mypy 1.4.1 and still getting the errors with ConfigParser, described in this comment - #5062 (comment). Is this expected, or duplicate issue was closed by mistake, and the error with ConfigParser is not fixed?
Reacted by Nick Sweeting, Alexandre Detiste, Russ Allbery, Domenico, dil-anovosz, bstrand-cai and cj-wong
Activity
spkersten commentedon Jun 6, 2015
The mismatching types is probably because mypy thinks
fis a method and therefore strips off the first argument (self).JukkaL commentedon Jun 7, 2015
Hmm.. actually not sure what's the right thing to do here. Based on a quick experiment, the desired behavior seems to depend on what kind of a callable
gis in your original example.Consider this:
But now look at this:
JukkaL commentedon Jun 7, 2015
Taking off priority label because the fix doesn't seem obvious.
spkersten commentedon Jun 7, 2015
Before reporting it I also thought for a while that I could fix it quickly 😄.
tharvik commentedon Jul 29, 2016
Is there any update? The issue is still present. For some real world example, it is used in the ctypes module (in the examples).
gives
Having
Callable[..., None]orCallable[[], None]do not resolve it, havingCallable[..., Any]remove the incompatible types line.gvanrossum commentedon Jul 29, 2016
I'm sorry, I don't have any further updates.
gnprice commentedon Aug 4, 2016
Thanks for bumping this with the new repro!
It's possible these two errors are different issues. The second looks like it may be a case of the
Void/NoneTypdistinction (recently eliminated in our--strict-optionalfuture: #1975). Would you try reproducing it on master and with--strict-optional?The first error looks like the same thing as the original issue.
gnprice commentedon Aug 4, 2016
@tharvik, would you also link more specifically to the bit in ctypes that refers to this? It's good to know about that use case, and it'd be interesting to see it more concretely.
tharvik commentedon Aug 5, 2016
Seems that I'm unable to reproduce 'Incompatible types in assignment' on master with or without
--strict-optional.@gnprice in ctypes, when declaring a foreign function, one can redefine
errcheck, which should be a callable. It allows processing of the return value (and raising on failure).Example from the documentation
This way, we can load a library, in which, every attribute is a foreign function, then assign to some an
errcheckfor freeing some pointer or other.For another example, here goes libLAS, where the library is loaded then it is assigned some checks.
gvanrossum commentedon Aug 5, 2016
62 remaining items
Fix callable instance variable support (python#10548)
Fix callable instance variable support (take 2) (#13400)
type hint application wrapper monkeypatch
type hint application wrapper monkeypatch
type hint application wrapper monkeypatch
type hint application wrapper monkeypatch
type hint application wrapper monkeypatch
Ignore mypy errors due to method assignment during patching
Ignore mypy errors due to method assignment during patching
kerimovkhikmet commentedon Aug 8, 2023
There was issue #5062, which was closed as a duplicate in favor of this issue, which is shown as fixed. However, I am currently using Python 3.11.4 with mypy 1.4.1 and still getting the errors with ConfigParser, described in this comment - #5062 (comment). Is this expected, or duplicate issue was closed by mistake, and the error with ConfigParser is not fixed?
mypy wrongly checks type for attribute function assigment - updating …
mypy wrongly checks type for attribute function assigment - updating …
mypy wrongly checks type for attribute function assigment - updating …
Correct type hint at Context.function using Protocol