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
Property, classmethod and staticmethod aliases not supported #6700
$ 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")
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
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):
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
Currently, this issue forces rather awkward typing for @classmethods, as we can't even omit the typing inside the class definition. AFAIK, the current only workarounds are:
Hint the alias as typing.Callable[..., <return_type>] inside the class definition
Bind the alias outside of the class definition (no need to provide a type-hint)
I'm not sure what is supposed to be a convincing way to type the alias; some attempts below:
from __future__ importannotationsfromtypingimportCallablefromtypesimportMethodTypeclassA:
@classmethoddef_internal(cls) ->int:
return0public_1=_internal# (bad; see `value_1`)public_2: classmethod[int] =_internal# Incompatible types in assignment (expression has type "Callable[[Type[A]], int]", variable has type "classmethod[int]")public_3: Callable[[], int] =_internal# Incompatible types in assignment (expression has type "Callable[[Type[A]], int]", variable has type "Callable[[], int]")public_4: Callable[[type[A]], int] =_internal# (bad; see `value_4`)public_5: MethodType=_internal# Incompatible types in assignment (expression has type "Callable[[Type[A]], int]", variable has type "MethodType")public_6: Callable[..., int] =_internal# OKpublic_7=A._internal# OKvalue_1: int=A.public_1() # Too few argumentsvalue_2: int=A.public_2() # (bad; see `public_2`)value_3: int=A.public_3() # (bad; see `public_3`)value_4: int=A.public_4() # Too few argumentsvalue_5: int=A.public_5() # (bad; see `public_5`)value_6: int=A.public_6() # OKvalue_7: int=public_7() # OK
public_4 / value_4 is especially concerning, as it is contradictory; what mypy thinks is the correct type for the public_4 method causes a mypy error when actually called.
Activity
ilevkivskyi commentedon Apr 19, 2019
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.
serhiy-storchaka commentedon Apr 19, 2019
The problem is not only with properties, but with static and class methods too.
[-]Property aliases not supported[/-][+]Property, classmethod and staticmethod aliases not supported[/+]ilevkivskyi commentedon Apr 25, 2019
Hm, this is more serious than I though then, raising priority to high.
ex-nerd commentedon Dec 31, 2019
And apparently with callable properties on a
dataclass(though the same errors are returned for a class with callable properties, which do not receive aselfpassed in when they are called):When run through mypy, this gets:
bzoracler commentedon Feb 21, 2022
Currently, this issue forces rather awkward typing for
@classmethods, as we can't even omit the typing inside the class definition. AFAIK, the current only workarounds are:typing.Callable[..., <return_type>]inside the class definitionI'm not sure what is supposed to be a convincing way to type the alias; some attempts below:
public_4/value_4is especially concerning, as it is contradictory; whatmypythinks is the correct type for thepublic_4method causes amypyerror when actually called.erictraut commentedon Feb 21, 2022
Do you really require it to be an alias? Here's another workaround:
Workaround a false positive about indexing into refs
Workaround a false positive about indexing into refs
Workaround a false positive about indexing into refs
Workaround a false positive about indexing into refs
Workaround a false positive about indexing into refs
Workaround a false positive about indexing into refs
mvadari commentedon Jul 6, 2023
Are there any updates on this issue?
work around mypy issue python/mypy#6700.
Remove unnecessary paths from Sphinx directives. (#132)
00-kat commentedon Mar 2, 2025
I'm not sure if this is useful information, but I just want to note that the types (according to
reveal_type) seem to only be incorrect:• For properties: in instances but not the class itself.
• For classmethods: in the class but not instances.
• For staticmethods: nowhere, allegedly.
The last line still has a diagnostic of
Attribute function "life" with type "Callable[[], int]" does not accept self argument.Work around mypy bug with aliased properties.
Generalize class/static method and property alias support (python#19297)