-
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
Generate and provide PEP-484 stub files for type-checking #1055
Comments
We looked into this a while ago, but it ended up not being terribly useful for a few reasons. Right off the bat there's no good way to say Basically, to make type hints effective we would need to rewrite boto3 and botocore to be fully code-generated, which is something we don't currently have plans to do. |
You can cast thing with the typing, so people calling boto3.client could do that themselves (i.e. That said, I don't think the clients are the most interesting place to see these; rather, I think the resources are. Looking at a (For clients, the dicts would probably be |
That's a fair point, resources would be a much better candidate for stubs |
How does this actually work? There's no type Anyhow, I'd echo that having types for boto would be really useful. |
I just used Java and Javascript (Typescript) SDKs lately (had to implement roughly the same code on 3 platforms), and type information is definitively something that would be nice to have. Comparing:
Most benefit would come from providing type stubs for requests and responses. |
@tuukkamustonen the good news is that it's possible to have types for dicts now! So that blocker is gone. But we still have the issue that, since clients are created from factory methods, the type checker won't know what class your client is. |
Also note that although |
Even if you don't want to do a wholesale refactoring, just providing a set of types for responses would be super helpful. (I'm assuming there's some of specification that is generating the resources and clients). As an example, I made a type stub for the S3PutEvent (based on AWS docs). It would be nice just to provide empty typed classes for easy mocking/type-checking too, e.g. here's a couple methods from Amazon Athena annotated. Even though it's all factory methods, at least you could write something like:
or
which would then let the type checker work quite well :) The go AWS SDK generates all of these stubs so I'm guessing there's some dictionary somewhere that might work for it? |
I take the point about a full refactoring, but perhaps its possible to provide an additional factory API which would be typesafe - users could opt into that, and its implementation could just be a shim to the existing factory interface? That doesn't imply a change to fully code-generated as far as I can tell (but perhaps I'm missing something :)). |
Looking forward seeing this out. |
I don't understand the point about requiring full refactoring of boto3.... A solution could be to also generate machine-readable documentation in the form of stub classes. |
@yaroslavvb once you have the stub files generation taken care of, there's little reason for them to actually remain stub files. Say there will be a client stub like this: class CloudWatch(BaseClient):
def delete_alarms(self, param: ParamType) -> ReturnType ... Then it's not really that big of a leap to turn it to this: class CloudWatch(BaseClient):
def delete_alarms(self, param: ParamType) -> ReturnType ...
return self._make_api_call("DeleteAlarms", {"Param": param}) I experimented with this recently. I wrote a script that generates clients based on botocore data files. I just pushed it in case someone is interested: https://github.com/KyrychukD/botocore/tree/generate-clients/botocore/data |
@KyrychukD is that the right link? I just see some data files that were last touched 1 month ago. If you had a list of generated stub files that can be used for type annotations, that would be pretty useful, could finally get decent completion/help in IDE's |
The link is to the branch where I experimented with generating client classes based on the data files. I meant to link to the root of the repo instead of the data directory, though, sorry about that. The right link is https://github.com/KyrychukD/botocore/tree/generate-clients
Unfortunately, I do not have a complete solution for boto3 at the moment, all work I've done so far was for botocore. |
Lack of autocompletion for methods available when instantiating a client makes using boto3 very unpleasant. Please address this in a future version of boto3! |
Should now be possible with https://mypy.readthedocs.io/en/latest/literal_types.html
https://mypy.readthedocs.io/en/latest/more_types.html#typeddict |
Microsoft's Azure looks better designed, just sayin ;) https://github.com/Azure/azure-sdk-for-python (I'm trying to light the fire to get AWS to try on their SDK) |
Is there any hope in getting type checking in boto3? Should we be looking to third party libraries to be able to provide stubs if this capability won't be introduced in the core library anytime soon? I am specifically looking at the boto3_type_annotations project which has an active PR to convert to the PEP-561 style of stubs. The project's maintainer opened up an issue on this project to get feedback on the approach, at which point he was directed to an alternative repo that AWS has (https://github.com/boto/botostubs) -- though that has been caveated to not use in production and hasn't been touched for about 5 months now. I think there was some mixed signals with them saying:
Python 3.8 is now out... are there updates coming soon to make "production ready" typing information available? |
Hello! I have created a
Let me know if this is useful. |
Any updates to this? Would be extremely helpful to speed up development and prevent bugs. |
I brought this topic up for discussion with the team and the consensus was that this feature request is not currently under consideration. Integrating type hints will be considered for the next major version of boto3. In the meantime there are packages such as https://pypi.org/project/boto3-stubs/ you can use for type checking. |
@tim-finnigan is there any tracking issue/project for the next major version? Is/will that be boto4? |
@fitzoh no there is not currently a tracking issue or project for the next major version. There will be an official announcement prior to then. (I can’t provide any guarantee as to when that would be.) In the meantime we are using the |
Having fleshed-out type definitions for the objects that boto3 generates at runtime would make writing bug-free code against boto3 a lot easier.
(The documentation generation code must already do the majority of the inference that would be required for this; could it be repurposed in some way?)
The text was updated successfully, but these errors were encountered: