-
Notifications
You must be signed in to change notification settings - Fork 2
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
Polish function/method signatures #16
Comments
I did some rough work toward this in 0ad4e74 and b62798d , but there are a few issues:
|
@jorenham, I saw your post at numpy/numpy#27588 (comment), and thought you might be interested here. I'd appreciate your help with the signatures and typing, if you'd like to take a look! Currently, I've made a sloppy attempt to pass the documentation and signatures of the underlying library through. If you can see ways of doing this better, LMK. |
At first glance, it looks like typing this seems pretty doable, even without https://github.com/data-apis/array-api-typing. What you could do, for instance, is describe |
Anyway, feel free to ping me if there's something specific I need to review or otherwise look at. |
Thanks. Can I get a little more advice before I have real code? I'm still a bit lost. Currently, for function documentation, I pass along the documentation of the underyling array library function with some modifications. I thought I'd want to do something similar with the type hints. At the most basic level, I thought that might look like getting the type hint information with But that wouldn't be complete, because those type hints would suggest that functions only accept arrays of the underlying library, whereas I'd also want the function to accept an (Does this make any sense?) |
Static type-checkers won't understand that, as they don't "run" your code, similar to
Yup; there's also that
You got that right. You could see a protocol as the static-typing analogue of duck-typing at runtime.
Aye! To make the ducktyping/protocol comparison a bit more concrete, consider this function def quack_the_duck(🦆):
return 🦆.quack() We don't even have a We can use a from typing import Protocol
class QuacksLikeADuck[QuackT](Protocol):
def quack(self) -> QuackT: ... And now we can use it to annotate the function: def quack_the_duck[QuackT](🦆: QuacksLikeDuck[T]) -> QuackT:
return 🦆.quack() This was, static duckcheckers will know what's allowed as a class Duck:
def quack(self) -> str:
return "Quack erat demonstranduck."
class Capybara:
def chill(self) -> None: ...
quack_the_duck(Duck()) # inferred as `str`
quack_the_duck(Capybara()) # type checker error |
Thanks for that description of Protocol. I especially like the 🦆 : )
Ok. I suppose I should have noticed that. You wrote
At a very high-level description, how did you have in mind that would be done? E.g. "manually from scratch" or "copy, paste, + search and replace" (from somewhere)? If I were to write a Protocol for MArray, then where would the typing information for the library functions that accept And even that seems like it shouldn't be necessary to do from scratch, since ISTM that the Protocol would be essentially the same as for any array API compatible array. Is that wrong - is it necessary for all Array API libraries to write type information from scratch? Or is that where https://github.com/data-apis/array-api-typing will come in to help somehow? I can't tell what it does - the repo looks like an empty shell to me.
It feels like there is something so obvious that you didn't mention, but I am completely missing it! |
That'd be one of those "manually from scratch" kinda deals.
Are there different functions per library, or are you assuming a common set of functions with same signature, like in the array-api? And are we talking about methods or the other kind here?
That protocol should be the common denominator of the masked arrays of all libraries. That way, if you have some masked array instance,
That's deferred at the moment, at least for me, as I don't have the time to work on that. But after scipy-stubs and numtype (a full rework of numpy's stubs), that'll be my next target. I hope that I'll be able to get it up and running this year (but there's always the unknown unknowns).
If that protocol is part of your public API, then all that a lib has to do to verify that their masked array matches your spec, is to try and assign an instance of it to you protocol. So that if |
The idea is that this API implements exactly and only the array API standard. There are a few minor tweaks which don't break compatibility, like an added The difference is that instead of working with arrays, it works with |
Some function/methods have correct signatures, but others don't follow the requirements of the array API standard. Let's clean this up.
The text was updated successfully, but these errors were encountered: