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

Wrong type for aioboto3 resource context manager #325

Open
kpark-hrp opened this issue Nov 24, 2024 · 11 comments
Open

Wrong type for aioboto3 resource context manager #325

kpark-hrp opened this issue Nov 24, 2024 · 11 comments
Labels
🐞 bug Something isn't working 🎉 released Changes were included to the latest release 👋 response needed Awaiting response from a reporter

Comments

@kpark-hrp
Copy link

kpark-hrp commented Nov 24, 2024

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
@kpark-hrp kpark-hrp added the 🐞 bug Something isn't working label Nov 24, 2024
@vemel
Copy link
Collaborator

vemel commented Nov 25, 2024

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

@kpark-hrp kpark-hrp reopened this Nov 25, 2024
@kpark-hrp
Copy link
Author

Accidentally closed. Reopened.

@vemel
Copy link
Collaborator

vemel commented Nov 25, 2024

Fixed in main. I will check if everything works as expected and release it later today or tomorrow. If you have time - you can help me by building types-aioboto3 manually and verify if it works:

# clone git repo
git clone [email protected]:youtype/mypy_boto3_builder.git
cd mypy_boto3_builder

# this will create mypy_boto3_output/types_aioboto3_stubs_package with ready to install package
# you might need uv: https://docs.astral.sh/uv/getting-started/installation/
uv run python -m mypy_boto3_builder mypy_boto3_output --product aioboto3 --no-smart-version

# install types-aioboto3 to active Python env
python mypy_boto3_output/types_aioboto3_package/setup.py install

Then, you can run type checker on your project.

Thank you!

@kpark-hrp
Copy link
Author

kpark-hrp commented Nov 25, 2024

Thank you @vemel for quickly working on this! It works like a charm for aioboto3.Session().resource().

But one more thing, I think you need to do the same for aioboto3.Session().client() because it returns ClientCreatorContext[GenericClient]. For example,

s3_client_context: ClientCreatorContext[S3Client] = aioboto3.Session().client("s3")

Thus,

async with aioboto3.Session().client("s3") as s3_client:
    assert isinstance(s3_client, S3Client)

@vemel
Copy link
Collaborator

vemel commented Nov 26, 2024

Makes sense. This is not directly related to aioboto3, but it is what aiobotocore does. I will fix it as well. Thank you for testing my changes!

@kpark-hrp
Copy link
Author

kpark-hrp commented Nov 29, 2024

@vemel Thanks for the release, but I don't think a new types-aioboto3 has been released yet. When will a new version be released to PyPI?

@vemel
Copy link
Collaborator

vemel commented Nov 29, 2024

Hello! I just released

  • types-aioboto3-lite 13.2.0.post2
  • types-aioboto3 13.2.0.post2
  • types-aiobotocore-lite 2.15.2.post3
  • types-aiobotocore 2.15.2.post3

with the fix included. Could you please test if it works as expected and let me know?

@vemel vemel added 👋 response needed Awaiting response from a reporter 🎉 released Changes were included to the latest release labels Nov 29, 2024
@kpark-hrp
Copy link
Author

Just checked with the following newly published package. Works. Thank you!

types-aioboto3 13.2.0.post2
types-aiobotocore 2.15.2.post3

@kpark-hrp
Copy link
Author

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

@vemel
Copy link
Collaborator

vemel commented Nov 29, 2024

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

@kpark-hrp
Copy link
Author

kpark-hrp commented Dec 2, 2024

Looks great. I am assuming that this ResourceCreatorContext[] one is still outstanding?
#325 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐞 bug Something isn't working 🎉 released Changes were included to the latest release 👋 response needed Awaiting response from a reporter
Projects
None yet
Development

No branches or pull requests

2 participants