-
Notifications
You must be signed in to change notification settings - Fork 9
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
MAINT: add typehint example #169
Conversation
Codecov Report
@@ Coverage Diff @@
## master #169 +/- ##
=======================================
Coverage 83.78% 83.78%
=======================================
Files 25 25
Lines 1351 1351
=======================================
Hits 1132 1132
Misses 219 219 |
Hi @PProfizi, I've added an actual example of a method with typehinting: Changes description:
Now the documentation generated (check artifacts) is showing the types in the description even though they are not explicitly written. When you agree and approve this I will try to do the same in dpf-core. |
Hi @GuillemBarroso , I took a look at the doc for sphinx auto-typehint and maybe we want to use |
After playing with the extension The method used as example contains:
The source code looks like this: Note that
Unfortunately, Regarding the Now the idea is to decide if we want to use |
@RobPasMue You could be interested in that for your geometry project. |
Adding @akaszynski because it could be used for other projects. |
I don't have a super strong opinion on the options, but would probably go for |
For the @GuillemBarroso if possible, can you add some screenshots of that to be able to compare with the same piece of code? |
Done. Please, see comment above. |
I agree with @MaxJPRey - for me it is more clear to see the |
Agreed
Personally I tend to prefer PS: the point of having As for the Union, I do not have an opinion, but I guess |
I just find it a bit awkward to see both "Optional" and "None" words for optional parameters depending if we have one or several valid types. Personally, I would go with the 4th approach. |
Let's use |
Hi, just joining this interesting discussion. My only concern is about not following the numpydoc style, as I see that the type hints in the We should investigate an approach to just modify our This issue is related with numpy/numpydoc#196 |
FMPOV, the advantage of not having to repeat the types in the docstring (and them potentially being stale!) outweighs being non-compliant with the numpydoc style. As for the style checker, is there a way we can ignore this specifically? As mentioned in the discussion today: the type hints should only be the preferred way if they are checked (e.g. by mypy). Otherwise, having the types in the docstring only is still the preferred approach. |
Changes description:
I will prepare an example using typehinting in conjuntion with mypy in the dev-guide, see ansys/pyansys-dev-guide#187. I think this is now ready to merge. |
Let's make a poll here: Upvote your preferred option. |
|
|
|
|
|
|
Just noticed the following bug in the |
src/ansys/dpf/post/result_data.py
Outdated
def plot_contour(self, display_option: str = "time", option_id=1, **kwargs): | ||
def plot_contour( | ||
self, | ||
display_option: Optional[str] = "time", |
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.
@GuillemBarroso quick question: is Optional
mandatory even when it obviously is due to the default value?
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.
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.
Common misunderstanding here: Optional
does not mean "you do not have to pass this parameter". It means "this parameter can be None
". It's optional in the type-theory sense, not optional in the "has a default" sense.
Example:
The following is typing.Optional
def func(x: Optional[int]) -> int:
if x is None:
return 0
else:
return x + 1
This is optional in the "has a default" sense:
def func(x: int = 0) -> int:
return x + 1
The typing.Optional
can sometimes be a bit of a code smell (better to give a nice default value!). And it's wrong to mark a parameter as Optional
if passing None
to it will not be handled gracefully, even if it has a default value.
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.
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.
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.
I will change the optional parameters depending on the poll's result.
If braces-after
ends up winning, we should first figure out if we can get around the aforementioned bug.
Hi @greschd, I was now checking the bug you mentioned before and I am not sure I am getting the same behavior. This is a test I did with a very long parameter description: Is that what you meant? By the way, I am starting to change my mind about the option that I prefer... Now I see the |
Doesn't look like it.. is the long parameter description split into multiple lines in the source for your example? |
My bad, that was the issue. I had all the description in a single line. If I split it into several lines I get the bug you mentioned: |
Well, if you change your vote to the |
I have just updated my vote on the @PProfizi, feel free to merge this PR if everything looks good to you (using |
Add example in plot_contour to verify that we are correctly handling typehinting.