mypy_boto3_builder icon indicating copy to clipboard operation
mypy_boto3_builder copied to clipboard

`AttributeNames` argument of `.receive_messages()` has incorrect type.

Open ngnpope opened this issue 3 years ago • 7 comments

Describe the bug

The AttributeNames argument of .receive_messages() is currently typed as Sequence[QueueAttributeNameType] but it seems that Sequence[MessageSystemAttributeNameType | QueueAttributeNameType] would be a better fit. Looking at the documentation we can see that the "Request Syntax" section shows only all of the values listed in QueueAttributeNameType, but below under the "Parameters" section it lists all of the values of MessageSystemAttributeNameType plus only All and SqsManagedSseEnabled from QueueAttributeNameType.

Looking through to the API Reference it seems to list all of these attributes under "AttributeName.N", but in two distinct blocks 🤷🏻

To Reproduce Steps to reproduce the behavior:

  1. Install boto3-stubs[sqs]==1.24.38
  2. Run mypy on the following code sample
import boto3
from mypy_boto3_sqs import SQSClient

client: SQSClient = boto3.client("sqs")
client.receive_message(
    QueueUrl="...",
    AttributeNames=["ApproximateReceiveCount", "SentTimestamp"],
)

Actual output

bug.py:7: error: List item 0 has incompatible type "Literal['ApproximateReceiveCount']"; expected
"Literal['All', 'ApproximateNumberOfMessages', 'ApproximateNumberOfMessagesDelayed', 'ApproximateNumberOfMessagesNotVisible', 'ContentBasedDeduplication', 'CreatedTimestamp', 'DeduplicationScope', 'DelaySeconds', 'FifoQueue', 'FifoThroughputLimit', 'KmsDataKeyReusePeriodSeconds', 'KmsMasterKeyId', 'LastModifiedTimestamp', 'MaximumMessageSize', 'MessageRetentionPeriod', 'Policy', 'QueueArn', 'ReceiveMessageWaitTimeSeconds', 'RedriveAllowPolicy', 'RedrivePolicy', 'SqsManagedSseEnabled', 'VisibilityTimeout']"
 [list-item]
        AttributeNames=["ApproximateReceiveCount", "SentTimestamp"],
                        ^
bug.py:7: error: List item 1 has incompatible type "Literal['SentTimestamp']"; expected
"Literal['All', 'ApproximateNumberOfMessages', 'ApproximateNumberOfMessagesDelayed', 'ApproximateNumberOfMessagesNotVisible', 'ContentBasedDeduplication', 'CreatedTimestamp', 'DeduplicationScope', 'DelaySeconds', 'FifoQueue', 'FifoThroughputLimit', 'KmsDataKeyReusePeriodSeconds', 'KmsMasterKeyId', 'LastModifiedTimestamp', 'MaximumMessageSize', 'MessageRetentionPeriod', 'Policy', 'QueueArn', 'ReceiveMessageWaitTimeSeconds', 'RedriveAllowPolicy', 'RedrivePolicy', 'SqsManagedSseEnabled', 'VisibilityTimeout']"
 [list-item]
        AttributeNames=["ApproximateReceiveCount", "SentTimestamp"],
                                                   ^
Found 2 errors in 1 file (checked 1 source file)

Expected output

Success: no issues found in 1 source files

ngnpope avatar Jul 27 '22 22:07 ngnpope

Thank you for the report! Looks like an error in botocore shapes, because it says AttributeNames: List[QueueAttributeName]. However, boto3 documentation looks correct. Let me check if boto3 somehow overrides the value.

vemel avatar Jul 28 '22 20:07 vemel

I will add a stub for this parameter.

botocore bug report: https://github.com/boto/botocore/issues/2726

vemel avatar Jul 28 '22 20:07 vemel

Fixed in mypy-boto3-sqs 1.24.40. Please let me know if it works as it should.

vemel avatar Jul 29 '22 09:07 vemel

Please let me know if it works as it should.

I think this is still not quite right as I now have the inverse problem:

xxx.py:154: error: List item 0 has incompatible type "Literal['ApproximateNumberOfMessages']"; expected
"Literal['AWSTraceHeader', 'All', 'ApproximateFirstReceiveTimestamp', 'ApproximateReceiveCount', 'MessageDeduplicationId', 'MessageGroupId', 'SenderId', 'SentTimestamp', 'SequenceNumber', 'SqsManagedSseEnabled']"

As mentioned in my original report, I think that AttributeNames should be Sequence[MessageSystemAttributeNameType | QueueAttributeNameType] instead of Sequence[QueueAttributeNameType], but 5cd024419b6867a1bda90122f12a56c1f45b38cb changed this to Sequence[QueueAttributeFilterType] where QueueAttributeFilterType is newly added as QueueAttributeNameType | Literal['All', 'SqsManagedSseEnabled']

As of 1.24.40 I'm seeing the following in mypy_boto3_sqs/literals.py:

...
MessageSystemAttributeNameType = Literal[
    "AWSTraceHeader",
    "ApproximateFirstReceiveTimestamp",
    "ApproximateReceiveCount",
    "MessageDeduplicationId",
    "MessageGroupId",
    "SenderId",
    "SentTimestamp",
    "SequenceNumber",
]
QueueAttributeFilterType = Literal[
    "AWSTraceHeader",
    "All",
    "ApproximateFirstReceiveTimestamp",
    "ApproximateReceiveCount",
    "MessageDeduplicationId",
    "MessageGroupId",
    "SenderId",
    "SentTimestamp",
    "SequenceNumber",
    "SqsManagedSseEnabled",
]
QueueAttributeNameType = Literal[
    "All",
    "ApproximateNumberOfMessages",
    "ApproximateNumberOfMessagesDelayed",
    "ApproximateNumberOfMessagesNotVisible",
    "ContentBasedDeduplication",
    "CreatedTimestamp",
    "DeduplicationScope",
    "DelaySeconds",
    "FifoQueue",
    "FifoThroughputLimit",
    "KmsDataKeyReusePeriodSeconds",
    "KmsMasterKeyId",
    "LastModifiedTimestamp",
    "MaximumMessageSize",
    "MessageRetentionPeriod",
    "Policy",
    "QueueArn",
    "ReceiveMessageWaitTimeSeconds",
    "RedriveAllowPolicy",
    "RedrivePolicy",
    "SqsManagedSseEnabled",
    "VisibilityTimeout",
]
...

Amazon's documentation is broken/confusing, but I think this should be both sets of attributes combined, effectively:

QueueAttributeFilterType = Literal[
    "AWSTraceHeader",
    "All",
    "ApproximateFirstReceiveTimestamp",
    "ApproximateNumberOfMessages",
    "ApproximateNumberOfMessagesDelayed",
    "ApproximateNumberOfMessagesNotVisible",
    "ApproximateReceiveCount",
    "ContentBasedDeduplication",
    "CreatedTimestamp",
    "DeduplicationScope",
    "DelaySeconds",
    "FifoQueue",
    "FifoThroughputLimit",
    "KmsDataKeyReusePeriodSeconds",
    "KmsMasterKeyId",
    "LastModifiedTimestamp",
    "MaximumMessageSize",
    "MessageDeduplicationId",
    "MessageGroupId",
    "MessageRetentionPeriod",
    "Policy",
    "QueueArn",
    "ReceiveMessageWaitTimeSeconds",
    "RedriveAllowPolicy",
    "RedrivePolicy",
    "SenderId",
    "SentTimestamp",
    "SequenceNumber",
    "SqsManagedSseEnabled",
    "VisibilityTimeout",
]

ngnpope avatar Jul 30 '22 18:07 ngnpope

Looks like values from QueueAttributeNameType do not have any effect. However, Let's wait for a proper fix from AWS team. I left only values listed in boto3 documentation.

vemel avatar Jul 30 '22 20:07 vemel

https://github.com/aws/aws-sdk/issues/310

vemel avatar Jul 31 '22 21:07 vemel

@ngnpope if you have time, could you please test on your code and send me a list of correct values? is it really Union[MessageSystemAttributeNameType,QueueAttributeNameType]?

vemel avatar Aug 05 '22 01:08 vemel

FInally fixed in mypy-boto3-sqs1.24.60.

vemel avatar Aug 25 '22 22:08 vemel