-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
hinting method decorators with protocols fails to remove the self attribute when binding #16200
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
Comments
Thanks, I think #15993 makes this behave as you expect |
after some digging i realized,that self attribute removal is indeed incorrect unfortunately the "correct" version spells out like class HasApplication(Protocol):
@property
def application(self) -> "Application": ...
HAS_APPLICATION = TypeVar("HAS_APPLICATION", bound=HasApplication)
class ApplicationEntityMethod(Generic[HAS_APPLICATION, P, T], Protocol):
def __call__(protocol_self, /, self: HasApplication, *k: P.kwargs , **kw: P.kwargs ) -> T:
...
@overload
def __get__(self, instance: HAS_APPLICATION, owner: type[HAS_APPLICATION]) -> Callable[P, T]: ...
@overload
def __get__(self, instance: None, owner: type[HAS_APPLICATION]) -> Self: ...
def __get__(self, instance: HAS_APPLICATION|None, owner: type[HasApplication]) -> Callable[P, T] | Self: ..
```
which is most boilerplate ridden and i still havent figrued how to make it match a function |
the working version spells like class HasApplication(Protocol):
@property
def application(self) -> Application: ...
R = TypeVar("R")
P = ParamSpec("P")
TM = TypeVar("TM", covariant=True)
HAS_APPLICATION = TypeVar("HAS_APPLICATION", bound=HasApplication, contravariant=True)
class ApplicationEntityMethod(Protocol, Generic[HAS_APPLICATION, P, TM]):
def __call__(protocol_self, self: HAS_APPLICATION, *k: P.args, **kw: P.kwargs) -> TM: ...
@overload
def __get__(
self, instance: HAS_APPLICATION, owner: type[HAS_APPLICATION]
) -> Callable[P, TM]: ...
@overload
def __get__(self, instance: None, owner: type[HAS_APPLICATION]) -> Self: ...
def __get__(
self, instance: HAS_APPLICATION | None, owner: type[HasApplication]
) -> Callable[P, TM] | Self: ... i wish there was a builtin version of that |
Bug Report
When hinting a decorator used on methods using
Callable
, the unbound method and bound method types differ by the first/self parameter as I expect. When I replace theCallable
with aProtocol
providing.__call__()
instead, the unbound and bound cases are the same.To Reproduce
https://mypy-play.net/?mypy=latest&python=3.11&gist=1046c1e56c21fd17cda1ecab5e9e131b
Expected Behavior
I don't know the details around 'modifying' protocols such as would be required here but I would expect that unbound and bound would not be the same.
Actual Behavior
Unbound and bound methods decorated with a decorator hinted with protocols have the same type.
Your Environment
master
mypy.ini
(and other config files): noneThe text was updated successfully, but these errors were encountered: