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

inlined method from @type_check_only callable protocols #98

Open
jorenham opened this issue Oct 3, 2024 · 0 comments
Open

inlined method from @type_check_only callable protocols #98

jorenham opened this issue Oct 3, 2024 · 0 comments

Comments

@jorenham
Copy link
Owner

jorenham commented Oct 3, 2024

this builds upon #97

additionally allow Protocols with only __call__ implemented and @type_check_only as the only class decorator

if the protocol is generic, then all params must be bound (or have a default), because these will
be substituted into the inlined method

it will be inlined into some class as method, when accessed as e.g.

from typing import Protocol, type_check_only

__all__ = ["A"]

@type_check_only
class _IOpMethod[OtherT](Protocol):
    def __call__[SelfT](self, lhs: SelfT, rhs: OtherT | SelfT, /) -> SelfT: ...

class A[T]:
    __iadd__: _IOpMethod[T]
    __isub__: _IOpMethod[T]

which results in something like

from typing import Protocol
from typing_extensions import Self

__all__ = ["A"]

class A[T]:
    def __iadd__(self, rhs: T | Self, /) -> Self: ...

note that the lhs param in _IOpMethod.__call__ was renamed to self, and was stripped of its annotation (following the rules in #97)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant