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

[Feature Request] Type hints #1725

Closed
stealthycoin opened this issue Oct 4, 2018 · 6 comments
Closed

[Feature Request] Type hints #1725

stealthycoin opened this issue Oct 4, 2018 · 6 comments
Labels
feature-request This issue requests a feature. p2 This is a standard priority issue

Comments

@stealthycoin
Copy link
Contributor

Now that pep 561 has passed its possible to have a typehint package added for boto3.

@stealthycoin stealthycoin added the feature-request This issue requests a feature. label Oct 4, 2018
@jamesls
Copy link
Member

jamesls commented Oct 9, 2018

Wouldn't this require an explicit cast though for clients/resources? While we can generate types for the clients/resources, there's not way to automatically associate the creation of a client to a specific type.

boto3.client('s3') -> S3ClientType
boto3.client('ec2') -> EC2ClientType

def client(service_name: str) -> What do we put here?

Though I suppose an explicit cast() is still better than nothing.

@salimfadhley
Copy link

What about type-hints for Resources, Buckets, this would be super useful.

l0b0 added a commit to linz/geostore that referenced this issue Jan 28, 2021
@lqueryvg
Copy link

Any plans to implement this ?

@tim-finnigan
Copy link
Contributor

tim-finnigan commented Apr 6, 2022

Linking related feature requests: #1055 and #2036

@kyle-query
Copy link

kyle-query commented Oct 16, 2022

Wouldn't this require an explicit cast though for clients/resources? While we can generate types for the clients/resources, there's not way to automatically associate the creation of a client to a specific type.

boto3.client('s3') -> S3ClientType
boto3.client('ec2') -> EC2ClientType

def client(service_name: str) -> What do we put here?

Though I suppose an explicit cast() is still better than nothing.

If I understand your question, it can be addressed with PEP-586 which was accepted in Python 3.8. You can define any number of overloads:

from typing import Literal, Union, overload

@overload
def client(service_name: Literal["s3"]) -> S3ClientType: ...

@overload
def client(service_name: Literal["ec2"]) -> EC2ClientType: ...

def client(service_name: Literal["s3", "ec2"]) -> Union[S3ClientType, EC2ClientType]:
    if service_name == "s3":
        return S3ClientType(...)

    return E2ClientType(...)

Note, at the call site, you must narrow the argument to one of the ones listed in the overloads.

c = client("s3") # fine, c is assigned the type S3ClientType

x = os.getenv("SERVICE_NAME") or "s3"
c = client(x) # Argument of type 'str' cannot be assigned to parameter service_name of type Literal[...

if x == "s3":
    c = client(x) # ok, type narrowing satisfies the type checker

@aBurmeseDev aBurmeseDev added the p2 This is a standard priority issue label Nov 10, 2022
@tim-finnigan
Copy link
Contributor

Closing in favor of #1055 for further tracking (provided update here).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request This issue requests a feature. p2 This is a standard priority issue
Projects
None yet
Development

No branches or pull requests

7 participants