Skip to content

fix mixin inheritance order, allow overwriting tags #14884

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

Merged
merged 7 commits into from
Sep 5, 2019

Conversation

amueller
Copy link
Member

@amueller amueller commented Sep 4, 2019

Incremental change from #14644, keeping the old logic manually looping over the MRO.
I would really prefer using super but this is a mostly backward-compatible change and seems less controversial.

Copy link
Member

@rth rth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have not checked that changes include all classes, but the included changes LGTM.

I would really prefer using super but this is a mostly backward-compatible change and seems less controversial.

OK, but I think in either case these changes with mixin reordering would be necessary so it's good to have this no?

cc @NicolasHug in case you have bandwidth for a second quick review :)

@amueller
Copy link
Member Author

amueller commented Sep 4, 2019

Yes these changes here are are prerequisite so I'm happy to merge them.

@NicolasHug
Copy link
Member

If we want to simulate super we should go down the MRO, not up.

from sklearn.base import BaseEstimator
class A(BaseEstimator):
    def _more_tags(self):
        return {'allow_nan': True,
                'multioutput': False}

class B(A):
    def _more_tags(self):
        return {'multioutput': True}

class C(B):
    def _more_tags(self):
        return {'allow_nan': False}

class D(C):
    # No _more_tags(), yet self._more_tags() exists and it's C's method
    pass

# "correctly" resolves to C's allow_nan only because C has _more_tags().
print(D()._get_tags()['allow_nan'])  # False, OK

# resolves to A's multioutput instead of B's since C's _more_tags does not
# specifies multioutput, and because we're going up the mro instead of down the
# mro.
print(D()._get_tags()['multioutput'])  # False, KO

@amueller
Copy link
Member Author

amueller commented Sep 4, 2019

I need to make sure I have the tests from the other PR, will fix this later.


diamond_tag_est = DiamondOverwriteTag()
with pytest.raises(TypeError, match="Inconsistent values for tag"):
diamond_tag_est._get_tags()
assert diamond_tag_est._get_tags()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol this is missing an ['allow_nan']

@amueller
Copy link
Member Author

amueller commented Sep 4, 2019

I'm confused why the current tests pass ...

@amueller
Copy link
Member Author

amueller commented Sep 4, 2019

tests were passing because the logic was to complicated and only wrong in complex cases.

Copy link
Member

@NicolasHug NicolasHug left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM when all goes green!

@rth rth merged commit 03ea20d into scikit-learn:master Sep 5, 2019
@rth
Copy link
Member

rth commented Sep 5, 2019

Thanks!

@thomasjpfan
Copy link
Member

In a follow up, we should document how the order of the inheritance matters now when using tags.

ivannz added a commit to ivannz/scikit-learn that referenced this pull request Oct 5, 2019
ivannz added a commit to ivannz/scikit-learn that referenced this pull request Oct 11, 2019
@jnothman jnothman mentioned this pull request Oct 28, 2019
8 tasks
ivannz added a commit to ivannz/scikit-learn that referenced this pull request Nov 1, 2019
artemmavrin added a commit to artemmavrin/logitboost that referenced this pull request Dec 7, 2019
ivannz added a commit to ivannz/scikit-learn that referenced this pull request Jan 10, 2020
ivannz added a commit to ivannz/scikit-learn that referenced this pull request Jan 30, 2020
ivannz added a commit to ivannz/scikit-learn that referenced this pull request Jan 30, 2020
ivannz added a commit to ivannz/scikit-learn that referenced this pull request Feb 27, 2020
ivannz added a commit to ivannz/scikit-learn that referenced this pull request Mar 12, 2020
ivannz added a commit to ivannz/scikit-learn that referenced this pull request Apr 17, 2020
ivannz added a commit to ivannz/scikit-learn that referenced this pull request May 3, 2020
ivannz added a commit to ivannz/scikit-learn that referenced this pull request May 30, 2020
ivannz added a commit to ivannz/scikit-learn that referenced this pull request Jul 5, 2020
ivannz added a commit to ivannz/scikit-learn that referenced this pull request Jul 5, 2020
ivannz added a commit to ivannz/scikit-learn that referenced this pull request Aug 5, 2020
ivannz added a commit to ivannz/scikit-learn that referenced this pull request Oct 15, 2020
ivannz added a commit to ivannz/scikit-learn that referenced this pull request Nov 28, 2020
ivannz added a commit to ivannz/scikit-learn that referenced this pull request Dec 12, 2020
ivannz added a commit to ivannz/scikit-learn that referenced this pull request Feb 25, 2021
ivannz added a commit to ivannz/scikit-learn that referenced this pull request Feb 25, 2021
ivannz added a commit to ivannz/scikit-learn that referenced this pull request Mar 27, 2021
ivannz added a commit to ivannz/scikit-learn that referenced this pull request Jun 15, 2021
ivannz added a commit to ivannz/scikit-learn that referenced this pull request Jul 23, 2021
ivannz added a commit to ivannz/scikit-learn that referenced this pull request Nov 10, 2021
ivannz added a commit to ivannz/scikit-learn that referenced this pull request May 15, 2022
ivannz added a commit to ivannz/scikit-learn that referenced this pull request Jun 14, 2022
ivannz added a commit to ivannz/scikit-learn that referenced this pull request Aug 29, 2022
ivannz added a commit to ivannz/scikit-learn that referenced this pull request Sep 5, 2022
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.

4 participants