Skip to content

Add type checks for provider arguments (ParamSpec) #314

Open
@rmk135

Description

@rmk135

This feature is to support type checks for provider arguments.

The technical possibility to implement it comes with Python 3.10 ParamSpec feature. Details: PEP 612: Parameter Specification Variables.

User suggestions:


For type hinting the arguments to a provider, was the decision, and perhaps the only option, to wait until Python 3.10 and the ParamSpec feature? Taking the below example, it would be useful to be able to use mypy to ensure all arguments and proper types are supplied to ApiClient and Service.

class Container(containers.DeclarativeContainer):

    config = providers.Configuration()

    api_client = providers.Singleton(
        ApiClient,
        api_key=config.api_key,
        timeout=config.timeout.as_int(),
    )

    service = providers.Factory(
        Service,
        api_client=api_client,
    )

Originally posted by @dbertouille in #268 (comment)


What I would really look forward to would be type safety when creating the object graph / the providers. I believe that should be possible once ParamSpec lands in Python. Then one could consider doing something like.

P = ParamSpec("P")
R = TypeVar("R")

def factory(cls: Callable[P, R]) -> Callable[P, R]

and use it like

a = providers.factory(A)(arg1, kwarg1=42)

Originally posted by @JarnoRFB in #268 (comment)

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions