8000 bpo-39411: pyclbr rewrite on AST by isidentical · Pull Request #18103 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-39411: pyclbr rewrite on AST #18103

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 10 commits into from
Nov 11, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
apply Pablo's suggestions
  • Loading branch information
isidentical committed Jan 21, 2020
commit f3ff250a79c034db04b466a5ba281d291e77c85a
2 changes: 1 addition & 1 deletion Doc/library/pyclbr.rst
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ statements. They have the following attributes:

.. attribute:: Function.parent

For functions that are defined with ``async`` prefix, ``True``.
``True`` for functions that are defined with the ``async`` prefix, ``False`` otherwise.

.. versionadded:: 3.9

Expand Down
26 changes: 15 additions & 11 deletions Lib/pyclbr.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

Instances of Function describe functions with the attributes from _Object,
plus the following:
is_async -- if function defined with 'async' prefix
is_async -- if a function is defined with an 'async' prefix

Instances of Class describe classes with the attributes from _Object,
plus the following:
Expand Down Expand Up @@ -188,8 +188,7 @@ def visit_ClassDef(self, node):
elif len(names := name.split(".")) > 1:
# Super class form is module.class:
# look in module for class.
module = names[-2]
class_ = names[-1]
*_, module, class_ = names
if module in _modules:
bases.append(_modules[module].get(class_) or name)
else:
Expand All @@ -206,14 +205,7 @@ def visit_ClassDef(self, node):
self.stack.pop()

def visit_Assign(self, node):
if (
not len(node.targets) == 1
or not len(self.stack) > 0
or not isinstance(self.stack[-1], Class)
or not isinstance(node.targets[0], ast.Name)
or not isinstance(node.value, ast.Name)
or not isinstance(self.tree.get(node.value.id), Function)
):
if not self.single_target_function_assign(node):
return

name = node.targets[0].id
Expand All @@ -222,6 +214,18 @@ def visit_Assign(self, node):
self.stack[-1]._addchild(name, child)
self.stack[-1]._addmethod(name, node.lineno)

def single_target_function_assign(self, node):
"""Check if given assignment consists from a single target
and single value within a class namespace. Check value for if it
is an already defined function."""

return (len(node.targets) == 1
and len(self.stack) > 0
and isinstance(self.stack[-1], Class)
and isinstance(node.targets[0], ast.Name)
and isinstance(node.value, ast.Name)
and isinstance(self.tree.get(node.value.id), Function))

def visit_FunctionDef(self, node, *, is_async=True):
parent = self.stack[-1] if self.stack else None
function = Function(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
Rewrite :mod:`pyclbr` with the support of abstract syntax trees. Add an
``is_async`` identifier to :mod:`pyclbr`'s ``Function`` objects. Patch by
Batuhan Taskaya
Add an ``is_async`` identifier to :mod:`pyclbr`'s ``Function`` objects.
Patch by Batuhan Taskaya
0