-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add type annotations to pyreverse #4551
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
Merged
Pierre-Sassoulas
merged 5 commits into
pylint-dev:master
from
mbyrnepr2:1548_pyreverse_type_annotations
Jun 26, 2021
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4b35586
Add type annotations to pyreverse dot files
mbyrnepr2 4534b72
Add type annotations to pyreverse dot files
mbyrnepr2 4dc5dc5
Merge branch 'master' of github.com:PyCQA/pylint into 1548_pyreverse_…
mbyrnepr2 1d3d13b
Merge branch 'master' into 1548_pyreverse_type_annotations
mbyrnepr2 194e6d3
Add type annotations to pyreverse dot files
mbyrnepr2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,9 @@ | |
import os | ||
import re | ||
import sys | ||
from typing import Optional, Union | ||
|
||
import astroid | ||
|
||
RCFILE = ".pyreverserc" | ||
|
||
|
@@ -213,3 +216,43 @@ def visit(self, node): | |
if methods[1] is not None: | ||
return methods[1](node) | ||
return None | ||
|
||
|
||
def get_annotation_label(ann: Union[astroid.Name, astroid.Subscript]) -> str: | ||
label = "" | ||
if isinstance(ann, astroid.Subscript): | ||
label = ann.as_string() | ||
elif isinstance(ann, astroid.Name): | ||
label = ann.name | ||
return label | ||
|
||
|
||
def get_annotation( | ||
node: Union[astroid.AnnAssign, astroid.AssignAttr] | ||
) -> Optional[Union[astroid.Name, astroid.Subscript]]: | ||
"""return the annotation for `node`""" | ||
ann = None | ||
if isinstance(node.parent, astroid.AnnAssign): | ||
ann = node.parent.annotation | ||
elif isinstance(node, astroid.AssignAttr): | ||
init_method = node.parent.parent | ||
try: | ||
annotations = dict(zip(init_method.locals, init_method.args.annotations)) | ||
ann = annotations.get(node.parent.value.name) | ||
except AttributeError: | ||
pass | ||
else: | ||
return ann | ||
|
||
default, *_ = node.infer() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There can be an |
||
label = get_annotation_label(ann) | ||
if ann: | ||
label = ( | ||
rf"Optional[{label}]" | ||
if getattr(default, "value", "value") is None | ||
and not label.startswith("Optional") | ||
else label | ||
) | ||
if label: | ||
ann.name = label | ||
return ann |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 there can be an inference fail, we should catch it (like line 238). Btw it look like the code is similar, maybe we can create a function for it ?