youtype/mypy_boto3_builder

"Incompatible types in assignment" when using boto3-stubs-lite

ericbn opened this issue · 4 comments

Describe the bug
Mypy fails with "Incompatible types in assignment" when using boto3-stubs-lite and an assignment with a type annotation as in the documentation.

To Reproduce
Steps to reproduce the behavior:

  1. Install boto3-stubs-lite[sqs]
  2. Run mypy on the following code sample
import os

import boto3
from mypy_boto3_sqs.client import SQSClient

QUEUE_URL = os.environ["QUEUE_URL"]
MAX_NUMBER_OF_MESSAGES = 2

sqs: SQSClient = boto3.client("sqs")
response = sqs.receive_message(
    QueueUrl=QUEUE_URL,
    MaxNumberOfMessages=MAX_NUMBER_OF_MESSAGES,
    MessageAttributeNames=["All"],
)
print(response["Messages"])

Actual output

src/sqs.py:9: error: Incompatible types in assignment (expression has type "BaseClient", variable has type "SQSClient")  [assignment]
Found 1 error in 1 file (checked 1 source file)

Expected output

Success: no issues found in 1 source file

Additional context
Using boto3-stubs-lite==1.26.41 and mypy-boto3-sqs==1.26.0.post1.

Two workarounds for the issue are:

  1. pip uninstall boto3-stubs-lite (uninstall boto3-stubs-lite or just install mypy-boto3-sqs)
  2. sqs: SQSClient = boto3.client("sqs") # type: ignore[assignment] (add the comment)

I was hoping it could work without any of the workarounds.

vemel commented

Unfortunately, the only way is to explicitly specify type and type: ignore it. I will try to find a better solution.

Maybe if you change

 def client(
     service_name: str,
     region_name: Optional[str] = ...,
     api_version: Optional[str] = ...,
     use_ssl: Optional[bool] = ...,
     verify: Union[bool, str, None] = ...,
     endpoint_url: Optional[str] = ...,
     aws_access_key_id: Optional[str] = ...,
     aws_secret_access_key: Optional[str] = ...,
     aws_session_token: Optional[str] = ...,
     config: Optional[Config] = ...,
-) -> BaseClient: ...
+) -> Any: ...

in boto3-stubs-lite-1.26.41/boto3-stubs/__init__.pyi? (just for the stubs-lite)

vemel commented

@ericbn thank you for the report and the fix. I tested it and released a new version with the fix. You can test in in boto3-stubs-lite 1.26.47.post1

Please try it out and let me know if it works as it should.

Worked, thanks! 🎉