Skip to content

inlined method from @type_check_only callable protocols #98

@jorenham

Description

@jorenham

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)

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions