Skip to content
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

Open
Findus23 opened this issue Feb 17, 2021 · 3 comments
Open

Python Type Hints #501

Findus23 opened this issue Feb 17, 2021 · 3 comments

Comments

@Findus23
Copy link
Contributor

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).

@hannorein
Copy link
Owner

hannorein commented Feb 17, 2021

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?

@Findus23
Copy link
Contributor Author

Findus23 commented Feb 17, 2021

That's the only limitation I found: mypy can check the types of a package from the cli and stubtest (included with mypy) can check type files, but it only checks the content of the stub files without taking the real code in consideration (which means it won't notice if the stub file is incorrect). (see https://github.com/python/mypy/issues/5028)

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 @property.setter() (ambv/retype#28)

@Findus23
Copy link
Contributor Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants