summaryrefslogtreecommitdiff
path: root/astroid/nodes
diff options
context:
space:
mode:
authorDaniël van Noord <13665637+DanielNoord@users.noreply.github.com>2023-04-03 22:35:37 +0200
committerDaniël van Noord <13665637+DanielNoord@users.noreply.github.com>2023-04-03 22:53:40 +0200
commite13e1770e3cb915da270261d185ec235928e2124 (patch)
tree14dcc944f34fb3ebde583d288c5921fdcae4577d /astroid/nodes
parentf70ad92f3eb235d80e65a5b0492892e7868c096c (diff)
downloadastroid-git-e13e1770e3cb915da270261d185ec235928e2124.tar.gz
Fix ``lineno`` API on nodes
Diffstat (limited to 'astroid/nodes')
-rw-r--r--astroid/nodes/node_classes.py4
-rw-r--r--astroid/nodes/node_ng.py19
-rw-r--r--astroid/nodes/scoped_nodes/scoped_nodes.py18
3 files changed, 27 insertions, 14 deletions
diff --git a/astroid/nodes/node_classes.py b/astroid/nodes/node_classes.py
index 93c8d47e..81d7658a 100644
--- a/astroid/nodes/node_classes.py
+++ b/astroid/nodes/node_classes.py
@@ -699,10 +699,10 @@ class Arguments(_base_nodes.AssignTypeNode):
return None
@cached_property
- def fromlineno(self):
+ def fromlineno(self) -> int:
"""The first line that this node appears on in the source code.
- :type: int or None
+ Can also return 0 if the line can not be determined.
"""
lineno = super().fromlineno
return max(lineno, self.parent.fromlineno or 0)
diff --git a/astroid/nodes/node_ng.py b/astroid/nodes/node_ng.py
index e21a2808..64611aba 100644
--- a/astroid/nodes/node_ng.py
+++ b/astroid/nodes/node_ng.py
@@ -446,15 +446,21 @@ class NodeNG:
# single node, and they rarely get looked at
@cached_property
- def fromlineno(self) -> int | None:
- """The first line that this node appears on in the source code."""
+ def fromlineno(self) -> int:
+ """The first line that this node appears on in the source code.
+
+ Can also return 0 if the line can not be determined.
+ """
if self.lineno is None:
return self._fixed_source_line()
return self.lineno
@cached_property
- def tolineno(self) -> int | None:
- """The last line that this node appears on in the source code."""
+ def tolineno(self) -> int:
+ """The last line that this node appears on in the source code.
+
+ Can also return 0 if the line can not be determined.
+ """
if self.end_lineno is not None:
return self.end_lineno
if not self._astroid_fields:
@@ -466,10 +472,11 @@ class NodeNG:
return self.fromlineno
return last_child.tolineno
- def _fixed_source_line(self) -> int | None:
+ def _fixed_source_line(self) -> int:
"""Attempt to find the line that this node appears on.
We need this method since not all nodes have :attr:`lineno` set.
+ Will return 0 if the line number can not be determined.
"""
line = self.lineno
_node = self
@@ -482,7 +489,7 @@ class NodeNG:
while parent and line is None:
line = parent.lineno
parent = parent.parent
- return line
+ return line or 0
def block_range(self, lineno):
"""Get a range from the given line number to where this node ends.
diff --git a/astroid/nodes/scoped_nodes/scoped_nodes.py b/astroid/nodes/scoped_nodes/scoped_nodes.py
index 17da7537..018da851 100644
--- a/astroid/nodes/scoped_nodes/scoped_nodes.py
+++ b/astroid/nodes/scoped_nodes/scoped_nodes.py
@@ -1531,8 +1531,11 @@ class FunctionDef(_base_nodes.MultiLineBlockNode, _base_nodes.Statement, Lambda)
return type_name
@cached_property
- def fromlineno(self) -> int | None:
- """The first line that this node appears on in the source code."""
+ def fromlineno(self) -> int:
+ """The first line that this node appears on in the source code.
+
+ Can also return 0 if the line can not be determined.
+ """
# lineno is the line number of the first decorator, we want the def
# statement lineno. Similar to 'ClassDef.fromlineno'
lineno = self.lineno
@@ -1541,7 +1544,7 @@ class FunctionDef(_base_nodes.MultiLineBlockNode, _base_nodes.Statement, Lambda)
node.tolineno - node.lineno + 1 for node in self.decorators.nodes
)
- return lineno
+ return lineno or 0
@cached_property
def blockstart_tolineno(self):
@@ -2152,8 +2155,11 @@ class ClassDef(
)
@cached_property
- def fromlineno(self) -> int | None:
- """The first line that this node appears on in the source code."""
+ def fromlineno(self) -> int:
+ """The first line that this node appears on in the source code.
+
+ Can also return 0 if the line can not be determined.
+ """
if not PY38_PLUS or IS_PYPY and PY38 and not PYPY_7_3_11_PLUS:
# For Python < 3.8 the lineno is the line number of the first decorator.
# We want the class statement lineno. Similar to 'FunctionDef.fromlineno'
@@ -2164,7 +2170,7 @@ class ClassDef(
node.tolineno - node.lineno + 1 for node in self.decorators.nodes
)
- return lineno
+ return lineno or 0
return super().fromlineno
@cached_property