youtype/mypy_boto3_builder

session.get_credentials() has become optional

Closed this issue · 2 comments

Describe the bug
session.get_credentials() has become optional, but it shoudn't.

To Reproduce
Steps to reproduce the behavior:

  1. Install boto3-stubs
  2. Run mypy/pyright on the following code sample
import boto3

session = boto3.Session()
creds = session.get_credentials()

reveal_type(creds)

Actual output

[...](file.py:6: note: Revealed type is "Union[botocore.credentials.Credentials, None]")

Expected output

[...](file.py:6: note: Revealed type is "botocore.credentials.Credentials")

Additional context
Your OS, boto3-stubs installation method, boto3 version, etc.

boto3==1.29.1
boto3-stubs-lite==1.29.1

Hello!

Session.get_credentials can return None if there are no credential providers, or none of the providers return valid credentials. So, you have to check if creds are not None

import boto3

session = boto3.Session()
creds = session.get_credentials()
assert creds is not None

reveal_type(creds)  # Type of "creds" is "Credentials"

Please let me know if it works for you.

Ah, I see.

    def load_credentials(self):
        """
        Goes through the credentials chain, returning the first ``Credentials``
        that could be loaded.
        """
        # First provider to return a non-None response wins.
        for provider in self.providers:
            logger.debug("Looking for credentials via: %s", provider.METHOD)
            creds = provider.load()
            if creds is not None:
                return creds

        # If we got here, no credentials could be found.
        # This feels like it should be an exception, but historically, ``None``
        # is returned.
        #
        # +1
        # -js
        return None