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

Type annotation in the for loop causes Unknown return values on method calls #9891

Open
gaborbernat opened this issue Feb 12, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@gaborbernat
Copy link

gaborbernat commented Feb 12, 2025

Having a type annotation in a for loop causes method calls on it to be threated as possibly unknown.

Code or Screenshots

❯ uv venv -p 3.13
❯ uv pip install pyright mypy types-boto3[sts] boto3

Resolved 17 packages in 3.89s
Installed 17 packages in 196ms
 + boto3==1.36.13
 + botocore==1.36.13
 + botocore-stubs==1.36.12
 + jmespath==1.0.1
 + mypy==1.15.0
 + mypy-extensions==1.0.0
 + nodeenv==1.9.1
 + pyright==1.1.393
 + python-dateutil==2.9.0.post0
 + s3transfer==0.11.2
 + six==1.17.0
 + types-awscrt==0.23.9
 + types-boto3==1.36.13
 + types-boto3-sts==1.36.0
 + types-s3transfer==0.11.2
 + typing-extensions==4.12.2
 + urllib3==2.3.0
from boto3 import client
from types_boto3_sts import STSClient

aws_access_key_id: str = ""
for role in ["r"]:
    sts_client: STSClient = client("sts", aws_access_key_id=aws_access_key_id)
    try:
        resp = sts_client.assume_role(RoleArn=role, RoleSessionName="S")
    except ValueError:
        continue
    else:
        aws_access_key_id = resp["Credentials"]["AccessKeyId"]
❯ .venv/bin/pyright --pythonpath=.venv/bin/python magic.py
magic.py:8:61 - error: Argument type is partially unknown
    Argument corresponds to parameter "aws_access_key_id" in function "client"
    Argument type is "str | Unknown" (reportUnknownArgumentType)
 magic.py:11:21 - information: Type of "resp" is "AssumeRoleResponseTypeDef | Unknown"
1 error, 0 warnings, 1 information

Passes the type checker on mypy. Removing the for loop also makes the type checker pass.

The type annotations from types-boto3[sts]:

    def assume_role(
        self, **kwargs: Unpack[AssumeRoleRequestRequestTypeDef]
    ) -> AssumeRoleResponseTypeDef:
class CredentialsTypeDef(TypedDict):
    AccessKeyId: str
    SecretAccessKey: str
    SessionToken: str
    Expiration: datetime

class AssumeRoleResponseTypeDef(TypedDict):
    Credentials: CredentialsTypeDef
    AssumedRoleUser: AssumedRoleUserTypeDef
    PackedPolicySize: int
    SourceIdentity: str
    ResponseMetadata: ResponseMetadataTypeDef
@gaborbernat gaborbernat added the bug Something isn't working label Feb 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant