Skip to content

Conversation

@seisman
Copy link
Member

@seisman seisman commented Jul 22, 2025

Fixes #4001.

@seisman seisman added this to the 0.17.0 milestone Jul 22, 2025
@seisman seisman added bug Something isn't working needs review This PR has higher priority and needs review. run/benchmark Trigger the benchmark workflow in PRs labels Jul 22, 2025
@seisman seisman requested a review from Copilot July 22, 2025 04:09
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes a bug in the is_nonstr_iter function where 0-dimensional NumPy arrays were incorrectly identified as iterable when they should be treated as non-iterable scalar values.

  • Updated the is_nonstr_iter function to properly handle 0-D arrays by checking for ndim == 0
  • Enhanced function documentation to clarify that 0-D arrays are excluded
  • Added docstring example demonstrating the correct behavior for 0-D arrays

return (
isinstance(value, Iterable)
and not isinstance(value, str)
and not (hasattr(value, "ndim") and value.ndim == 0)
Copy link

Copilot AI Jul 22, 2025

Choose a reason for hiding this comment

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

[nitpick] The condition hasattr(value, "ndim") and value.ndim == 0 could be made more explicit by importing numpy and using isinstance(value, np.ndarray) instead of hasattr(value, "ndim"). This would be more specific and avoid potential false positives from other objects that might have an ndim attribute.

Copilot uses AI. Check for mistakes.
@seisman seisman changed the title is_nonstr_iter: 0-D array is now recognized as non-iterable correctly is_nonstr_iter: 0-D array is now recognized as non-iterable Jul 22, 2025
@seisman seisman added final review call This PR requires final review and approval from a second reviewer and removed run/benchmark Trigger the benchmark workflow in PRs needs review This PR has higher priority and needs review. labels Jul 22, 2025
@seisman seisman requested a review from weiji14 July 23, 2025 06:37
return (
isinstance(value, Iterable)
and not isinstance(value, str)
and not (isinstance(value, np.ndarray) and value.ndim == 0)
Copy link
Member

Choose a reason for hiding this comment

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

Should we just check for the ndim attribute, since it part of the Python array API standard? Xref https://data-apis.org/array-api/2024.12/API_specification/generated/array_api.array.ndim.html#ndim

Suggested change
and not (isinstance(value, np.ndarray) and value.ndim == 0)
and not (hasattr(value, "ndim") and value.ndim == 0)

Copy link
Member

Choose a reason for hiding this comment

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

Oh, I see you did that first, and then copilot suggested otherwise 😅

Copy link
Member Author

Choose a reason for hiding this comment

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

This is exactly what I wrote in the intial version, then copilot made the suggestion at #4004 (comment).

Copy link
Member

Choose a reason for hiding this comment

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

I think ok to use hasattr to avoid an import, and also allow for duck typing.

Copy link
Member Author

Choose a reason for hiding this comment

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

Reverted in 001cbb9.

from pathlib import Path
from typing import Any, Literal

import numpy as np
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
import numpy as np

@seisman seisman removed the final review call This PR requires final review and approval from a second reviewer label Jul 23, 2025
@seisman seisman merged commit 25febab into main Jul 23, 2025
23 of 24 checks passed
@seisman seisman deleted the is_nonstr_iter/0Darray branch July 23, 2025 08:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

is_nonstr_iter recognize 0-D numpy array as iterable

4 participants