youtype / mypy_boto3_builder

Type annotations builder for boto3 compatible with VSCode, PyCharm, Emacs, Sublime Text, pyright and mypy.
https://youtype.github.io/mypy_boto3_builder/
MIT License
551 stars 38 forks source link

Generate type annotations for clients' methods as `TypedDict`s #310

Closed danielgafni closed 2 months ago

danielgafni commented 2 months ago

Describe your idea It would be great if this project also generated TypedDicts for function arguments.

Why it's useful: library authors often want to allow the end users to pass arbitrary **kwargs to boto3 clients. It's possible to annotate **kwargs with TypedDicts, for example:

from typing import TypedDict
from typing_extensions import Unpack

import boto3

class RunTaskParamsTypeDef(TypedDict):
    launchType: NotRequired[Literal["EC2", "FARGATE", "EXTERNAL"]]
    cluster: NotRequired[str]
    group: NotRequired[str]
    networkConfiguration: NotRequired[Dict[str, Any]]
    overrides: NotRequired[Dict[str, Any]]
    placementConstraints: NotRequired[List[Dict[str, Any]]]
    placementStrategy: NotRequired[List[Dict[str, Any]]]
    platformVersion: NotRequired[str]
    referenceId: NotRequired[str]
    startedBy: NotRequired[str]
    tags: NotRequired[List[Dict[str, str]]]
    clientToken: NotRequired[str]
    volumeConfigurations: NotRequired[List[Dict[str, Any]]]
    capacityProviderStrategy: NotRequired[List[Dict[str, Any]]]
    enableEcsManagedTags: NotRequired[bool]
    enableExecuteCommand: NotRequired[bool]
    propagateTags: NotRequired[Literal["TASK_DEFINITION", "SERVICE", "NONE"]]
    count: int

def my_ecs_run_task(**kwargs: Unpack[RunTaskParamsTypeDef]):
    boto3.client("ecs").run_task(**kwargs)

These TypedDicts could be reused in the client stubs. Example:

class ECSClient(BaseClient):
    def run_task(self, *, **kwargs: Unpack[RunTaskParamsTypeDef]) -> RunTaskResponseTypeDef:
        ...

Code sample

from mypy_boto3_ecs.type_defs import RunTaskParamsTypeDef

Additional context IRL: example: https://github.com/dagster-io/dagster/pull/23568

vemel commented 2 months ago

Hello! This is already implemented.

You can find a TypedDict generated for **kwargs in documentation: https://youtype.github.io/boto3_stubs_docs/mypy_boto3_ecs/client/#run_task

As you see, you can do

from mypy_boto3_esc.type_defs import RunTaskRequestRequestTypeDef 

kwargs: RunTaskRequestRequestTypeDef = {
    "taskDefinition": "my_task",
}

ecs_client.run_task(**kwargs)

You can find documentation for RunTaskRequestRequestTypeDef here: RunTaskRequestRequestTypeDef

Let me know if this helps. And feel free to share your ideas regarding this feature to make it easier to use.

danielgafni commented 2 months ago

Oh, I missed it. Thanks!