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

Functions should implement callable protocols #616

Open
MiguelMonteiro opened this issue Nov 6, 2024 · 0 comments
Open

Functions should implement callable protocols #616

MiguelMonteiro opened this issue Nov 6, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@MiguelMonteiro
Copy link
Contributor

🚀 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

?

@MiguelMonteiro MiguelMonteiro added the enhancement New feature or request label Nov 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant