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

fix: use duck-typing check in place of isinstance(..., np.ndarray) #402

Merged
Merged
Changes from all commits
Commits
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
17 changes: 13 additions & 4 deletions src/dask_awkward/lib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,10 +534,20 @@ def new_record_object(dsk: HighLevelGraph, name: str, *, meta: Any) -> Record:
return Record(dsk, name, meta)


def _is_numpy_or_cupy_like(arr: Any) -> bool:
return (
hasattr(arr, "ndim")
and hasattr(arr, "shape")
and isinstance(arr.shape, tuple)
and hasattr(arr, "dtype")
)


def _finalize_array(results: Sequence[Any]) -> Any:
# special cases for length 1 results
if len(results) == 1:
if isinstance(results[0], (int, ak.Array, np.ndarray)):
np_like = _is_numpy_or_cupy_like(results[0])
if isinstance(results[0], (int, ak.Array)) or np_like: # type: ignore[unreachable]
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea why mypy is complaining that the second half of this or is unreachable. It's absolutely reachable! results[0] is an Any.

return results[0]

# a sequence of arrays that need to be concatenated.
Expand All @@ -546,13 +556,12 @@ def _finalize_array(results: Sequence[Any]) -> Any:

# a sequence of scalars that are stored as np.ndarray(N) where N
# is a number (i.e. shapeless numpy array)
elif any(isinstance(r, np.ndarray) for r in results) and any(
elif any(_is_numpy_or_cupy_like(r) for r in results) and any(
r.shape == () for r in results
):
return ak.Array(list(results))

# sometimes we just check the length of partitions so all results
# will be integers, just make an array out of that.
# in awkward < 2.5 we can get integers instead of np.array scalars
elif isinstance(results, (tuple, list)) and all(
isinstance(r, (int, np.integer)) for r in results
):
Expand Down
Loading