Thanks to visit codestin.com
Credit goes to github.com

Skip to content

_IffHasAttrDescriptor fails if attribute exists, but cannot be coerced to a truthy value #21144

@Zahlii

Description

@Zahlii

Describe the bug

See also alkaline-ml/pmdarima#454.

If an attribute is e.g. a numpy array (instead of a value that can be coerced to true/false), the check fails even though it (as per my understanding) shouldn't

16:50:56  >       return getattr(delegate, self.attribute_name) or True
16:50:56  E       ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

The following patch fixes the issue:

image

Steps/Code to Reproduce

import numpy as np

from sklearn.base import BaseEstimator
from sklearn.utils.metaestimators import if_delegate_has_method


class Result:
    xyz = np.random.random((2, ))


class Model(BaseEstimator):
    def __init__(self):
        self.arima_res_ = Result()

    @if_delegate_has_method('arima_res_')
    def xyz(self):
        return self.arima_res_.xyz


m = Model()
print(m.xyz())

Expected Results

No error is thrown.

Actual Results

Error is thrown (see description)

Versions

System:
python: 3.7.9 (default, Sep 22 2021, 17:11:30) [Clang 12.0.0 (clang-1200.0.26.2)]
executable: /Users/*/.pyenv/versions/-venv/bin/python
machine: Darwin-19.6.0-x86_64-i386-64bit

Python dependencies:
pip: 21.2.4
setuptools: 57.5.0
sklearn: 1.0
numpy: 1.19.5
scipy: 1.7.1
Cython: 0.29.24
pandas: 1.2.5
matplotlib: 3.4.3
joblib: 1.0.1
threadpoolctl: 2.2.0

Built with OpenMP: True

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions