Skip to content

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

@EternalRights

Description

@EternalRights

Came across this while poking around the approx code:

elif hasattr(obj, "__array__") or hasattr("obj", "__array_interface__"):

elif hasattr(obj, "__array__") or hasattr("obj", "__array_interface__"):

Second hasattr uses the string "obj" instead of the variable obj. Means any object that implements __array_interface__ without __array__ won't be recognized as a numpy-like array by _as_numpy_array. So they fall through to ApproxSequenceLike instead of ApproxNumpy.

Not a huge impact day-to-day since most numpy users work with actual ndarray, but could affect users with custom array types or libraries that only implement __array_interface__. The fix is just removing the quotes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs backportapplied to PRs, indicates that it should be ported to the current bug-fix branchtopic: approxrelated to pytest.approx functiontype: bugproblem that needs to be addressed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions