Skip to content

TYP: Type annotations overhaul, episode 2 #288

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

Merged
merged 24 commits into from
Apr 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
b9b0206
TYP: annotate `_internal.get_xp` (and curse at `ParamSpec` for being …
jorenham Mar 22, 2025
6a17007
TYP: fix (or ignore) typing errors in `common._helpers` (and curse at…
jorenham Mar 22, 2025
3b134b0
TYP: fix typing errors in `common._fft`
jorenham Mar 22, 2025
344ac1e
TYP: fix typing errors in `common._aliases`
jorenham Mar 22, 2025
cbec5f3
TYP: fix typing errors in `common._linalg`
jorenham Mar 22, 2025
dc79e3f
TYP: fix/ignore typing errors in `numpy.__init__`
jorenham Mar 22, 2025
9643256
TYP: fix typing errors in `numpy._typing`
jorenham Mar 22, 2025
18870dc
TYP: fix typing errors in `numpy._aliases`
jorenham Mar 22, 2025
1fb929b
TYP: fix typing errors in `numpy._info`
jorenham Mar 22, 2025
014385f
TYP: fix typing errors in `numpy._fft`
jorenham Mar 22, 2025
ec72825
TYP: it's a bad idea to import `TypeAlias` from `typing` on `python<3…
jorenham Mar 22, 2025
ccd9bc6
TYP: it's also a bad idea to import `TypeGuard` from `typing` on `pyt…
jorenham Mar 22, 2025
b8c7883
TYP: don't scare the prehistoric `dtype` from numpy 1.21
jorenham Mar 22, 2025
ef066d1
TYP: dust off the DeLorean
jorenham Mar 22, 2025
a522dbc
TYP: figure out how to drive a DeLorean
jorenham Mar 22, 2025
bca9c0c
TYP: apply review suggestions
jorenham Apr 15, 2025
0dd925f
TYP: sprinkle some `TypeAlias`es and `Final`s around
jorenham Apr 15, 2025
953d7c0
TYP: `__dir__`
jorenham Apr 15, 2025
c66b750
TYP: fix typing errors in `numpy.linalg`
jorenham Apr 15, 2025
9acba46
TYP: add a `common._typing.Capabilities` typed dict type
jorenham Apr 15, 2025
ba0b4e5
TYP: `__array_namespace_info__` helper types
jorenham Apr 15, 2025
4278dfb
TYP: `dask.array` typing fixes and improvements
jorenham Apr 15, 2025
4f6ef6d
STY: give the `=` some breathing room
jorenham Apr 17, 2025
d758d6f
STY: apply review suggestions
jorenham Apr 17, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions array_api_compat/_internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@
Internal helpers
"""

from collections.abc import Callable
from functools import wraps
from inspect import signature
from types import ModuleType
from typing import TypeVar

def get_xp(xp):
_T = TypeVar("_T")


def get_xp(xp: ModuleType) -> Callable[[Callable[..., _T]], Callable[..., _T]]:
"""
Decorator to automatically replace xp with the corresponding array module.

Expand All @@ -22,14 +28,14 @@ def func(x, /, xp, kwarg=None):

"""

def inner(f):
def inner(f: Callable[..., _T], /) -> Callable[..., _T]:
@wraps(f)
def wrapped_f(*args, **kwargs):
def wrapped_f(*args: object, **kwargs: object) -> object:
return f(*args, xp=xp, **kwargs)

sig = signature(f)
new_sig = sig.replace(
parameters=[sig.parameters[i] for i in sig.parameters if i != "xp"]
parameters=[par for i, par in sig.parameters.items() if i != "xp"]
)

if wrapped_f.__doc__ is None:
Expand All @@ -40,7 +46,14 @@ def wrapped_f(*args, **kwargs):
specification for more details.

"""
wrapped_f.__signature__ = new_sig
return wrapped_f
wrapped_f.__signature__ = new_sig # pyright: ignore[reportAttributeAccessIssue]
return wrapped_f # pyright: ignore[reportReturnType]

return inner


__all__ = ["get_xp"]


def __dir__() -> list[str]:
return __all__
2 changes: 1 addition & 1 deletion array_api_compat/common/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from ._helpers import * # noqa: F403
from ._helpers import * # noqa: F403
Loading
Loading