Type () function not returning correct result for boto3 sqs object?

I am using syntax like Python 3 and I am writing a small AWS application that uses SQS. I am trying to hint at the type of queue. This is how I get the queue type:

>>> import boto3
>>> session = boto3.session.Session(
>>>     aws_access_key_id=AWS_ACCESS_KEY,
>>>     aws_secret_access_key=AWS_SECRET_KEY,
>>>     region_name='us-west-2'
>>> )
>>> sqs = session.resource('sqs')
>>> queue=sqs.Queue(AWS_QUEUE_URL)
>>> 
>>> type(queue)
<class 'boto3.resources.factory.sqs.Queue'>

      

And I write my intended type function like this:

def get_session() -> boto3.resources.factory.sqs.Queue:
    ...

      

But I am getting the error:

AttributeError: module 'boto3.resources.factory' has no attribute 'sqs'

      

I went through the package itself using dir(...)

. It factory

doesn't seem to contain sqs

, really. Thus, I have two questions:

  • Why is type

    returning this nonexistent class?
  • How do I find the correct type for this object?
+3


source to share


1 answer


The class sqs.Queue

seems to be generated on the fly every time it invoked:

>>> import boto3
>>> session = boto3.session.Session(aws_access_key_id='foo', aws_secret_access_key='bar', region_name='us-west-2')
>>> sqs = session.resource('sqs')
>>> sqs.Queue
<bound method sqs.ServiceResource.Queue of sqs.ServiceResource()>
>>> q = sqs.Queue('blah')
>>> type(q)
<class 'boto3.resources.factory.sqs.Queue'>
>>> q2 = sqs.Queue('bluh')
>>> type(q) == type(q2)
False

      

So, such a bad design choice on boto ends. I think this means that it is impossible to intelligently introduce an annotation, even with direct links.



It is best to give a clue to the type of common base class of all these dynamic classes boto3.resources.base.ServiceResource

:

>>> type(q).__bases__
(<class 'boto3.resources.base.ServiceResource'>,)

      

+2


source







All Articles