-
Notifications
You must be signed in to change notification settings - Fork 221
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
Python Type Hints #501
Comments
Nice! I completely agree, type hints would be great. And adding it as a new package rather than breaking backwards compatibility seems like the right way to go for the time being. Is there an easy way to add units tests to make sure we can catch any changes? |
That's the only limitation I found: mypy can check the types of a package from the cli and One thing that could work quite nicely is using retype to automatically apply the stub files to the original rebound code in tests and then run mypy on this output. Update: This would be a great idea, but unfortunately retype doesn't support |
Okay, now that ambv/retype#28 is fixed (or rather worked around), the rest seems to work. Findus23/rebound-stubs@a63d190 https://travis-ci.com/github/Findus23/rebound-stubs retype throws an error if the type signature between rebound and rebound-stubs differs and therefore the test fails. I also run mypy on the complete typed rebound source, but don't consider its output as it gives a lot of unrelated errors. |
This isn't really an issue, but more of an ad for a rebound extension.
I am quite a fan of type hints in Python as they are a great sanity check to see if the code makes sense and doesn't have a typo in some attribute.
But they also make it far more easy to simply explore the possibility of a library if ones editor does support autocompletion:
test-2021-02-17_18.01.20.mp4
And even if it doesn't one can use mypy to do the static type check from the commandline.
I created type hints for all classes and functions in rebound in the last few weeks as a test to see if it was possible.
I would not recommend adding them to rebound anytime soon as they are a bit buggy and would break support with old python versions (<3.6 I think).
But thankfully one can create type hints in separate stub files and ship them as a separate python package:
https://github.com/Findus23/rebound-stubs
So a single additional pip install gives all the benefits of the type hints. And as it is not loaded during the runtime, it can't introduce any new bugs into the code.
If anyone is interested, I could also publish the package to pypi and give write permission to Hanno, so I don't hog the namespace (it seems like the package has to be named
rebound-stubs
to work).The text was updated successfully, but these errors were encountered: