-
Notifications
You must be signed in to change notification settings - Fork 46
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
__array_namespace__ type Hint #267
Comments
Unfortunately you can't return from __future__ import annotations
import types
import numpy.array_api
from typing import TYPE_CHECKING
if TYPE_CHECKING:
# Optionally subclass `types.ModuleType` to closer match runtime behavior
class _ArrayAPINameSpace(types.ModuleType):
asarray = numpy.array_api.asarray
arange = numpy.array_api.arange
empty = numpy.array_api.empty
# etc
else:
import numpy.array_api as _ArrayAPINameSpace
class Foo:
def __array_namespace__(self, /, *, api_version: None | str = None) -> _ArrayAPINameSpace:
return numpy.array_api |
Small update: unfortunately python/mypy#708 was never completelly fixed, so you'd have to return class Foo:
def __array_namespace__(self, /, *, api_version: None | str = None) -> type[_ArrayAPINameSpace]:
return numpy.array_api |
This issue is also somewhat related #229 |
Hi @BvB93, Are there any thoughts about making such a type/class part of the standard (It may be helpful for statically checking conformance)? |
My personal thoughts: the standard currently specifies that |
Thanks @BvB93 |
Recently the - def __array_namespace__(self, /, *, api_version: None | str = None) -> Any: ...
+ def __array_namespace__(self, /, *, api_version: None | str = None) -> types.ModuleType: ... |
That sounds like a good idea. |
So
It might be worthwhile, though I don't expect any major breakthroughs without a mypy plugin of some sort. While comparatively minor, the upstream |
Ah okay, got it now - Note that in the standard, the docs now say |
I feel that |
This is more precise, we are returning a module here. Type checkers will be able to use this info in the future - see data-apis/array-api#267
This is more precise, we are returning a module here. Type checkers will be able to use this info in the future - see data-apis/array-api#267
This is more precise, we are returning a module here. Type checkers will be able to use this info in the future - see data-apis/array-api#267
This is more precise, we are returning a module here. Type checkers will be able to use this info in the future - see data-apis/array-api#267 Original NumPy Commit: 781c9463673af478b2799549d70ac6d16e0555a5
This is more precise, we are returning a module here. Type checkers will be able to use this info in the future - see data-apis/array-api#267 Original NumPy Commit: 781c9463673af478b2799549d70ac6d16e0555a5
I hope this falls under the scope of this repo -
Is there any a way type-hint
__array_namespace__
? Is it even possible to do so, and it needs to be standardized, or maybe it's not possible with current python type hinting system?Motivation:
Suppose I want to write a function that will work in multiple conforming libraries, it will probably start with:
or, like in NEP-47:
In both cases, I want to be able to type check
xp
, and have auto-complete onxp
in the various IDEs. (At least some auto-complete engines relay on static typing e.g https://github.com/microsoft/pyright)Is this possible?
The text was updated successfully, but these errors were encountered: