◐ Shell
clean mode source ↗

bpo-33211: Add def_lineno and class_lineno attributes to FunctionDef and ClassDef by emmatyping · Pull Request #6410 · python/cpython

As reported in the bug, a decorated function or class like below will report the first line of the decorator as its line number (so the FunctionDef.lineno will be 1).

@decorate(
        "multi",
        "line"
        )
def foo(
        a,
        b,
        ):
        pass

This change adds a def_lineno for functions and class_lineno for classes. These attributes report the real line numbers of the beginning of the function or class definition. This is especially valuable for linters and tools parsing Python.

I wasn't sure where to put something in the docs since the ast docs are sparse.

This isn't quite ready yet, I need to figure out what changes I need to make to the unparser (which I will do tomorrow).

https://bugs.python.org/issue33211