Skip to content

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

Closed as not planned

Description

@refi64

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.

Activity

  1. gvanrossum commented on Oct 17, 2016

    @gvanrossum
    Member

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

  2. elazarg commented on Oct 17, 2016

    @elazarg
    Contributor

    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.

  3. added this to the milestone on Oct 20, 2016
  4. gvanrossum commented on Oct 20, 2016

    @gvanrossum
    Member

    Would be good to fix but probably complicated.

  5. gvanrossum commented on Jan 7, 2017

    @gvanrossum
    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__.

  6. refi64 commented on Mar 27, 2017

    @refi64
    ContributorAuthor

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

  7. gvanrossum commented on Mar 27, 2017

    @gvanrossum
    Member

    Ah, got it.

  8. removed this from the milestone on Mar 29, 2017
  9. erictraut commented on Apr 8, 2022

    @erictraut

    This doesn't appear to generate a runtime exception with newer versions of Python, so it's no longer a false negative in mypy.

  10. JelleZijlstra commented on Apr 8, 2022

    @JelleZijlstra
    Member

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

  11. hauntsaninja commented on Aug 27, 2023

    @hauntsaninja
    Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions