Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

bpo-33211: Add def_lineno and class_lineno attributes to FunctionDef and ClassDef#6410

Closed
emmatyping wants to merge 5 commits into
python:masterpython/cpython:masterfrom
emmatyping:decorlinenoemmatyping/cpython:decorlinenoCopy head branch name to clipboard
Closed

bpo-33211: Add def_lineno and class_lineno attributes to FunctionDef and ClassDef#6410
emmatyping wants to merge 5 commits into
python:masterpython/cpython:masterfrom
emmatyping:decorlinenoemmatyping/cpython:decorlinenoCopy head branch name to clipboard

Conversation

@emmatyping

@emmatyping emmatyping commented Apr 7, 2018

Copy link
Copy Markdown
Member

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

Decorated functions and classes report the line of the first decorator
as their lineno. This can be misleading and confusing, especially if the
decorator spans multiple lines.
@emmatyping

Copy link
Copy Markdown
Member Author

I'm abandoning this in favor of a better solution.

@emmatyping emmatyping closed this Apr 12, 2018
@emmatyping emmatyping deleted the decorlineno branch April 12, 2018 22:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.