-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Fix pyreverse type hinting for class methods #5881
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
Fix pyreverse type hinting for class methods #5881
Conversation
This commit fixes the alignment of arguments and their type annotations in pyreverse printer output. It does so by checking for the type of the current function rather than the name of the first argument. This allows class methods having a non-standard first argument (different from "self" or "cls") to be correctly serialized in class diagrams.
Pull Request Test Coverage Report for Build 1953999813
💛 - Coveralls |
I'm going to try to cover the |
According to astroid docs, this happens for builtin functions implemented in C. In this case, we return an empty argument list.
@Pierre-Sassoulas, did you change the coveralls setting to disallow decreasing coverage PRs? The CI seems to have passed on that commit that did not have full coverage. @teobouvard Thanks for the contribution! I have requested a review from @DudeNr33 as they are the |
No, I think it can be problematic, for example in #5874 (comment) we removed code and the coverage decreased. I don't want to (have to) deal with this kind of false positive. I think the easier thing to do would be to check the coverage before approving. |
annotations = dict(zip(arguments, method.args.annotations[1:])) | ||
if method.args.args is None: | ||
return [] | ||
|
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 cleanup and lowering the indentation level!
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 catch, thank you for the fix. Looks good to me, well tested with both relevant cases added to the example test data. 👍
Only thing I noticed: this PR was set to milestone 2.14.0, while the changelog has the change listed for 2.13.0.
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.
Thank you for the quick review @DudeNr33 👍 Let's put it in 2.13 then !
This commit fixes the alignment of arguments and their type annotations
in pyreverse printer output.
doc/whatsnew/<current release.rst>
.Not a feature, is it an important bug fix ?
Type of Changes
Description
Until now, the following class
would generate the following PlantUML diagram when running
pyreverse -o puml example.py
This output displays wrong type hints because the alignment of arguments and their type annotations is based on the first argument having name
self
. This is not the case for class methods, where the first argument is usuallycls
and static methods where the first argument is not related to the class.This commit fixes the issue by performing the alignment using the method type rather than the parameters names. This generates the correct class diagram shown below.