Skip to content

Investigate whether first-class support for parameterized generics is worth it #122

Description

@maldoinc

Today, generic dependencies are handled by registering concrete types such as directly with the container.

from sqlalchemy.orm import Session

from wireup import injectable

# Define base type
class Repository[T]:
    model: type[T]

    def __init__(self, session: Session) -> None:
        self.session = session

    def get(self, id: int) -> T | None:
        return self.session.get(self.model, id)


# Define concrete types for injection
@injectable
class UserRepository(Repository[User]):
    model = User


@injectable
class BlogPostRepository(Repository[BlogPost]):
    model = BlogPost

Investigate whether allowing the following registration is worth it.
One way this could work is by having a special parameter in the ctor.

class Repository[T]:
    def __init__(self, session: Session, model: GenericType[type[T]]) -> None:
        self.session = session
        self.model = model

    def get(self, id: int) -> T | None:
        return self.session.get(self.model, id)

That would make it possible to inject types like Repository[User] or Repository[BlogPost] without defining a subclass for each one.

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions