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
544 stars 36 forks source link

The query paginator returns unexpected Type QueryOutputTableTypeDef #217

Closed Weber-Tr closed 1 year ago

Weber-Tr commented 1 year ago

Describe the bug If you try to use the query paginator returned items always fails by the type validation. The page response Type is QueryOutputTableTypeDef and refers to TableAttributeValueTypeDef for all item attribute values. TableAttributeValueTypeDef is a Union which does not expect data types. I would that the query page item is of type QueryOutputTypeDef instead.

To Reproduce First case:

import boto3
from mypy_boto3_dynamodb.paginator import QueryPaginator
from mypy_boto3_dynamodb.type_defs import AttributeValueTypeDef

hash_key = '123'

dynamodb_client = boto3.client('dynamodb')
paginator: QueryPaginator = dynamodb_client.get_paginator('query')

pages = paginator.paginate(
    TableName='demo_table',
    KeyConditionExpression='HASH_KEY = :hash_key',
    ExpressionAttributeValues={
        ':hash_key': {
            'S': hash_key
        }
    }
)

for page in pages:
    for item in page['Items']:
        item_hash: AttributeValueTypeDef = item['HASH_KEY']
        key = item_hash['S']

Second case:

import boto3

hash_key = '123'

dynamodb_client = boto3.client('dynamodb')
paginator = dynamodb_client.get_paginator('query')

pages = paginator.paginate(
    TableName='demo_table',
    KeyConditionExpression='HASH_KEY = :hash_key',
    ExpressionAttributeValues={
        ':hash_key': {
            'S': hash_key
        }
    }
)

for page in pages:
    for item in page['Items']:
        key = item['HASH_KEY']['S']

Actual output

First case:
(error: Incompatible types in assignment (expression has type "bytes | bytearray | str | int | Decimal | bool | set[int] | set[Decimal] | set[str] | set[bytes] | set[bytearray] | Sequence[Any] | Mapping[str, Any] | None", variable has type "AttributeValueTypeDef")  [assignment])

Second case:
error: No overload variant of "__getitem__" of "bytes" matches argument type "str"  [call-overload]

Expected output

No error

Additional context MacOS, boto3 1.28.20

vemel commented 1 year ago

Thank you for the report! Yes, boto3 replaces AttributeValue handling not only for Table resource and Client, but also for paginators. Will be fixed soon.

vemel commented 1 year ago

Fix in the latest release. Please update to mypy-boto3-dynamodb 1.28.27 and let me know if it works as it should.