Skip to content

Callable ClassVar is incorrectly treated as a method #11077

Closed

Description

@NeilGirdhar
from typing import Callable, MutableMapping, ClassVar

class C:
    x: ClassVar[Callable[[], MutableMapping]] = dict
    def f(self) -> MutableMapping:
        return self.x()  

c = C()
print(c.f())

Gives

a.py:6: error: Attribute function "x" with type "Callable[[], MutableMapping[Any, Any]]" does not accept self argument

Activity

  1. sobolevn commented on Sep 17, 2021

    @sobolevn
    Member

    This is a known problem / feature. The same happens without ClassVar as well:

    from typing import Callable, MutableMapping, ClassVar
    
    class C:
        x: Callable[[], MutableMapping] = dict
        def f(self) -> MutableMapping:
            # error: Attribute function "x" with type "Callable[[], MutableMapping[Any, Any]]" does not accept self argument
            return self.x()
    
    c = C()
    print(c.f())

    Related #9489

  2. thejcannon commented on Jan 31, 2022

    @thejcannon
    Contributor

    FWIW #9489 was fixed by #10548. But this appears to still be an issue

  3. AlexWaygood commented on Mar 26, 2022

    @AlexWaygood
    Member

    Closing as a duplicate of #6910 (which is not about ClassVars, but is the same but nonetheless)

  4. h4l commented on Jun 15, 2025

    @h4l
    Contributor

    Can we re-open this? It's not fixed and not a duplicate of #6910. The related issue for instance vars was fixed, but it remains a problem for ClassVar[Callable[...]].

  5. thejcannon commented on Jun 15, 2025

    @thejcannon
    Contributor

    Blast from the past!

    FWIW there's a recent ongoing Discourse discussion about this

    https://discuss.python.org/t/when-should-we-assume-callable-types-are-method-descriptors/92938

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

    bugmypy got something wrong

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions