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

Large unnamed inline Union types impacts developer ergonomics #196

Closed perlow closed 1 year ago

perlow commented 1 year ago

An example of this is LastEvaluatedKey of ScanOutputTableTypeDef. It has the inline-type

  Dict[
            str,
            Union[
                bytes,
                bytearray,
                str,
                int,
                Decimal,
                bool,
                Set[int],
                Set[Decimal],
                Set[str],
                Set[bytes],
                Set[bytearray],
                Sequence[Any],
                Mapping[str, Any],
                None,
            ],
        ]

This means that if code calling table.scan needs to return this to its caller, it needs to replicate that very large type definition. Ideally, this would be a named type like LastEvaluatedKeyTypeDef which could then be referenced in application code.

vemel commented 1 year ago

Hello!

Thanks for the feature request. Yes, I was thinking about it. As you see, for now, I only have Literals and TypedDicts. It is a good idea to also have named type annotations for large types.

There are several issues with this approach:

I will think about it. Also, feel free to share your ideas.

vemel commented 1 year ago

Four months passed, and now we have named unions in boto3-stubs>=1.28.16.post1. You can find them in type_defs.py files. In your case, you can use:

import boto3
from mypy_boto3_dynamodb.type_defs import TableAttributeValueTypeDef

def scan() -> list[dict[str, TableAttributeValueTypeDef]]:
   table = boto3.resource("mypy_boto3_dynamodb").Table("my_table")
   result = table.scan()
   return result["Items"]

Please let me know if you have any ideas about the implementation, or if you face any issues. Thank you for the feature request again!