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

Equinox seems to break cooperative multiple inheritance #832

Closed
NeilGirdhar opened this issue Sep 6, 2024 · 4 comments · Fixed by #834
Closed

Equinox seems to break cooperative multiple inheritance #832

NeilGirdhar opened this issue Sep 6, 2024 · 4 comments · Fixed by #834

Comments

@NeilGirdhar
Copy link
Contributor

NeilGirdhar commented Sep 6, 2024

from typing import override

import equinox as eqx
import jax.numpy as jnp
from jax import Array


class AcceptsStreams(eqx.Module):
    def __post_init__(self) -> None:
        if hasattr(super(), '__post_init__'):
            super().__post_init__()  # pyright: ignore


class InputNode(AcceptsStreams):
    total_value_error: Array = eqx.field(init=False)

    @override
    def __post_init__(self) -> None:
        print("PI InputNode")  # noqa: T201
        super().__post_init__()
        self.total_value_error = jnp.zeros(())


class DeductionSource(AcceptsStreams):
    pass


class DistillationBase(DeductionSource, AcceptsStreams):
    @override
    def __post_init__(self) -> None:
        print("PI DistillationBase")  # noqa: T201
        super().__post_init__()


class InputPerception(DistillationBase, InputNode):
    pass


print(", ".join(x.__qualname__ for x in InputPerception.__mro__))  # noqa: T201
# InputPerception, DistillationBase, DeductionSource, InputNode, AcceptsStreams, Module, object
InputPerception()
# ValueError: The following fields were not initialised during __init__: {'total_value_error'}

For some reason, InputNode.__post_init__ is never called. If DeductionSource is removed, then it is.

@patrick-kidger
Copy link
Owner

Thankyou for the report! This should be fixed in #834.

FWIW I generally recommend against co-operative multiple inheritance (I never see this done correctly except in code I own entirely), so these days I generally prefer the abstract/final pattern instead. IMO this is more readable + is certainly more robust. This is probably contributing to not having bumped into this edge-case before.

@NeilGirdhar
Copy link
Contributor Author

NeilGirdhar commented Sep 6, 2024

MO this is more readable + is certainly more robust.

That would mean that every concrete class would need to duplicate the initialization code and init-variables of its superclasses. Besides the repeated code, it also breaks separation of concerns.

Personally, I think it's better to just use inheritance properly. It's a shame that Python inheritance confuses people.

I generally recommend against co-operative multiple inheritance (I never see this done correctly except in code I own entirely

It's pretty straightforward in Equinox. You define __post_init__ whenever you need it, and make sure that it calls super and forwards all keyword arguments.

All eqx.Modules can have:

  • ordinary variables,
  • keyword-only InitVars, and
  • init_only variables, which must be set in __post_init__.

This is probably contributing to not having bumped into this edge-case before.

Yeah, I realize that inheritance is unpopular 😄

@patrick-kidger
Copy link
Owner

(For the record --

the initialization code and init-variables of its superclasses

I would argue that this shouldn't exist at all: an abstract class cannot be initialized by definition.)

@NeilGirdhar
Copy link
Contributor Author

NeilGirdhar commented Sep 6, 2024

I would argue that this shouldn't exist at all: an abstract class cannot be initialized by definition.)

Right, you're basically pushing for what some people call "interface inheritance" only, which is a paradigm that many people consider to be very safe in any programming language.

Nothing wrong with that, but I'd like to have some classes that have data members too. Consider an optional base class that tracks errors. It overrides certain methods and saves its results in its member variables. If I did things with ABCs only, I'd have to add a bunch of code in every concrete class that inherits from it (at the very least to write to and initialize those member variables).

@patrick-kidger patrick-kidger mentioned this issue Sep 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants