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

Wrong types for IAMClient.get_policy_version #212

Closed skeggse closed 1 year ago

skeggse commented 1 year ago

Describe the bug

The get_policy_version function returns a value v with type(v["Policy"]["Document"]) == dict, rather than the str claimed by the types provided by boto3-stubs[iam].

What's interesting about this issue is that the official documentation for boto3 indicates that the Document value ought to be a str, but it's not.

To Reproduce

import boto3

iam = boto3.client("iam")

document = iam.get_policy_version(
    PolicyArn="arn:aws:iam::aws:policy/SecurityAudit", VersionId="v39"
)["PolicyVersion"]["Document"]

print(document)
print(type(document))  # -> <class 'dict'>

Steps to reproduce the behavior:

  1. Install boto3-stubs[iam]
  2. Run pyright on the above code sample
  3. Observe:

Actual output

/tmp/test.py
  /tmp/test.py:7:38 - error: Expression of type "str | None" cannot be assigned to declared type "dict[str, Any] | None" (reportGeneralTypeIssues)
1 error, 0 warnings, 0 informations

Yet we demonstrated in the earlier script that this is a dict! And from testing, this is consistent for both AWS-managed policies and customer-managed policies.

Expected output

0 errors, 0 warnings, 0 informations

Additional context

macOS, pip

$ pip show boto3
Name: boto3
Version: 1.28.13
[...]
$ pip show boto3-stubs
Name: boto3-stubs
Version: 1.28.13
vemel commented 1 year ago

Thank you for the report!

Yes, looks like an issue in botocore shapes, because as I see boto3 does not override this type and returns original botocore response.

    "policyDocumentType":{
      "type":"string",
      "max":131072,
      "min":1,
      "pattern":"[\\u0009\\u000A\\u000D\\u0020-\\u00FF]+"
    },
vemel commented 1 year ago

I created a bug report for botocore: https://github.com/boto/botocore/issues/2992

I added a temporary fix and released mypy-boto3-iam 1.28.16. Please update and let me know if it works as it should.