Mypy: Property, classmethod and staticmethod aliases not supported

Created on 19 Apr 2019  路  4Comments  路  Source: python/mypy

MyPy does not support property aliases. Example:

$ cat a.py 
class A:
    @property
    def f(self) -> int:
        return 1
    g = f

x: int = A().f
y: int = A().g
$ mypy a.py 
a.py:8: error: Incompatible types in assignment (expression has type "Callable[[], int]", variable has type "int")
bug false-positive priority-0-high

Most helpful comment

Hm, this is more serious than I though then, raising priority to high.

All 4 comments

Thanks for reporting!

Yeah, unfortunately properties are known to be often problematic. They were implemented via some special-casing before a more general descriptor support was added.

The problem is not only with properties, but with static and class methods too.

$ cat b.py
class A:
    @classmethod
    def f(cls) -> int:
        return 1
    g = f

x: int = A().f()
y: int = A().g()
$ mypy b.py
b.py:8: error: Invalid self argument "A" to attribute function "g" with type "Callable[[Type[A]], int]"
$ cat c.py
class A:
    @staticmethod
    def f() -> int:
        return 1
    g = f

x: int = A().f()
y: int = A().g()
$ mypy c.py
c.py:8: error: Attribute function "g" with type "Callable[[], int]" does not accept self argument

Hm, this is more serious than I though then, raising priority to high.

And apparently with callable properties on a dataclass (though the same errors are returned for a class with callable properties, which do not receive a self passed in when they are called):

from typing import Callable
from dataclasses import dataclass

@dataclass
class Demo:
    call_this: Callable[[str], str]

def call(s: str) -> str:
    return s

demo = Demo(call_this=call)

demo.call_this("test")

When run through mypy, this gets:

demo.py:13: error: Invalid self argument "Demo" to attribute function "call_this" with type "Callable[[str], str]"
demo.py:13: error: Too many arguments
Was this page helpful?
0 / 5 - 0 ratings

Related issues

msullivan picture msullivan  路  26Comments

spkersten picture spkersten  路  40Comments

philthompson10 picture philthompson10  路  40Comments

gvanrossum picture gvanrossum  路  26Comments

JukkaL picture JukkaL  路  28Comments