-
-
Notifications
You must be signed in to change notification settings - Fork 296
Create AssignName
nodes for ClassDef
and FunctionDef
#1390
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,9 @@ | |
""" | ||
|
||
import sys | ||
import token | ||
from io import StringIO | ||
from tokenize import generate_tokens | ||
from typing import ( | ||
TYPE_CHECKING, | ||
Callable, | ||
|
@@ -88,9 +91,13 @@ class TreeRebuilder: | |
"""Rebuilds the _ast tree to become an Astroid tree""" | ||
|
||
def __init__( | ||
self, manager: AstroidManager, parser_module: Optional[ParserModule] = None | ||
self, | ||
manager: AstroidManager, | ||
parser_module: Optional[ParserModule] = None, | ||
data: Optional[str] = None, | ||
): | ||
self._manager = manager | ||
self._data = data.split("\n") if data else None | ||
self._global_names: List[Dict[str, List[nodes.Global]]] = [] | ||
self._import_from_nodes: List[nodes.ImportFrom] = [] | ||
self._delayed_assattr: List[nodes.AssignAttr] = [] | ||
|
@@ -133,6 +140,45 @@ def _get_context( | |
) -> Context: | ||
return self._parser_module.context_classes.get(type(node.ctx), Context.Load) | ||
|
||
def _create_name_node( | ||
self, | ||
node: Union["ast.ClassDef", "ast.FunctionDef", "ast.AsyncFunctionDef"], | ||
parent: Union[nodes.ClassDef, nodes.FunctionDef, nodes.AsyncFunctionDef], | ||
) -> Optional[nodes.AssignName]: | ||
if not self._data: | ||
cdce8p marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return None | ||
end_lineno: Optional[int] = getattr(node, "end_lineno", None) | ||
if node.body: | ||
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. I will need to see how this interacts with 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.
|
||
end_lineno = node.body[0].lineno | ||
# pylint: disable-next=unsubscriptable-object | ||
data = "\n".join(self._data[node.lineno - 1 : end_lineno]) | ||
|
||
if isinstance(parent, nodes.FunctionDef): | ||
search_token_name = "def" | ||
else: | ||
search_token_name = "class" | ||
token_found: bool = False | ||
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. Is 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. Not necessary, I find it helpful never the less :) |
||
|
||
for t in generate_tokens(StringIO(data).readline): | ||
if token_found and t.type == token.NAME and t.string == node.name: | ||
break | ||
if t.type == token.NAME and t.string == search_token_name: | ||
token_found = True | ||
continue | ||
token_found = False | ||
else: | ||
return None | ||
|
||
# pylint: disable=undefined-loop-variable | ||
return nodes.AssignName( | ||
name=t.string, | ||
lineno=node.lineno - 1 + t.start[0], | ||
col_offset=t.start[1], | ||
end_lineno=node.lineno - 1 + t.end[0], | ||
end_col_offset=t.end[1], | ||
parent=parent, | ||
) | ||
|
||
def visit_module( | ||
self, node: "ast.Module", modname: str, modpath: str, package: bool | ||
) -> nodes.Module: | ||
|
@@ -1203,6 +1249,7 @@ def visit_classdef( | |
for kwd in node.keywords | ||
if kwd.arg != "metaclass" | ||
], | ||
name_node=self._create_name_node(node, newnode), | ||
) | ||
return newnode | ||
|
||
|
@@ -1551,6 +1598,7 @@ def _visit_functiondef( | |
returns=returns, | ||
type_comment_returns=type_comment_returns, | ||
type_comment_args=type_comment_args, | ||
name_node=self._create_name_node(node, newnode), | ||
) | ||
self._global_names.pop() | ||
return newnode | ||
|
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.
According to the typing you added in
astroid/builder.py
this should bestr
right? I don't see any other place whereTreeRebuilder
is instantiated.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.
True, inside
astroid
it will always bestr
. Technically, you could considerTreeRebuilder
part of the public interface however. Adding a new required argument would then be a breaking change.Defining it as
Optional
isn't too bad considering everything. Thename_node
/position
needs to beOptional
, so a small checkif not self._data
doesn't really hurt.