Skip to content

Descriptors shouldn't be considered callable inside class definition #2267

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

Closed
refi64 opened this issue Oct 17, 2016 · 9 comments
Closed

Descriptors shouldn't be considered callable inside class definition #2267

refi64 opened this issue Oct 17, 2016 · 9 comments
Labels
bug mypy got something wrong priority-2-low topic-descriptors Properties, class vs. instance attributes topic-runtime-semantics mypy doesn't model runtime semantics correctly

Comments

@refi64
Copy link
Contributor

refi64 commented Oct 17, 2016

The following is an error in Python but passes mypy's checks:

class X:
    @staticmethod
    def f(): pass
    f()

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

Same thing goes for classmethod.

@gvanrossum
Copy link
Member

Ugh. Mypy's lack of understanding of class vs. instance attributes strikes again. :-(

@elazarg
Copy link
Contributor

elazarg commented 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 gvanrossum added this to the Future milestone Oct 20, 2016
@gnprice gnprice added the bug mypy got something wrong label Oct 20, 2016
@gvanrossum
Copy link
Member

Would be good to fix but probably complicated.

@gvanrossum
Copy link
Member

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
Copy link
Contributor Author

refi64 commented Mar 27, 2017

@gvanrossum Well, I meant descriptors because of the staticmethod descriptor.

@gvanrossum
Copy link
Member

Ah, got it.

@gvanrossum gvanrossum removed this from the Future milestone Mar 29, 2017
@AlexWaygood AlexWaygood added topic-descriptors Properties, class vs. instance attributes topic-runtime-semantics mypy doesn't model runtime semantics correctly labels Mar 27, 2022
@erictraut
Copy link

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
Copy link
Member

The runtime change is from https://bugs.python.org/issue43682 which was included in Python 3.10.

@hauntsaninja
Copy link
Collaborator

Closing as won't fix, though if anyone feels inspired to write a PR for 3.9 feel free to

@hauntsaninja hauntsaninja closed this as not planned Won't fix, can't repro, duplicate, stale Aug 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong priority-2-low topic-descriptors Properties, class vs. instance attributes topic-runtime-semantics mypy doesn't model runtime semantics correctly
Projects
None yet
Development

No branches or pull requests

8 participants