youtype/mypy_boto3_builder

`session.client('...')`: Type of client is partially unknown

dd-ssc opened this issue · 1 comments

dd-ssc commented

Describe the bug
With typeCheckingMode = 'strict', pyright reports error: Type of "client" is partially unknown.

To Reproduce
Steps to reproduce the behavior:

  1. Install boto3-stubs with the ec2 extra as dev dependency (as per the pip docs, using poetry):
$ poetry add --group=dev 'boto3-stubs[ec2]'
  1. Run pyright on the following code sample
from boto3.session import Session

session = Session(profile_name=<my profiles>, region_name=<my region>)
client  = session.client('ec2')

Actual output

error: Type of "client" is partially unknown
    Type of "client" is "Overload[
        (service_name: Literal['accessanalyzer'], ...),
          ... (similar entries for many more AWS services) ...
        (service_name: Literal['ec2'], ...),
          ... (similar entries for many more AWS services) ...
        (service_name: Literal['xray'], ...)]

Expected output

none

Additional context

Working on currently latest macOS Ventura 13.4 with boto / boto-stubs 1.26.155.
I am using different AWS services in different projects and I observe the same issue in all of them.

I understand from the boto3-stubs docs (under install annotations for services you use) that it should suffice to add the "extra" for the services I use. That doesn't seem to work for me.

I tried installing boto3-stubs[essential], but that doesn't seem to make any difference. Also, ec2 happens to be in the list of extras for the project the sample code above is from, but in another project, I use codecommit, which is not.

I tried installing boto3-stubs[boto3], but that doesn't seem to make any difference, either - in fact, this doesn't seem to install any extras at all (from poetry add ... output):

Package operations: 4 installs, 0 updates, 0 removals

  • Installing types-awscrt (0.16.19)
  • Installing botocore-stubs (1.29.155)
  • Installing types-s3transfer (0.6.1)
  • Installing boto3-stubs (1.26.155)

Following this I tried installing boto3-stubs[all] - that does fix the issue, but requires installing countless modules - of which I only use one.

I also tried using explicit type annotations:

from boto3.session import Session
from mypy_boto3_ec2.client import EC2Client

session = Session(profile_name=<my profiles>, region_name=<my region>)
client: EC2Client = session.client('ec2')

but that doesn't seem to make a difference, either.

Finally, I tried installing the VS Code extension and set it up as documented, but the issue there is the same.

These observations seem consistent for different AWS services / boto-stubs extras in different project.

Am I doing anything wrong ? Have I misunderstood anything ?

vemel commented

Hello!

Sorry for the late response. Yes, this is a known issue, that pyright complains about partially unknown types if not all overloads can be resolved.

Options:

  • Add boto3-stubs[all] dev dependency
  • # type: ignore this line. client will still correctly be types as EC2Client

I hope thi helps. Please let me know if you have any better solutions for this issue.