-
Notifications
You must be signed in to change notification settings - Fork 89
Add Type Hinting #1114
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
base: main
Are you sure you want to change the base?
Add Type Hinting #1114
Conversation
If I understand correctly, you are referring to the |
viljarjf
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! Would be nice to also have annotated outputs for the functions, but that can come later. Would also be nice to see the effect of updating the docstrings, but it seems buildthedocs is failing still?
Python 3.8 is scheduled for end-of-life next month. Typing using builtin types was added in 3.9, so we could avoid e.g. from typing import Tuple if we want. I do like the more general Sequence though.
Using pipe for unions was added in 3.10, so we have to stick to typing.Union for now sadly.
Of course, all of this is circumvented by adding from __future__ import annotations, which converts all annotations to strings, but that feels a little inelegant to me...
Decorators for modifying the docstring is powerful! I wanted to try it out in orix earlier, as there is so much subclassing with basically duplicate docstrings. You could instead define the docstring in the parent class, and only overload certain words/names (like the class name). This additionally works for type annotations. Python 3.11 adds the typing.Self for annotating the outputs of class method factories, which is also useful in orix but less so here in pyxem.
| radial_range: Tuple[float] = None, | ||
| azimuth_range: Tuple[float] = None, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to be nitpicky, I believe this technically means "A tuple of length one, with the first element being a float". Arbitrary-length tuples are annotated tuple[int, ...], and length-2 tuples (as is the case here) are annotated tuple[int, int].
https://docs.python.org/3/library/typing.html#annotating-tuples
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@viljarjf That's not nitpicky at all! That's very helpful :) I was thinking there should be a way to do that but wasn't quite sure.
| peaks: BaseSignal, | ||
| center: Union[np.ndarray, Tuple[float]] = None, | ||
| calibration: Union[np.ndarray, Tuple[float]] = None, | ||
| column_names: Tuple[str] = None, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As mentioned, should be Tuple[str, ...]
| scan_rotation: float = None, | ||
| convergence_angle: float = None, | ||
| rocking_angle: float = None, | ||
| rocking_frequency: float = None, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here we could define Degrees = float earlier and use that as type hint instead, just to make it even clearer
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm what do you think of annotations? Maybe we just create a pyxem units module?
Degrees = Annotated[float, ValueRange(0,360)]
Rad = Annotated[float, ValueRange(0,np.pi*2)]| def get_slices1d( | ||
| self, | ||
| npt: int, | ||
| radial_range: typing.Union[typing.Sequence[float], typing.Sequence[int]] = None, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Explicitly supporting both float and int is done a lot of places, I see. Maybe we could define e.g. Number = int | float in a little utils.typing module for example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good idea :)
Add Type Hinting
I started adding type hinting as it seems to really help code completion. @viljarjf this might be exciting for you
I also added a wrapper which automatically extends the documentation with "other parmaters" I'm not sure if that should be helpful for code completion or not... Maybe we should just add those arguments directly as arguments rather than passing through as kwargs?
I also think the second part would be better in Hyperspy. @ericpre any thoughts?
Checklist
What does this PR do? Please describe and/or link to an open issue.
#951