-
Notifications
You must be signed in to change notification settings - Fork 3
Could we just get get_namespace typed for now? #13
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
Comments
Are you talking about implementing this as a runtime function, so that you could |
I just meant the type annotation for |
This project will be mostly So in other words; it's not possible to provide generic annotations for functions this way. The closest you could get is through a callable protocol, but I don't think that this would help for this |
Did you have a specific use-case or array-api library in mind? |
Sorry, I don't understand where
I imagined that you would ultimately do something like: def get_namespace(...) -> Namespace: ...
class Namespace(ModuleType):
def asarray(self, ...) -> Array: ...
def logaddexp(self, ...) -> Array: ...
# etc. That way, in user code: def softplus(x: Array, ...) -> Array:
xp = get_namespace(x)
return xp.logaddexp(x, ...) # This can be type-checked because xp has type Namespace.
Most functions that use the Array API will call |
Ah I see; the issue is that
|
Right. I honestly thought that's what this project would be providing. What kind of things are you planning on annotating here? And why wouldn't you also want to annotate the Also, where will the |
Protocols like |
I wish we could, but it's not possible within the Python typing system: You can only annotate functions from either dedicated stub packages, or from within the package itself. |
Got it. Perfect, that's what I figured.
Got it, that makes sense. So, it's probably unreasonable for me to ask the array-api-compat project to depend on this one to provide the annotation that says Just want to make sure I understand. |
Eventually; yes. If type-checkers can't understand it, then But for now, you could gradually introduce annotations, starting with e.g. the |
Anyway, let me know if there's anything with |
Perfect, thanks for explaining. I think we won't need partial since there are only a few functions exported by that project and we can probably just type them all. Just confirming: you think it would be unreasonable for array-api-compat to depend on array-api-typing? Wouldn't all users benefit from the annotations? Looks like they've started with some rudimentary protocols over there in Also, is it possible to do something like: if TYPE_CHECKING:
from jax import Array as JaxArray # Somehow this should not cause any type errors even if Jax isn't installed.
def is_jax_array(x: Array) -> IsType[JaxArray]: ...
else:
def is_jax_array(...) -> ...: ... |
I can imagine that doing so would allow for writing narrower annotations, so it might indeed be a good idea. |
Assuming that they are correct and complete, then yes. But in my experience, it's better to have no annotations, then to have bad ones. Because without them, type-checkers can usually figure out quite a bit by just looking a the implementation. But if there are annotations present, then the type-checkers are "forced" to use them, even if they are incorrect. |
Okay, thanks! It looks like what will probably end up happening is that they will switch |
Yea, sure 👌🏻 |
Great! feel free to ping me if you need me to look at anything :) |
It would make a big difference just to have something like this:
The text was updated successfully, but these errors were encountered: