Adding type annotations to codebase #371
Replies: 3 comments 7 replies
-
|
@EvanBldy let me know if you have any thoughts here :) |
Beta Was this translation helpful? Give feedback.
-
|
Hi @hww-nikhilshankarnoble in order to do that I started making type-stubs for Gazu : https://github.com/johhnry/gazu-stubs This is the alternative from adding the types directly in the source code, but on the other hand it's another package to maintain. |
Beta Was this translation helpful? Give feedback.
-
|
Hi @EvanBldy have you had a chance to look at this proposal? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Creating Github Discussion as requested!
There have previously been bits of type hinting introduced in the
gazuAPI, but they've had to be ripped out in order to maintain Python 2.7 compatibility (see #342 #343). However, now that Python 2 compatibility has been dropped, I'd like to ask if there's a willingness within the CG-Wire team for type annotations to be introduced and maintained in the codebase?What are type annotations?
This article from the FastAPI documentation will do a better job of explaining them than I will be able to, but in short:
Python allows for developers to declare type "hints"/"annotations" to describe the type of the variable when they are defined, for use by IDEs and type checkers. Python is not a statically typed language and these hints aren't enforced at runtime, but can allow for a lot of really useful IDE feedback and error checking.
Take for example, the following call where VS Code guesses that the return type is a


strand gives autocomplete for that, whereas actually it will either be a dictionary or None:It also means that you won't be warned by the IDE when passing an incorrect object type in as a parameter:

Example of type annotating a function
As an example, this is how the type annotations would be added to the function below:
would become:
Once this is done, you can see that we get lovely autocompletion and error checking in the IDE, with it being able to validate paramater arguments and give proper autocompletion on returns as it will know the types:

Python compatibility
While some of the type annotation functionality was first introduced in Python 3.10, it can be used in Python 3.7+ versions by simply adding the below import at the top of the file
As long as that's included there should be no issues with compatibility going back to 3.7 - which I believe is the earliest supported version currently? Correct me if that's wrong.
Changes
These type hints don't have to be introduced wholesale, they can be progressively added over time and multiple PRs - I'm happy to do the leg work of adding the hints, I just want to make sure that it's something that you're interested in before committing the time towards it :) and that you'd be interested in maintaining these annotations moving forwards as the API continues to evolve.
Let me know if there's anything you want me to flesh out more!
Cheers,
Nikhil
Beta Was this translation helpful? Give feedback.
All reactions