ITADN

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

#14456OpenEternalRights 创建于 2026-05-10
type: bugtopic: approxneeds backport
E
EternalRightscommented
Came across this while poking around the approx code: https://github.com/pytest-dev/pytest/blob/8344ded8f522ace904b054ee538442f2114b69d5/src/_pytest/python_api.py#L909 ```python 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.
2 条评论