Skip to content

DI LSP #144

Closed
Closed

Description

@felippemr

Hi folks I created a small POC of an LSP that parses container.py files and is able to serve "Go to implementation" requests.

Let me know your thoughts as this is something I could invest more time on if folks see some value. I did it using another DI lib but I could improve it so that it works with multiple providers. Is this something you would be interested in?

https://renard-engineering.ghost.io/guerrilla-coding-enabling-subcultures-to-thrive-through-custom-made-tooling/

Activity

  1. xelandernt commented on Jan 15, 2025

    @xelandernt
    Collaborator

    Hi @felippemr, thank you for sharing this!

    Currently, that_depends supports injection primarily through an IOC container.
    This means that in my average case, my code looks something like this:

    # mymodule.py
    @inject
    async def create_item(session: AsyncSession = Provide[Container.session]):
        ...

    Thus, I specify explicitly the provider that then injects the dependency.

    # container.py
    class Container(BaseContainer):
        config = providers.Singleton(Config)
        session = providers.ContextResource(create_async_session, config=config)

    And finally, the functions/classes that actually generate the dependencies:

    # deps.py
    from pydantic_settings import BaseSettings
    class Config(BaseSettings):
         host: str
         port: str
    
    async def create_async_session(config: Config):
        # creates an async sqlalchemy session 
        ....

    You can also see a pretty good example for this in the fastapi template.

    Thus, in my average case, getting to the implementation is just 2 "go to implementation" (for example Ctrl + B in Pycharm) away (without having multiple options to select from).

    Now that_depends technically supports resolution by name using Container.resolve() in which case your tool would be more useful.

    In general, in the current state of the project, I am not sure if a tool like you suggests would be too useful, but I can see it being more relevant as the project grows. Currently, the resolution framework of that_depends is just pretty simple and thus easy to follow.

    This is my personal opinion, it is likely someone else has a different one.

  2. converted this issue into a discussion #145 on Jan 15, 2025
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

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions