omni-us / jsonargparse

Implement minimal boilerplate CLIs derived from type hints and parse from command line, config files and environment variables
https://jsonargparse.readthedocs.io
MIT License
326 stars 49 forks source link

Functions should implement callable protocols #616

Open MiguelMonteiro opened 2 weeks ago

MiguelMonteiro commented 2 weeks ago

🚀 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

?