Skip to content

self type on property creates a spurious error #3223

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

Closed
JelleZijlstra opened this issue Apr 22, 2017 · 4 comments · Fixed by #4016
Closed

self type on property creates a spurious error #3223

JelleZijlstra opened this issue Apr 22, 2017 · 4 comments · Fixed by #4016

Comments

@JelleZijlstra
Copy link
Member

Running mypy master on this file:

from typing import TypeVar, List
_T = TypeVar('_T')

class X:
    @property
    def __members__(self: _T) -> List[_T]: ...

reveal_type(X().__members__)

gives

../bin/selftypeproperty.py:8: error: Revealed type is 'builtins.list[selftypeproperty.X*]'
../bin/selftypeproperty.py:8: error: Invalid method type

The revealed type is correct but the "Invalid method type" doesn't make sense.

@gvanrossum
Copy link
Member

Is it the same if you rename __members__ to foo?

@JelleZijlstra
Copy link
Member Author

Yes, doesn't make a difference.

@elazarg
Copy link
Contributor

elazarg commented Apr 23, 2017

I should look into it.

Note that adding a bound, i.e. _T = TypeVar('_T', bound='X') seems to fix this.

@JelleZijlstra
Copy link
Member Author

Thanks! FYI, the code where I found this had a Type over a bound typevar (python/typeshed#1195). It has _T = TypeVar('_T', bound=Enum) and the property is def __members__(self: Type[_T]) -> Mapping[str, _T]: ....

@elazarg elazarg mentioned this issue Sep 26, 2017
5 tasks
elazarg added a commit to elazarg/mypy that referenced this issue Sep 26, 2017
1. Reverse subtyping check for self argument
2. For classmethod, fallback on the argument instead of on the parameter
3. Mimic dispatch better: meet original_type with static class
4. Better error message
JukkaL pushed a commit that referenced this issue Oct 12, 2017
…take 2) (#4016)

Fix #3223. Minimal examples:

```
from typing import Callable
class X:
    f: Callable[[object], None]
X().f  # E: Invalid method type
```

```
class X:
    @Property
    def g(self: object): pass
X().g  # E: Invalid method type
```

The problem is that for non-methods the check performed on access 
to self is whether object <: X instead of the other way around.

1. Reverse subtyping check. This change alone fixes the issue described 
   above.
2. For classmethod, fall back on the argument instead of on the parameter.
3. Mimic dispatch better: meet original_type with static class. This handles 
   the case were instead of X() above there was something of type 
   Union[X, Y].
4. Better error message.

Take 2 of #3227, but without the reverse-operator part.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants