Skip to content

AbstractService, __init__ not called #81

Closed

Description

@djsliders

Hello,
In order to avoid declaring an init method inside all services, with often the same things (db, current_user) I use an abstract class that UserService (for example) inherits:

class AbstractService(ABC):

    def __init__(self,):
        self.config = config
        self.session = self.config.get_db()


class UsersService(AbstractService):
   def get_users():
      **self.session.query....**

But this doesn't work, as the self.session is never set. But if I create a init in UsersService and call the super.init(), then it works.

Is there a way to fix that ?

THank you for the help and the great framework.

Activity

  1. ItayTheDar commented on Oct 10, 2024

    @ItayTheDar
    Contributor

    Hi @djsliders
    Thanks for raising this issue. I believe that the problem of this abstraction is due to the implementation of the Injectable decorator. I need to take a second look at this. Maybe you can share the repo you are working on, so i can have a real use case to inspect?

  2. djsliders commented on Oct 11, 2024

    @djsliders
    Author

    My project is not public sorry. But you can create the default project generated, then add an abstract class on the service, with a init that does self.test = "bla"
    Then you will see in your App Service (in debug mode or with a print) that this self.test is never set.

  3. ItayTheDar commented on Jul 28, 2026

    @ItayTheDar
    Contributor

    Resolved by the dependency-injection rewrite in #125. @Injectable now preserves the provider class and the container constructs real instances, so a subclass without its own __init__ correctly executes its inherited base-class initializer.

    The reported inheritance pattern was reproduced against current main and the inherited state is initialized correctly. Closing as completed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions