Skip to content

[MRG] allow child classes to override parents' tags - #14069

Closed
NicolasHug wants to merge 4 commits into
scikit-learn:masterfrom
NicolasHug:tags
Closed

NicolasHug wants to merge 4 commits into
scikit-learn:masterfrom
NicolasHug:tags

Conversation

@NicolasHug

@NicolasHug NicolasHug commented Jun 11, 2019 •

Copy link
Copy Markdown
Member

Fixes #14044

This PR relaxes the current constraint on the tags and implements the solution proposed in #14044 (comment)

That is:

  • any child is allowed to override its parent's tags
  • if D inherits from B and C, then B and C must agree on their tags (else there's no way to know the tags of D). This constraint is dismissed if D overrides the inconsistent tags.

waiting to see if CI goes green

Comment thread sklearn/base.py
more_tags = base_class._more_tags(self)

if '_more_tags' in self.__class__.__dict__:
# we cannot use getattr since it would resolve to the parent's

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

we could use getattr(type(self), '_more_tags'), right?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

no I think it would also resolve to the parents'

@amueller

Copy link
Copy Markdown
Member

Do we really want to overwrite the python inheritance mechanism just so we don't have to fix the way we inherit from mixins?

Comment thread sklearn/base.py
return

for direct_parent_class in c.__bases__:
_resolve_tags(direct_parent_class)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

can't you do get_tags maybe?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

yeah I suppose there are other ways to wrap it up

@NicolasHug

Copy link
Copy Markdown
Member Author

Do we really want to overwrite the python inheritance mechanism just so we don't have to fix the way we inherit from mixins

No idea yet. I'm just opening this so we have a concrete implementation of what it could look like.

@NicolasHug NicolasHug changed the title [WIP] allow child classes to override parents' tags [MRG] allow child classes to override parents' tags Jun 11, 2019
Comment thread sklearn/base.py

# filter-out tags that will be overridden in child class
more_tags = {key: val for (key, val) in more_tags.items()
if key not in overridden_tags.keys()}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

So if you don't do this does the consistency check fails?

Does that generalize to multiple inheritances: A (has some tags) > B (overwrite tags) > (overwrite tags) ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We need this because we allow tags that are overridden on the child class to be inconsistent in the parents classes

Comment thread sklearn/base.py
overridden_tags = {}

def _resolve_tags(c):
nonlocal collected_tags

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

can't you just pass collected_tags as a reference to _resolve_tags?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Sure

@rth

rth commented Jun 20, 2019

Copy link
Copy Markdown
Member

Do we really want to overwrite the python inheritance mechanism just so we don't have to fix the way we inherit from mixins?

It's a pretty impressive solution @NicolasHug , but I'm also not so convinced about reimplementing something that behaves like python inheritance but not quite. I think, just using python MRO and fixing the mixin ordering would be a simpler and easier to maintain approach in the long run.

@NicolasHug

Copy link
Copy Markdown
Member Author

Agreed, closing :) !

@NicolasHug NicolasHug closed this Jun 26, 2019
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.

Estimator tag overwriting and update in _get_tags

3 participants