-
Notifications
You must be signed in to change notification settings - Fork 206
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
Add type hinting on user-facing methods #2986
base: RELEASE_next_minor
Are you sure you want to change the base?
Add type hinting on user-facing methods #2986
Conversation
Codecov Report
@@ Coverage Diff @@
## RELEASE_next_minor #2986 +/- ##
======================================================
- Coverage 80.80% 80.77% -0.04%
======================================================
Files 209 209
Lines 32709 32736 +27
Branches 7325 7333 +8
======================================================
+ Hits 26432 26442 +10
- Misses 4514 4523 +9
- Partials 1763 1771 +8
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. |
I intentionally return I also return These could be fixed by using function overloading, but there would be a huge amount of code bloat since many methods having |
72f7022
to
1b8419d
Compare
If I understand correctly, you are saying that you prefer to providing type hints which are sometimes in favour of always correct at the cost of having a syntax slightly more complicated to read? If so, without any doubt, I prefer the "always correct" approach, because the other one is misleading! ;) Would it be possible to add a test on CI to check that the imports in the |
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.
It needs to be rebase to fix the failure of the test suite. Ideally, it would be better to rebase on RELEASE_next_major
as this will not be a minor release before release 2.0 - we need to start using rosettasciio ASAP.
@@ -25,6 +26,8 @@ | |||
from hyperspy.ui_registry import add_gui_method, DISPLAY_DT, TOOLKIT_DT | |||
from hyperspy.signal import BaseSetMetadataItems | |||
|
|||
if TYPE_CHECKING: | |||
from hyperspy.models.edssemmodel import EDSSEMModel |
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.
Is it possible to test these imports on CI?
Description of the change
.mean()
and.sum()
. See note below aboutout
.I am using
TYPE_CHECKING
to solve circular import errors. See here for more info.Progress of the PR
upcoming_changes
folder (seeupcoming_changes/README.rst
),readthedocs
doc build of this PR (link in github checks)Here is an example of how pylance recognizes the type of
m
, even before running the code.Notes
I intentionally return
BaseSignal
fromhs.load
. Strictly, it should beUnion[BaseSignal, List[BaseSignal]]
, since the function can return a list of signals. But since I'm not going for full mypy coverage and instead going for "help out the user", I think it is more friendly to stick to just the former.I also return
BaseSignal
from methods that do have anout
parameter, when it strictly should beUnion[None, BaseSignal]
. Again because of convenience: if you're usingout
then you won't be returning into an object so you don't care that there is a return type. When you are returning you don't want the output type to possibly beNone
(this raises warnings aboutNone
not having the e.g..plot()
methods.These could be fixed by using function overloading, but there would be a huge amount of code bloat since many methods having
out
as an argument.