-
Notifications
You must be signed in to change notification settings - Fork 231
[Python APIView] Support @overloads and fix issue with parameterized decorators #3044
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
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
eaf8825
Progress on fix.
tjprescott 768858e
Parsing progress.
tjprescott f20350f
Progress on astroid parsing.
tjprescott e4a4dc3
Complete implementation for @overload
tjprescott b5a4064
Add decorator test.
tjprescott 4c5ce54
Decorator fixes.
tjprescott 1269def
Update changelog.
tjprescott a607642
Update method comment.
tjprescott edc7788
Code review feedback and rebase fixes.
tjprescott 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
79 changes: 79 additions & 0 deletions
79
packages/python-packages/api-stub-generator/apistub/nodes/_astroid_parser.py
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 |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import astroid | ||
| import inspect | ||
| from ._base_node import get_qualified_name | ||
| from ._argtype import ArgType | ||
|
|
||
|
|
||
| class AstroidArgumentParser: | ||
|
|
||
| def __init__(self, node: astroid.Arguments, namespace: str, func_node): | ||
| if not isinstance(node, astroid.Arguments): | ||
| raise TypeError("Can only pass in an astroid Arguments node.") | ||
| self._namespace = namespace | ||
| self._node = node | ||
| self._parent = func_node | ||
| self.args = {} | ||
| self.kwargs = {} | ||
| self.posargs = {} | ||
| self.varargs = None | ||
| self._parse_args() | ||
| self._parse_kwargs() | ||
| self._parse_posonly_args() | ||
| self._parse_varargs() | ||
|
|
||
| def _default_value(self, name): | ||
| try: | ||
| return self._node.default_value(name).as_string() | ||
| except astroid.NoDefault: | ||
| return inspect.Parameter.empty | ||
|
|
||
| def _argtype(self, name, idx, annotations, type_comments): | ||
| if annotations: | ||
| argtype = annotations[idx] | ||
| elif type_comments: | ||
| argtype = type_comments[idx] | ||
| else: | ||
| argtype = None | ||
| return get_qualified_name(argtype, self._namespace) if argtype else None | ||
|
|
||
| def _parse_args(self): | ||
| for (idx, arg) in enumerate(self._node.args): | ||
| name = arg.name | ||
| argtype = self._argtype(name, idx, self._node.annotations, self._node.type_comment_args) | ||
| default = self._default_value(name) | ||
| self.args[name] = ArgType(name, argtype=argtype, default=default, keyword=None, func_node=self._parent) | ||
|
|
||
| def _parse_kwargs(self): | ||
| for (idx, arg) in enumerate(self._node.kwonlyargs): | ||
| name = arg.name | ||
| argtype = self._argtype(name, idx, self._node.kwonlyargs_annotations, self._node.type_comment_kwonlyargs) | ||
| default = self._default_value(name) | ||
| self.kwargs[name] = ArgType(name, argtype=argtype, default=default, keyword="keyword", func_node=self._parent) | ||
| if self._node.kwarg: | ||
| kwarg_name = self._node.kwarg | ||
| if self._node.kwargannotation: | ||
| kwarg_type = self._node.kwargannotation.as_string() | ||
| else: | ||
| kwarg_type = None | ||
| # This wonky logic matches the existing code | ||
| arg = ArgType(kwarg_name, argtype=kwarg_type, default=inspect.Parameter.empty, keyword="keyword", func_node=self._parent) | ||
| arg.argname = f"**{kwarg_name}" | ||
| self.args[arg.argname] = arg | ||
|
|
||
| def _parse_posonly_args(self): | ||
| for (idx, arg) in enumerate(self._node.posonlyargs): | ||
| name = arg.name | ||
| argtype = self._argtype(name, idx, self._node.posonlyargs_annotations, self._node.type_comment_posonlyargs) | ||
| default = self._default_value(name) | ||
| self.posargs[name] = ArgType(name, argtype=argtype, default=default, keyword=None, func_node=self._parent) | ||
|
|
||
| def _parse_varargs(self): | ||
| if self._node.vararg: | ||
| name = self._node.vararg | ||
| if self._node.varargannotation: | ||
| argtype = self._node.varargannotation.as_string() | ||
| else: | ||
| argtype = None | ||
| arg = ArgType(name, argtype=argtype, default=inspect.Parameter.empty, keyword=None, func_node=self._parent) | ||
| arg.argname = f"*{name}" | ||
| self.args[name] = arg |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.