Skip to content
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

Property, classmethod and staticmethod aliases not supported #6700

Open
serhiy-storchaka opened this issue Apr 19, 2019 · 7 comments
Open

Property, classmethod and staticmethod aliases not supported #6700

serhiy-storchaka opened this issue Apr 19, 2019 · 7 comments
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-0-high topic-descriptors Properties, class vs. instance attributes

Comments

@serhiy-storchaka
Copy link
Member

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")
@ilevkivskyi
Copy link
Member

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.

@ilevkivskyi ilevkivskyi added bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal labels Apr 19, 2019
@serhiy-storchaka
Copy link
Member Author

serhiy-storchaka commented Apr 19, 2019

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

@serhiy-storchaka serhiy-storchaka changed the title Property aliases not supported Property, classmethod and staticmethod aliases not supported Apr 19, 2019
@ilevkivskyi
Copy link
Member

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

@ex-nerd
Copy link

ex-nerd commented 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 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

@bzoracler
Copy link
Contributor

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__ import annotations

from typing import Callable
from types import MethodType


class A:
    @classmethod
    def _internal(cls) -> int:
        return 0

    public_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        # OK

public_7 = A._internal                              # OK


value_1: int = A.public_1()  # Too few arguments
value_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 arguments
value_5: int = A.public_5()  # (bad; see `public_5`)
value_6: int = A.public_6()  # OK
value_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.

@erictraut
Copy link

Do you really require it to be an alias? Here's another workaround:

class A:
    @classmethod
    def public_8(cls) -> int:
        return cls._internal()

@AlexWaygood AlexWaygood added the topic-descriptors Properties, class vs. instance attributes label Mar 27, 2022
DMRobertson pushed a commit to matrix-org/synapse that referenced this issue Apr 1, 2022
```
scripts-dev/release.py:495: error: Unsupported right operand type for in ("Callable[[], IterableList[Reference]]")  [operator]
scripts-dev/release.py:496: error: Value of type "Callable[[], IterableList[Reference]]" is not indexable  [index]
```

`refs` is an alias the the property `references`. Mypy gets confused by
the alias, see python/mypy#6700
DMRobertson pushed a commit to matrix-org/synapse that referenced this issue Apr 8, 2022
```
scripts-dev/release.py:495: error: Unsupported right operand type for in ("Callable[[], IterableList[Reference]]")  [operator]
scripts-dev/release.py:496: error: Value of type "Callable[[], IterableList[Reference]]" is not indexable  [index]
```

`refs` is an alias the the property `references`. Mypy gets confused by
the alias, see python/mypy#6700
DMRobertson pushed a commit to matrix-org/synapse that referenced this issue Apr 8, 2022
```
scripts-dev/release.py:495: error: Unsupported right operand type for in ("Callable[[], IterableList[Reference]]")  [operator]
scripts-dev/release.py:496: error: Value of type "Callable[[], IterableList[Reference]]" is not indexable  [index]
```

`refs` is an alias the the property `references`. Mypy gets confused by
the alias, see python/mypy#6700
DMRobertson pushed a commit to matrix-org/synapse that referenced this issue Apr 8, 2022
```
scripts-dev/release.py:495: error: Unsupported right operand type for in ("Callable[[], IterableList[Reference]]")  [operator]
scripts-dev/release.py:496: error: Value of type "Callable[[], IterableList[Reference]]" is not indexable  [index]
```

`refs` is an alias the the property `references`. Mypy gets confused by
the alias, see python/mypy#6700
DMRobertson pushed a commit to matrix-org/synapse that referenced this issue Apr 20, 2022
```
scripts-dev/release.py:495: error: Unsupported right operand type for in ("Callable[[], IterableList[Reference]]")  [operator]
scripts-dev/release.py:496: error: Value of type "Callable[[], IterableList[Reference]]" is not indexable  [index]
```

`refs` is an alias the the property `references`. Mypy gets confused by
the alias, see python/mypy#6700
DMRobertson pushed a commit to matrix-org/synapse that referenced this issue Apr 20, 2022
```
scripts-dev/release.py:495: error: Unsupported right operand type for in ("Callable[[], IterableList[Reference]]")  [operator]
scripts-dev/release.py:496: error: Value of type "Callable[[], IterableList[Reference]]" is not indexable  [index]
```

`refs` is an alias the the property `references`. Mypy gets confused by
the alias, see python/mypy#6700
@mvadari
Copy link

mvadari commented Jul 6, 2023

Are there any updates on this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-0-high topic-descriptors Properties, class vs. instance attributes
Projects
None yet
Development

No branches or pull requests

7 participants