Protocols for Modules?
Does anyone know if there has been any existing discussion on providing an equivalent of `typing.Protocol` but for modules instead of classes? For example, a use case coming up in the numeric world is a function like `np.get_array_module(x)` which should return a a module that "behaves like" the base `numpy` module. See NEP 0037 for the details: https://numpy.org/neps/nep-0037-array-module.html#how-to-use-get-array-modul... Currently, this type of function isn't statically typed, but we are working to create a standard array API which would be some version of a slimmed down NumPy API. If we did provide a function like `get_array_module()` to return some module which implements, I am unclear how to type it currently. Should we type it as a protocol? Are modules considered instances of a protocol? Ideally, I could imagine wanting to write a module like this: ``` from typing_extensions import Protocol def arange(x: int) -> Array: ... class Array(Protocol): def __add__(self, Union[Array, int]) -> Array: ... ``` And then ideally we would be able to annotate a function like `get_array_module()` to say "You must return a module or object that has the `arange` function that returns an object that follow the Array protocol."
participants (3)
-
Anthony Sottile
-
Brett Cannon
-
Saul Shanabrook