Thanks to visit codestin.com
Credit goes to Github.com

Skip to content

Conversation

@CSSFrancis
Copy link
Member

@CSSFrancis CSSFrancis commented Sep 16, 2024


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

  • implementation steps
  • update the Changelog
  • mark as ready for review

What does this PR do? Please describe and/or link to an open issue.

#951

@coveralls
Copy link

coveralls commented Sep 16, 2024

Coverage Status

coverage: 92.85% (+0.03%) from 92.819%
when pulling 123b939 on CSSFrancis:add_typing
into 971a7be on pyxem:main.

@ericpre
Copy link
Contributor

ericpre commented Sep 20, 2024

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?

If I understand correctly, you are referring to the extend_docs decorator, right? I didn't look into details, but it seems to makes a lot of sense in pyxem because the hyperspy docstrings module is not public. It could also be used a few places in hyperspy too and that would be an improvement on how the docstring are reused.

Copy link
Collaborator

@viljarjf viljarjf left a 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.

Comment on lines +2095 to +2096
radial_range: Tuple[float] = None,
azimuth_range: Tuple[float] = None,
Copy link
Collaborator

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

Copy link
Member Author

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,
Copy link
Collaborator

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, ...]

Comment on lines +127 to +130
scan_rotation: float = None,
convergence_angle: float = None,
rocking_angle: float = None,
rocking_frequency: float = None,
Copy link
Collaborator

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

Copy link
Member Author

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,
Copy link
Collaborator

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.

Copy link
Member Author

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

@CSSFrancis CSSFrancis added this to the v1.0.0 milestone Oct 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants