Skip to content

Functions should implement callable protocols #616

@MiguelMonteiro

Description

@MiguelMonteiro

🚀 Feature request

Functions that correctly implement a callable protocol should be considered as implementing said protocol when checking for subtypes / protocol implementations. First mentioned here.

Motivation

Currently implements_protocol does not check if functions implement the protocol and returns False when the value is a function. Since these are valid implementations of the protocol implements_protocol should return true such that the parser can identify them as valid subtypes / implementations.

Pitch

Consider the following callable interface and two valid implementations:

class CallableInterface(Protocol):
    def __call__(self, items: List[float]) -> List[float]: ...

class ImplementsCallableInterface1:
    def __init__(self, batch_size: int):
        self.batch_size = batch_size

 def __call__(self, items: List[float]) -> List[float]:
        return items

def implements_callable_interface2(items: List[float]) -> List[float]:
    return items

The current behavior of def implements_protocol(value, protocol) -> bool: is:

implements_protocol(ImplementsCallableInterface1, CallableInterface) => True
implements_protocol(implements_callable_interface2, CallableInterface) => False

It should be:

implements_protocol(ImplementsCallableInterface1, CallableInterface) => True
implements_protocol(implements_callable_interface2, CallableInterface) => True

Alternatives

?

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions