Skip to content
Open
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
26 changes: 16 additions & 10 deletions lib/astunparse/unparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ def interleave(inter, f, seq):
inter()
f(x)

class Unparser:
class Unparser(object):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is apparently change unrelated to the functionality provided.

"""Methods in this class recursively traverse an AST and
output source code for the abstract syntax; original formatting
is disregarded. """

def __init__(self, tree, file=sys.stdout):
def __init__(self, tree, file=sys.stdout, version_info=None):
"""Unparser(tree, file=sys.stdout) -> None.
Print the source for tree to file."""
self.version_info = version_info or sys.version_info
self.f = file
self.future_imports = []
self._indent = 0
Expand Down Expand Up @@ -207,7 +208,7 @@ def _YieldFrom(self, t):

def _Raise(self, t):
self.fill("raise")
if six.PY3:
if self.version_info[0] == 3:
if not t.exc:
assert not t.cause
return
Expand Down Expand Up @@ -281,7 +282,7 @@ def _ExceptHandler(self, t):
self.dispatch(t.type)
if t.name:
self.write(" as ")
if six.PY3:
if isinstance(t.name, six.string_types):
self.write(t.name)
else:
self.dispatch(t.name)
Expand All @@ -295,7 +296,7 @@ def _ClassDef(self, t):
self.fill("@")
self.dispatch(deco)
self.fill("class "+t.name)
if six.PY3:
if self.version_info[0] == 3:
self.write("(")
comma = False
for e in t.bases:
Expand All @@ -306,7 +307,7 @@ def _ClassDef(self, t):
if comma: self.write(", ")
else: comma = True
self.dispatch(e)
if sys.version_info[:2] < (3, 5):
if self.version_info[:2] < (3, 5):
if t.starargs:
if comma: self.write(", ")
else: comma = True
Expand Down Expand Up @@ -427,7 +428,7 @@ def _Bytes(self, t):
self.write(repr(t.s))

def _Str(self, tree):
if six.PY3:
if self.version_info[0] == 3:
self.write(repr(tree.s))
else:
# if from __future__ import unicode_literals is in effect,
Expand Down Expand Up @@ -501,7 +502,7 @@ def _Repr(self, t):

def _Num(self, t):
repr_n = repr(t.n)
if six.PY3:
if self.version_info[0] == 3:
self.write(repr_n.replace("inf", INFSTR))
else:
# Parenthesize negative numbers, to avoid turning (-1)**2 into -1**2.
Expand Down Expand Up @@ -608,7 +609,7 @@ def _UnaryOp(self, t):
self.write("(")
self.write(self.unop[t.op.__class__.__name__])
self.write(" ")
if six.PY2 and isinstance(t.op, ast.USub) and isinstance(t.operand, ast.Num):
if self.version_info[0] == 2 and isinstance(t.op, ast.USub) and isinstance(t.operand, ast.Num):
# If we're applying unary minus to a number, parenthesize the number.
# This is necessary: -2147483648 is different from -(2147483648) on
# a 32-bit machine (the first is an int, the second a long), and
Expand Down Expand Up @@ -671,7 +672,7 @@ def _Call(self, t):
if comma: self.write(", ")
else: comma = True
self.dispatch(e)
if sys.version_info[:2] < (3, 5):
if self.version_info[:2] < (3, 5):
if t.starargs:
if comma: self.write(", ")
else: comma = True
Expand Down Expand Up @@ -770,6 +771,11 @@ def _arguments(self, t):
if t.kwarg.annotation:
self.write(": ")
self.dispatch(t.kwarg.annotation)
elif hasattr(t.kwarg, 'id'):
self.write("**"+t.kwarg.id)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spaces around + ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I agree that would be better style, it's better to remain consistent with the rest of the file.
Other commits can improve the style globally if it's desired.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's better to remain consistent with the rest of the file.

Absolutely! I actually went and check how unparser.py has it, and in immediate vicinity of your changes I saw this: https://github.com/simonpercivall/astunparse/blob/master/lib/astunparse/unparser.py#L763 , that's why I made a comment. Looking in more detail, I see cases like https://github.com/simonpercivall/astunparse/blob/master/lib/astunparse/unparser.py#L804 , https://github.com/simonpercivall/astunparse/blob/master/lib/astunparse/unparser.py#L809 - so yes, your change looks good wrt to them.

Other commits can improve the style globally if it's desired.

I'm sure that falls on maintainer's plate. (I'm just a random passer-by who spotted an issue with the lib: #39, and proceeded to check how active maintenance of this repo by looking at pending PRs, then figuring I could benefit from this PR too. Oh, btw, ping @simonpercivall ;-) ).

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Off-topic: I'm pretty bad at maintaining this repository :/

Re. style: The style (and code) line-by-line should be kept as near identical as possible to the different versions of Tools/parser/unparse.py in the official cpython source tree.

if t.kwarg.annotation:
self.write(": ")
self.dispatch(t.kwarg.annotation)
else:
self.write("**"+t.kwarg)
if getattr(t, 'kwargannotation', None):
Expand Down