summaryrefslogtreecommitdiff
path: root/astroid/rebuilder.py
diff options
context:
space:
mode:
authorClaudiu Popa <pcmanticore@gmail.com>2019-06-04 08:10:09 +0200
committerClaudiu Popa <pcmanticore@gmail.com>2019-06-04 08:10:09 +0200
commit981a13962a0d3b2ca359d920dc94530650c15785 (patch)
treed4636a3016504fc23f153c542ad0379d63f4f528 /astroid/rebuilder.py
parent0a0c5c91e2e516869b7014ab3be72285e04ee688 (diff)
downloadastroid-git-981a13962a0d3b2ca359d920dc94530650c15785.tar.gz
Maintain the same line number for decorators for Python 3.8+
Diffstat (limited to 'astroid/rebuilder.py')
-rw-r--r--astroid/rebuilder.py12
1 files changed, 8 insertions, 4 deletions
diff --git a/astroid/rebuilder.py b/astroid/rebuilder.py
index 64485eec..51fa0ab8 100644
--- a/astroid/rebuilder.py
+++ b/astroid/rebuilder.py
@@ -460,7 +460,12 @@ class TreeRebuilder:
"""visit a Decorators node by returning a fresh instance of it"""
# /!\ node is actually a _ast.FunctionDef node while
# parent is an astroid.nodes.FunctionDef node
- newnode = nodes.Decorators(node.lineno, node.col_offset, parent)
+ if PY38:
+ # Set the line number of the first decorator for Python 3.8+.
+ lineno = node.decorator_list[0].lineno
+ else:
+ lineno = node.lineno
+ newnode = nodes.Decorators(lineno, node.col_offset, parent)
newnode.postinit([self.visit(child, newnode) for child in node.decorator_list])
return newnode
@@ -583,7 +588,7 @@ class TreeRebuilder:
node, doc = self._get_doc(node)
lineno = node.lineno
- if PY38:
+ if PY38 and node.decorator_list:
# Python 3.8 sets the line number of a decorated function
# to be the actual line number of the function, but the
# previous versions expected the decorator's line number instead.
@@ -591,8 +596,7 @@ class TreeRebuilder:
# first decorator to maintain backward compatibility.
# It's not ideal but this discrepancy was baked into
# the framework for *years*.
- if node.decorator_list:
- lineno = node.decorator_list[0].lineno
+ lineno = node.decorator_list[0].lineno
newnode = cls(node.name, doc, lineno, node.col_offset, parent)
if node.decorator_list: