question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using aws session to create clients results in "TypeError: client() missing 1 required positional argument: 'self'"

See original GitHub issue

Hi I’m trying to use the boto3 session in a container to create new boto3 clients. I’m running into the error "TypeError: client() missing 1 required positional argument: 'self'". Am I setting this up correctly?

import boto3.session

class Boto3ContainerWorking(containers.DeclarativeContainer):
    config = providers.Configuration()

    session = providers.Singleton(
        boto3.session.Session,
        aws_access_key_id=config.aws_access_key_id,
        aws_secret_access_key=config.aws_secret_access_key,
        aws_session_token=config.aws_session_token
    )

    s3_client = providers.Singleton(
        session().client,  # <------ this works
        service_name='s3'
    )


class Boto3ContainerNotWorking(containers.DeclarativeContainer):
    config = providers.Configuration()

    session = providers.Singleton(
        boto3.session.Session,
        aws_access_key_id=config.aws_access_key_id,
        aws_secret_access_key=config.aws_secret_access_key,
        aws_session_token=config.aws_session_token
    )

    s3_client = providers.Singleton(
        session.provides.client,   # <------ this doesn't work
        service_name='s3'
    )


def main_not_working():
    container = Boto3ContainerNotWorking()
    container.init_resources()
    container.wire(modules=[sys.modules[__name__]])

    s3_client = container.s3_client()  # breaks here with "TypeError: client() missing 1 required positional argument: 'self'"


def main_working():
    container = Boto3ContainerWorking()
    container.init_resources()
    container.wire(modules=[sys.modules[__name__]])

    s3_client = container.s3_client()  # This passes

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
ecs-jnguyencommented, Feb 25, 2021

Thank you! This helped me a lot!

1reaction
rmk135commented, Feb 25, 2021

@ecs-jnguyen here is a working code sample:

import boto3.session
from dependency_injector import containers, providers


class Service:
    def __init__(self, s3_client, sqs_client):
        self.s3_client = s3_client
        self.sqs_client = sqs_client


class Boto3Container(containers.DeclarativeContainer):

    config = providers.Configuration()

    session = providers.Resource(
        boto3.session.Session,
        aws_access_key_id=config.aws_access_key_id,
        aws_secret_access_key=config.aws_secret_access_key,
        aws_session_token=config.aws_session_token,
    )

    s3_client = providers.Resource(
        session.provided.client.call(),
        service_name='s3',
    )

    sqs_client = providers.Resource(
        providers.MethodCaller(session.provided.client),  # Alternative syntax
        service_name='sqs',
    )

    service1 = providers.Factory(
        Service,
        s3_client=s3_client,
        sqs_client=sqs_client,
    )

    service2 = providers.Factory(
        Service,
        s3_client=session.provided.client.call(service_name='s3'),    # Alternative inline syntax
        sqs_client=session.provided.client.call(service_name='sqs'),  # Alternative inline syntax
    )


def main():
    container = Boto3Container()
    container.init_resources()

    s3_client = container.s3_client()
    print(s3_client)

    sqs_client = container.sqs_client()
    print(sqs_client)

    service1 = container.service1()
    print(service1, service1.s3_client, service1.sqs_client)
    assert service1.s3_client is s3_client
    assert service1.sqs_client is sqs_client

    service2 = container.service1()
    print(service2, service2.s3_client, service2.sqs_client)
    assert service2.s3_client is s3_client
    assert service2.sqs_client is sqs_client


if __name__ == '__main__':
    main()

Dependency Injector can call provided object methods and fetch attributes. You can read more about this feature here: https://python-dependency-injector.ets-labs.org/providers/provided_instance.html

I also added an example to the docs with similar example.

PS: A note on service2. Session returns singleton object for the same client session.client(service_name='s3') is session.client(service_name='s3'). It’s a reason why last two asserts work.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeError: Missing 1 required positional argument: 'self'
You need to instantiate a class instance here. Use p = Pump() p.getPumps(). Small example - >>> class TestClass: def __init__(self): print("in init")...
Read more >
TypeError: missing 1 required positional argument: 'self'
The Python "TypeError: missing 1 required positional argument: 'self'" occurs when we call a method on the class instead of on an instance...
Read more >
Python missing 1 required positional argument: 'self' Solution
The “missing 1 required positional argument: 'self'” error is raised when you do not instantiate an object of a class before calling a...
Read more >
Feature Store methods are broken · Issue #3090 - GitHub
Describe the bug When calling FeatureGroup.create (and other) method the SDK fails with ... missing 1 required positional argument: 'self'.
Read more >
TypeError: load() missing 1 required positional argument - Odoo
I go this following error when I click a button on my custom addons: Error: Odoo Server Error Traceback (most recent call last):...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found