-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Comments
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.
Though I suppose an explicit |
What about type-hints for Resources, Buckets, this would be super useful. |
See relevant issues: - boto3 <boto/boto3#1725> - jsonschema <python/typing#182>
Any plans to implement this ? |
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 |
Now that pep 561 has passed its possible to have a typehint package added for boto3.
The text was updated successfully, but these errors were encountered: