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
Descriptors shouldn't be considered callable inside class definition #2267
The following is an error in Python but passes mypy's checks:
classX:
@staticmethoddeff(): passf()
Error:
Traceback (most recent call last):
File "cls.py", line 1, in <module>
class X:
File "cls.py", line 4, in X
f()
TypeError: 'staticmethod' object is not callable
I would expect mypy to mimic the runtime - treat the current block as if it was not inside a class definition, then send the definitions to a "metaclass" (hard coded, in the case to type) to handle. It will also yield a natural extension point for a plugin: implement your home-made "metaclass".
Does that make sense at all? As far as I understand, this isn't how it's done, currently - not even remotely similar.
I think you meant "decorators" in the title, not "descriptors", right? (A decorator uses @ syntax -- a descriptor is something that implements __get__ and (optionally) __set__ and __delete__.
Activity
gvanrossum commentedon Oct 17, 2016
Ugh. Mypy's lack of understanding of class vs. instance attributes strikes again. :-(
elazarg commentedon Oct 17, 2016
I would expect mypy to mimic the runtime - treat the current block as if it was not inside a class definition, then send the definitions to a "metaclass" (hard coded, in the case to
type) to handle. It will also yield a natural extension point for a plugin: implement your home-made "metaclass".Does that make sense at all? As far as I understand, this isn't how it's done, currently - not even remotely similar.
gvanrossum commentedon Oct 20, 2016
Would be good to fix but probably complicated.
gvanrossum commentedon Jan 7, 2017
I think you meant "decorators" in the title, not "descriptors", right? (A decorator uses
@syntax -- a descriptor is something that implements__get__and (optionally)__set__and__delete__.refi64 commentedon Mar 27, 2017
@gvanrossum Well, I meant descriptors because of the
staticmethoddescriptor.gvanrossum commentedon Mar 27, 2017
Ah, got it.
erictraut commentedon Apr 8, 2022
This doesn't appear to generate a runtime exception with newer versions of Python, so it's no longer a false negative in mypy.
JelleZijlstra commentedon Apr 8, 2022
The runtime change is from https://bugs.python.org/issue43682 which was included in Python 3.10.
hauntsaninja commentedon Aug 27, 2023
Closing as won't fix, though if anyone feels inspired to write a PR for 3.9 feel free to