-
Notifications
You must be signed in to change notification settings - Fork 101
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
We should have type annotations #1709
Comments
I just found another really amazing usecase with vscode that is enabled by type hinting. In the example
If we are editing code below that, the editor does not know what However, if I simply put a wrapper around
Then anywhere later in the code, vscode knows that That's really cool. I definitely think we should add type hints to many of our most common methods and functions for this reason alone. |
Interestingly, you can get the same behavior without adding type hints to the definition of
That works just fine, and the editor knows what |
It looks like there are a couple projects that do code analysis to guess type hinting information and then automatically modify your code. So this could be a first step if we wanted to do this with much of or most of PINT: |
Type hinting is also something that can reasonably be done incrementally. In my experience it often turns up awkward corner cases (oops, I forgot that could return None) so it can be useful to go through the code fixing these at the same time. There will be a certain amount of fighting with the type checker - many interfaces don't really work especially well for static typing (notably dynamically generated class attributes like A further benefit is that the generated documentation can (does?) automatically include type information for function arguments and return values; for fully documented functions this doesn't necessarily add much, but it can save some effort when documenting new functions, and it makes the docs for poorly or un-documented functions more usable. |
There's some discussion in #1717 on the best way to do this. Some points that I think came out of that discussion (others may disagree!):
There are some questions:
|
Often the correct types for function arguments require some thought. But many IDEs - including VSCode - can be persuaded to include "inlay hints" showing inferred types for function arguments, return values, and variables; generally double-clicking on these will fill in the guess as source code. So a semi-automated procedure can be quite quick and convenient. |
I generally agree with this plan. In terms of typing quantities, how would that interact with |
If we decide to take typing seriously, we could add mypy (or another type checker) to our CI. It is (they are) reasonably well-designed for a codebase that is only partially annotated, and they support incrementally annotating more and more of the code. They also support linter-like comments to suppress known non-problems. |
I really haven't worked with typing and Quantities, so I'm not sure how this would look. I think a conservative approach might be to simply mark Quantities as Quantities? |
The PR #1725 is an initial setup getting static type checking into our CI. There are also some notes on what a gradual typing workflow would look like. |
For Astropy version 6.0, we can have the best of both worlds: we can write @u.quantity_input
def spin_period(spin_frequency: u.Quantity[u.Hz]) -> u.Quantity[u.s]: ... and it will do the right thing in terms of both static type checking and informing Unfortunately, it looks like pre-6.0 - @u.quantity_input(spin_frequency=u.Hz)
def spin_period(spin_frequency: u.Quantity) -> u.Quantity: ... but I'll have to try that out. [Edited to add:] Looks like this works; not as nice as the astropy 6.0 version but seems to work okay. |
As we do more of this, we might want to standardize some type definitions of our own. For instance, we often have things like: |
There were some issues combining type hints and unit decorators with old astropy.
seems to fail with the older astropy since it interprets the return type for a return unit. Without the
and specify the units for the return rather than the type/class. This may exclusively be in |
Type hinting was introduced in Python 3.5. Now that we no longer support older python versions (<3.8), there is no reason why we cannot have type annotations everywhere. It makes life so much easier while using an IDE with code highlighting.
30-05-2024 Update: This is gradually being implemented.
The text was updated successfully, but these errors were encountered: