Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] BaseObject._get_init_signature() if statement never True? #65

Closed
RNKuhns opened this issue Oct 26, 2022 · 2 comments
Closed

[BUG] BaseObject._get_init_signature() if statement never True? #65

RNKuhns opened this issue Oct 26, 2022 · 2 comments
Labels
bug Something isn't working code quality Code formatting and quality
Milestone

Comments

@RNKuhns
Copy link
Contributor

RNKuhns commented Oct 26, 2022

Describe the bug

BaseObject._get_init_signature() returns the init signature of a BaseObject. It starts off with code to shortcut the rest of the calculations if we notice that the returned init dunder is the same for the basic Python object. This never seems to be True since BaseObject currently inherits from scikit-learn's BaseEstimator.

A snapshot of the related code (from the beginning lines of the _get_init_signature class method:

init = getattr(cls.__init__, "deprecated_original", cls.__init__)
if init is object.__init__:
    # No explicit constructor to introspect
    return []

For example, the following indicates that BaseObject's will not return True for the if condition:

from skbase import BaseObject
FIXTURE_BASEOBJECT = BaseObject
init = getattr(
    FIXTURE_BASEOBJECT.__init__, "deprecated_original", FIXTURE_BASEOBJECT.__init__
)
print(init is object.__init__)  # False
print(init == object.__init__)  # False

However, this code will be useful once we remove the inherittance from scikit-learn's BaseEstimator:

class InitSignatureTester:

    @classmethod
    def _get_init_signature(cls):
        """Get class init sigature.

        Useful in parameter inspection.

        Returns
        -------
        List
            The inspected parameter objects (including defaults).

        Raises
        ------
        RuntimeError if cls has varargs in __init__.
        """
        # fetch the constructor or the original constructor before
        # deprecation wrapping if any
        init = getattr(cls.__init__, "deprecated_original", cls.__init__)
        if init is object.__init__:
            # No explicit constructor to introspect
            return []
        else:
            return "Rest code is run"


FIXTURE_BASEOBJECT = InitSignatureTester
init = getattr(
    FIXTURE_BASEOBJECT.__init__, "deprecated_original", FIXTURE_BASEOBJECT.__init__
)
print(init is object.__init__)  # True
print(init == object.__init__)  # True

Expected behavior

Conditional code should sometimes be True or it is not needed. In this case, we will need the code later once we finish refactoring so we don't inherit from BaseEstimator. Simiarly, the part of this code accounting for "deprecated_original" is related to how scikit-learn handles/handled deprecation. We may or may not want to retain that portion.

This issue is just to raise awareness in the meantime.

@RNKuhns RNKuhns added the bug Something isn't working label Oct 26, 2022
@fkiraly
Copy link
Contributor

fkiraly commented Nov 4, 2022

Yes, you are completely right!

Since BaseObject has an __init__, the condition will never be triggered.

We can remove both lines without danger.

@fkiraly
Copy link
Contributor

fkiraly commented May 2, 2023

addressed.

@fkiraly fkiraly closed this as completed May 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working code quality Code formatting and quality
Projects
None yet
Development

No branches or pull requests

2 participants