Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate type annotations for clients' methods as TypedDicts #310

Closed
danielgafni opened this issue Aug 23, 2024 · 2 comments
Closed

Generate type annotations for clients' methods as TypedDicts #310

danielgafni opened this issue Aug 23, 2024 · 2 comments
Labels
🚀 enhancement New feature or request

Comments

@danielgafni
Copy link

danielgafni commented Aug 23, 2024

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: dagster-io/dagster#23568

@danielgafni danielgafni added the 🚀 enhancement New feature or request label Aug 23, 2024
@vemel
Copy link
Collaborator

vemel commented Aug 23, 2024

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
Copy link
Author

Oh, I missed it. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🚀 enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants