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

GetKeyPolicyResponseTypeDef Policy should be `str` not `Dict[str, Any]` #223

Closed sodul closed 1 year ago

sodul commented 1 year ago

Describe the bug After upgrading to from boto3 1.28.25 to 1.28.37, and the matching stubs we get an error on the types returned by KMS get_key_policy().

The type is GetKeyPolicyResponseTypeDef with a 'Policykey typed asDict[str, Any]`.

GetKeyPolicyResponseTypeDef = TypedDict(
    "GetKeyPolicyResponseTypeDef",
    {
        "Policy": Dict[str, Any],
        "ResponseMetadata": ResponseMetadataTypeDef,
    },
)

While the policy is json encoded and that content is a dict, the type is actually str.

I tested it and confirmed the return type:

>>> kms.get_key_policy(KeyId='ad13a324-4bf6-4f7c-877a-5b0d5a32ce36', PolicyName='default')
{'Policy': '{\n  "Version" : "2012-10-17",\n  "Statement" : [

To Reproduce Steps to reproduce the behavior:

  1. Install boto3-stubs[kms]
  2. Run mypy on the following code sample:
import boto3
session = boto3.Session()
kms = session.client('kms')
policy_id = 'ad13a324-4bf6-4f7c-877a-5b0d5a32ce36'
policy: str = kms.get_key_policy(KeyId=policy_id, PolicyName='default')

Actual output

incompatible type "dict[str, Any]"; expected "str"

Expected output

no error

Additional context OS: macOS 13.5.1, Python 3.11.5. https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/kms/client/get_key_policy.html#

sodul commented 1 year ago

Further details.

With pip install mypy-boto3-kms==1.28.16 (previous) we have str:

GetKeyPolicyResponseTypeDef = TypedDict(
    "GetKeyPolicyResponseTypeDef",
    {
        "Policy": str,
        "ResponseMetadata": ResponseMetadataTypeDef,
    },
)

With pip install mypy-boto3-kms==1.28.36 (latest) we have:

GetKeyPolicyResponseTypeDef = TypedDict(
    "GetKeyPolicyResponseTypeDef",
    {
        "Policy": Dict[str, Any],
        "ResponseMetadata": ResponseMetadataTypeDef,
    },
)

I hope this helps.

vemel commented 1 year ago

Thank you for the report.

Well, that means I have to properly check when botocore parses strings as JSON and returns a parsed object. Until then, String shapes should always return strings. I wIll fix it today.

vemel commented 1 year ago

Released fixed packages:

Please update and let me know if it works as it should.

sodul commented 1 year ago

Thank you for the fix!