Skip to content

Fix _as_numpy_array: use variable obj instead of string literal "obj"#14457

Closed
quick123-666 wants to merge 2 commits intopytest-dev:mainfrom
quick123-666:fix/hasattr-string-literal-14456
Closed

Fix _as_numpy_array: use variable obj instead of string literal "obj"#14457
quick123-666 wants to merge 2 commits intopytest-dev:mainfrom
quick123-666:fix/hasattr-string-literal-14456

Conversation

@quick123-666
Copy link
Copy Markdown

Describe your changes

Fixes a bug in _as_numpy_array where hasattr was called with the string literal "obj" instead of the variable obj for the __array_interface__ check.

# Before (bug):
elif hasattr(obj, "__array__") or hasattr("obj", "__array_interface__"):
# After (fix):
elif hasattr(obj, "__array__") or hasattr(obj, "__array_interface__"):

This meant any object implementing __array_interface__ without also implementing __array__ would not be recognized as a numpy-like array by _as_numpy_array, causing it to fall through to ApproxSequenceLike instead of ApproxNumpy. In practice this could affect users with custom array types or libraries that only implement __array_interface__.

GitHub Issue Link

Testing Plan

  • Added test_numpy_array_via_interface_protocol which creates a class implementing __array_interface__ without __array__ and verifies it works with approx()
  • Existing test_numpy_array_implicit_conversion and test_numpy_array_protocol continue to pass (they test __array__ path)

@RonnyPfannschmidt
Copy link
Copy Markdown
Member

closing due to ai abuse

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.

_as_numpy_array: hasattr uses string literal 'obj' instead of variable

2 participants