Skip to content

Fix ClassDef.fromlineno for Python < 3.8 #1395

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 5 commits into from
Feb 19, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
3 changes: 3 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ Release date: TBA

Closes #1085

* Fix ``ClassDef.fromlineno``. For Python < 3.8 the ``lineno`` attribute includes decorators.
``fromlineno`` should return the line of the ``class`` statement itself.

What's New in astroid 2.9.4?
============================
Release date: TBA
Expand Down
26 changes: 19 additions & 7 deletions astroid/nodes/scoped_nodes/scoped_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
from astroid import bases
from astroid import decorators as decorators_mod
from astroid import mixins, util
from astroid.const import PY39_PLUS
from astroid.const import PY38_PLUS, PY39_PLUS
from astroid.context import (
CallContext,
InferenceContext,
Expand Down Expand Up @@ -1706,13 +1706,10 @@ def type(
return type_name

@decorators_mod.cachedproperty
def fromlineno(self):
"""The first line that this node appears on in the source code.

:type: int or None
"""
def fromlineno(self) -> Optional[int]:
"""The first line that this node appears on in the source code."""
# lineno is the line number of the first decorator, we want the def
# statement lineno
# statement lineno. Similar to 'ClassDef.fromlineno'
lineno = self.lineno
if self.decorators is not None:
lineno += sum(
Expand Down Expand Up @@ -2297,6 +2294,21 @@ def _newstyle_impl(self, context=None):
doc=("Whether this is a new style class or not\n\n" ":type: bool or None"),
)

@decorators_mod.cachedproperty
def fromlineno(self) -> Optional[int]:
"""The first line that this node appears on in the source code."""
if not PY38_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'
lineno = self.lineno
if self.decorators is not None:
lineno += sum(
node.tolineno - node.lineno + 1 for node in self.decorators.nodes
)

return lineno
return super().fromlineno

@decorators_mod.cachedproperty
def blockstart_tolineno(self):
"""The line on which the beginning of this block ends.
Expand Down
45 changes: 44 additions & 1 deletion tests/unittest_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import os
import socket
import sys
import textwrap
import unittest

import pytest
Expand Down Expand Up @@ -132,12 +133,54 @@ def function(
__name__,
)
function = astroid["function"]
# XXX discussable, but that's what is expected by pylint right now
# XXX discussable, but that's what is expected by pylint right now, similar to ClassDef
self.assertEqual(function.fromlineno, 3)
self.assertEqual(function.tolineno, 5)
self.assertEqual(function.decorators.fromlineno, 2)
self.assertEqual(function.decorators.tolineno, 2)

@staticmethod
def test_decorated_class_lineno() -> None:
code = textwrap.dedent(
"""
class A:
...

@decorator
class B:
...

@deco1
@deco2(
var=42
)
class C:
...
"""
)

ast_module: nodes.Module = builder.parse(code) # type: ignore[assignment]

a = ast_module.body[0]
assert isinstance(a, nodes.ClassDef)
assert a.fromlineno == 2
assert a.tolineno == 3

b = ast_module.body[1]
assert isinstance(b, nodes.ClassDef)
assert b.fromlineno == 6
assert b.tolineno == 7

c = ast_module.body[2]
assert isinstance(c, nodes.ClassDef)
if not PY38_PLUS:
# Not perfect, but best we can do for Python 3.7
# Can't detect closing bracket on new line.
assert c.fromlineno == 12
else:
assert c.fromlineno == 13
assert c.tolineno == 14

def test_class_lineno(self) -> None:
stmts = self.astroid.body
# on line 20:
Expand Down