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."
I believe this mypy issue is related (but perhaps not the same?) https://github.com/python/mypy/issues/5018 "Allow using modules as subtypes of protocols" Anthony On Mon, Aug 10, 2020 at 4:13 PM Saul Shanabrook <s.shanabrook@gmail.com> wrote:
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." _______________________________________________ Typing-sig mailing list -- typing-sig@python.org To unsubscribe send an email to typing-sig-leave@python.org https://mail.python.org/mailman3/lists/typing-sig.python.org/ Member address: asottile@umich.edu
Thanks for the link. That covers one side of it, the other would be using a module full of type definition as a protocol/type itself. Sort of the inverse of that issue.
I don't think you need to use a concrete module as a protocol itself. A protocol is just a description of the shape of an object. Modules are just objects as well. So you could define a protocol using typing.Protocol as a class as normal and then still have it apply to module objects. IOW the fact that you are trying to return a module shouldn't play into this; Python is just objects all the way down. 😉 On Mon, Aug 10, 2020 at 6:18 PM Saul Shanabrook <s.shanabrook@gmail.com> wrote:
Thanks for the link. That covers one side of it, the other would be using a module full of type definition as a protocol/type itself. Sort of the inverse of that issue. _______________________________________________ Typing-sig mailing list -- typing-sig@python.org To unsubscribe send an email to typing-sig-leave@python.org https://mail.python.org/mailman3/lists/typing-sig.python.org/ Member address: brett@python.org
participants (3)
-
Anthony Sottile
-
Brett Cannon
-
Saul Shanabrook