Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal - introduce call in ProvidedInstance #40

Open
Minitour opened this issue Feb 8, 2025 · 0 comments · May be fixed by #41
Open

Proposal - introduce call in ProvidedInstance #40

Minitour opened this issue Feb 8, 2025 · 0 comments · May be fixed by #41

Comments

@Minitour
Copy link
Contributor

Minitour commented Feb 8, 2025

Currently the ProvidedInstance offers a get_value function which returns the attribute based on the _attrs path. However if the returned attribute is a callable (aka function) then one needs to perform a "double call".

For example

session = providers.Singleton(Session)

 awsauth = providers.Singleton(
        AWS4Auth,
        session.provided.get_credentials.get_value()().access_key,
        session.provided.get_credentials.get_value()().secret_key,
        config.region(),
        'aoss',
        session_token=session.provided.get_credentials.get_value()().token
    )

Notice how we first get the value

# This returns the attribute `get_credentials` which is in fact a function
session.provided.get_credentials.get_value()

Then we need to invoke it to obtain the access_key

session.provided.get_credentials.get_value()().access_key,
#                                         ^^^^ - Not very elegant 

Proposal - Introduce a call function

class ProvidedInstance:
    def __init__(self, provided: "BaseProvider[Any]") -> None:
        self._provided = provided
        self._attrs: List[str] = []

    def __getattr__(self, attr: str) -> "ProvidedInstance":
        self._attrs.append(attr)
        return self

   def call() -> Any:
       func = self.get_value()
       return func()

    def get_value(self) -> Any:
        resolved_provider_object = self._provided()

        if not self._attrs:
            msg = (
                "Please provide at least one attribute. For example: provide.some_attr"
            )
            raise Exception(msg)

        attribute_path = ".".join(self._attrs)
        attr_value = _get_value_from_object_by_dotted_path(
            resolved_provider_object,
            attribute_path,
        )
        return attr_value

This would make the above example appear as:

session = providers.Singleton(Session)

 awsauth = providers.Singleton(
        AWS4Auth,
        session.provided.get_credentials.call().access_key,
        session.provided.get_credentials.call().secret_key,
        config.region(),
        'aoss',
        session_token=session.provided.get_credentials.call().token
    )
@Minitour Minitour linked a pull request Feb 8, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant