ets-labs / python-dependency-injector

Dependency injection framework for Python
https://python-dependency-injector.ets-labs.org/
BSD 3-Clause "New" or "Revised" License
3.89k stars 304 forks source link

PyLint no-member errors #616

Open rkr87 opened 2 years ago

rkr87 commented 2 years ago

I'm in the process of adding DI to one of my existing projects and everything is working well except I do seem to be getting a few PyLint errors, as below;

Application Class

class Application(containers.DeclarativeContainer):

    wrappers = providers.Container(
        Wrappers
    )

    services = providers.Container(
        Services,
        igdb_wrapper=wrappers.igdb_wrapper,
        steam_wrapper=wrappers.steam_wrapper
    )

    repos = providers.Container(
        Repositories,
        igdb_service=services.igdb_service,
        steam_service=services.steam_service,
        local_service=services.local_service
    )

PyLint Errors

distiller/di/application.py:18:21: E1101: Instance of 'Container' has no 'igdb_wrapper' member (no-member)
distiller/di/application.py:19:22: E1101: Instance of 'Container' has no 'steam_wrapper' member (no-member)
distiller/di/application.py:24:21: E1101: Instance of 'Container' has no 'igdb_service' member (no-member)
distiller/di/application.py:25:22: E1101: Instance of 'Container' has no 'steam_service' member (no-member)
distiller/di/application.py:27:22: E1101: Instance of 'Container' has no 'local_service' member (no-member)

Now, those members are defined in my containers, I was tempted to just add an ignore flag to the relevant lines but thought I'd post here first to see if this is something I've setup incorrectly.

Below is my Services and Wrappers containers.

Services

class Services(containers.DeclarativeContainer):

    igdb_wrapper = providers.Dependency(IGDBWrapper)
    steam_wrapper = providers.Dependency(SteamWrapper)

    igdb_service = providers.Singleton(IGDBService, igdb_wrapper)
    steam_service = providers.Singleton(SteamService, steam_wrapper)
    local_service = providers.Singleton(LocalService)

Wrappers

class Wrappers(containers.DeclarativeContainer):

    igdb_wrapper = providers.Singleton(IGDBWrapper)
    steam_wrapper = providers.Singleton(SteamWrapper)

Thanks for any support.

toinbis commented 1 year ago

Same issue here. Will be adding c-extension-no-member to the list of disabled rules for now, unless anyone has other workaround.

zeshuaro commented 1 year ago

People may or may not like this, but I'm using string identifiers as a workaround: https://python-dependency-injector.ets-labs.org/wiring.html#string-identifiers

Nefendi commented 1 year ago

I have encountered a similar issue when using the container property (described in #590) and found a way around it using PyLint's generated-members option (imagine ApplicationContainer is a top-level container holding sub-containers, for example something like this: ApplicationContainer.user_container.user_management_service:

pyproject.toml

[tool.pylint.typecheck]
generated-members = [
    "ApplicationContainer.*.container",
]

Alternatively, you could do this:

pyproject.toml

[tool.pylint.typecheck]
generated-members = [
    ".*.container",
]

But this would ignore everything having a container attribute, which may not be what you want.