mypy_boto3_builder: Wrong type for `aioboto3` resource context manager

Describe the bug

Currently the aioboto3 type generator creates the following incorrect type hint (snippet from aioboto3-stubs/session.pyi).

@overload
def resource(
    self,
    service_name: Literal["dynamodb"],
    region_name: Optional[str] = ...,
    api_version: Optional[str] = ...,
    use_ssl: Optional[bool] = ...,
    verify: Union[bool, str, None] = ...,
    endpoint_url: Optional[str] = ...,
    aws_access_key_id: Optional[str] = ...,
    aws_secret_access_key: Optional[str] = ...,
    aws_session_token: Optional[str] = ...,
    config: Optional[Config] = ...,
) -> DynamoDBServiceResource: ...

To Reproduce

The proper aioboto3 usage is the following where dynamodb: DynamoDBServiceResource

async with aioboto3.Session().resource("dynamodb") as dynamodb:
    table = await dynamodb.Table(TABLE_NAME)

The current type hint generator makes it seem like following is also possible because type checker will think that dynamodb: DynamoDBServiceResource. But it is NOT possible since the .resource() returns ResourceCreatorContext type that needs to go through await __aenter__() before it becomes DynamoDBServiceResource type. Thus, this will cause a run-time error.

dynamodb = faioboto3.Session().resource("dynamodb")
table = await dynamodb.Table(TABLE_NAME)

Solution

The correct type hint would be something along the lines of the following. DynamoDBServiceResource itself should not be an async context manager.

@overload
def resource(
    self,
    service_name: Literal["dynamodb"],
    region_name: Optional[str] = ...,
    api_version: Optional[str] = ...,
    use_ssl: Optional[bool] = ...,
    verify: Union[bool, str, None] = ...,
    endpoint_url: Optional[str] = ...,
    aws_access_key_id: Optional[str] = ...,
    aws_secret_access_key: Optional[str] = ...,
    aws_session_token: Optional[str] = ...,
    config: Optional[Config] = ...,
) -> ResourceCreatorContext[DynamoDBServiceResource]: ...

Additional context

types-aioboto3[aioboto3,dynamodb,s3]==13.2.0

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 4
  • Comments: 18

Most upvoted comments

Just tested. The issue seems to be fixed now. Thanks!

Good call! Fixed release script, published *.tar.gz packages for types-aioboto3 and types-aiobotocore.

Unrelated to this issue, I noticed that the source .tar.gz is no longer being published. Not an issue for me but wanted to bring up to make sure that this is intentional. image

Thank you for the report! I will test the proposed change and include it to the next release.