Skip to content
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

refactor: introduce parametrised nplike types #2795

Merged
merged 5 commits into from
Nov 3, 2023

Conversation

agoose77
Copy link
Collaborator

@agoose77 agoose77 commented Nov 3, 2023

The existing type hints for nplike were watered down to support a single ArrayLike protocol. This prevents us from imposing the constraint that a CuPy array can only be handled by a CuPy nplike.

This PR restores the original parametrised-nplike protocol, so that it's possible to declare an nplike as operating upon fixed array types. Unfortunately, mypy doesn't support higher-kinded-types, which are required to compose generic types. This means that we can't e.g. have

class NumpyArray(Generic[Backend]):
    @property
    def backend(self) -> Backend:
        ...

and so on. There are higher-kinded-type works related to mypy, e.g. https://sobolevn.me/2020/10/higher-kinded-types-in-python, but they don't look like a good fit for our needs; they have some drawbacks and impose constraints on the runtime logic.

So, this PR won't have huge sweeping impact, but it will allow us to explicitly type an nplike as a typetracer nplike, e.g.

def is_typetracer_nplike(nplike: NumpyLike) -> TypeGuard[TypeTracer]:
    return not nplike.known_data


if is_typetracer_nplike(nplike):
    print(nplike.zeros(4).report)

such that we can use fewer assertions.

Copy link

codecov bot commented Nov 3, 2023

Codecov Report

Merging #2795 (8049b9f) into main (c572295) will increase coverage by 0.01%.
The diff coverage is 87.21%.

Additional details and impacted files
Files Coverage Δ
src/awkward/_backends/backend.py 88.09% <100.00%> (ø)
src/awkward/_backends/cupy.py 66.66% <100.00%> (ø)
src/awkward/_backends/dispatch.py 92.85% <100.00%> (ø)
src/awkward/_backends/jax.py 100.00% <100.00%> (ø)
src/awkward/_backends/numpy.py 100.00% <100.00%> (ø)
src/awkward/_backends/typetracer.py 78.12% <100.00%> (ø)
src/awkward/_broadcasting.py 95.43% <100.00%> (ø)
src/awkward/_categorical.py 58.62% <100.00%> (ø)
src/awkward/_connect/cling.py 25.71% <100.00%> (ø)
src/awkward/_connect/jax/reducers.py 86.20% <100.00%> (ø)
... and 143 more

@agoose77
Copy link
Collaborator Author

agoose77 commented Nov 3, 2023

This is really just typing, so we should be safe to merge.

@agoose77 agoose77 merged commit 4ed2397 into main Nov 3, 2023
36 checks passed
@agoose77 agoose77 deleted the agoose77/refactor-type-hint-nplike-parametrised branch November 3, 2023 14:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant