-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Description
What is the problem this feature will solve?
While Astropy does not yet make extensive use of type hint annotations, there has been a great discussion in #15170 about implementing them. However, it is difficult to add type hints for dynamically generated objects, such as most of the units in astropy.units. 🤔
Adding type hint annotations for units would benefit downstream packages. For example, PlasmaPy makes heavy use of astropy.units, and the lack of type hint annotations for individual unit objects limits the extent to which mypy can do static type checking. For example, mypy isn't able to follow how types change when doing an operation like 88 * u.imperial.mile 🚗. (It is possible to configure mypy to ignore missing imports, though.)
Additionally, I've had trouble with IDEs not recognizing objects like u.km when they attempt to do smart highlighting, most likely because they are based on static type checking.
Describe the desired outcome
The possibilities that I can think of are that we could:
- Add a type stub file that contains the types of all of the units in
astropy.units. - Create a type stub package outside of this repository that could include these types.
Ideally it would be possible to automagically regenerate the type stub file based on the contents of astropy.units. That way, when new units are added, we can make sure that they get added to the .pyi file. In this comment, @mhvk made the important point that:
we do not want to run this on every import, but just on installation.
Additional context
I've been trying to find tools that can help with generating type stub files at runtime, like MonkeyType. However, I haven't been able to get any to work. 😿
Thank you!