Incorrect line numbers for decorated functions

Bug #879945 reported by Wilfred Hughes
24
This bug affects 3 people
Affects Status Importance Assigned to Milestone
Pyflakes
New
Wishlist
Unassigned

Bug Description

In the code:

def decorator(func):
    return func

@decorator
def defined_twice():
    pass

@decorator
def defined_twice():
    pass

Pyflakes gives the error: twice.py:10: redefinition of function 'defined_twice' from line 5

It ought to be: twice.py:11: redefinition of function 'defined_twice' from line 6

Revision history for this message
Jean-Paul Calderone (exarkun) wrote :

Why? The Python AST makes the decision that the first line of a function is the first decorator that applies to it. Pyflakes just propagates this information.

Revision history for this message
Wilfred Hughes (yatc18ks0-9a-ngeefk4xa) wrote :

The Python AST gives the line number where the function definition starts, which includes the decorator. However, the fault isn't on that line and the user cannot fix the issue by changing that line.

Given a FunctionDef node:

node.lineno # where the function definition starts
node.lineno + len(node.decorator_list) # where the function name is

Phil Frost (bitglue)
Changed in pyflakes:
importance: Undecided → Wishlist
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Duplicates of this bug

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.