Skip to content

Commit e8ef508

Browse files
committed
fix alternative decorator macro args syntax (parens) in letdoutil
Needed up to Python 3.8, since decorators can be sliced only in Python 3.9+.
1 parent c54ac72 commit e8ef508

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

unpythonic/syntax/letdoutil.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,14 @@ def islet(tree, expanded=True):
116116
# dlet[(k0, v0), ...] (usually in a decorator list)
117117
deconames = ("dlet", "dletseq", "dletrec",
118118
"blet", "bletseq", "bletrec")
119-
if type(tree) is Subscript and type(tree.value) is Name:
119+
if type(tree) is Subscript and type(tree.value) is Name: # could be a Subscript decorator (Python 3.9+)
120120
s = tree.value.id
121121
if any(s == x for x in deconames):
122122
return ("decorator", s)
123+
if type(tree) is Call and type(tree.func) is Name: # up to Python 3.8: parenthesis syntax for decorator macros
124+
s = tree.func.id
125+
if any(s == x for x in deconames):
126+
return ("decorator", s)
123127
# otherwise we should have an expr macro invocation
124128
if not type(tree) is Subscript:
125129
return False
@@ -347,6 +351,9 @@ def _theexpr_ref(self):
347351
def _getbindings(self):
348352
t = self._type
349353
if t == "decorator": # bare Subscript, dlet[...], blet[...]
354+
if type(self._tree) is Call: # up to Python 3.8: parenthesis syntax for decorator macros
355+
return canonize_bindings(self._tree.args, self._tree)
356+
# Subscript as decorator (Python 3.9+)
350357
if sys.version_info >= (3, 9, 0): # Python 3.9+: the Index wrapper is gone.
351358
theargs = self._tree.slice
352359
else:
@@ -367,6 +374,9 @@ def _getbindings(self):
367374
def _setbindings(self, newbindings):
368375
t = self._type
369376
if t == "decorator":
377+
if type(self._tree) is Call: # up to Python 3.8: parenthesis syntax for decorator macros
378+
self._tree.args = newbindings
379+
# Subscript as decorator (Python 3.9+)
370380
if sys.version_info >= (3, 9, 0): # Python 3.9+: the Index wrapper is gone.
371381
self._tree.slice.elts = newbindings
372382
else:

0 commit comments

Comments
 (0)