Bug report
Bug description:
In python 3.13.8 calling inspect.getsourcelines on a decorated function with a comment between the decorator and the name only returns the function declaration, the body is skipped.
import inspect def dummy(fn): return fn @dummy def test1(a, b): return a @dummy #comment def test2(a, b): return a print(inspect.getsourcelines(test1)) print("------------------") print(inspect.getsourcelines(test2))
returns
(['@dummy\n', 'def test1(a, b):\n', ' return a\n'], 6)
------------------
(['@dummy\n', '#comment\n', 'def test2(a, b):\n'], 10)
the source of test2 is incomplete and missing the return statement.
CPython versions tested on:
3.13
Operating systems tested on:
Linux
Linked PRs
- gh-139783: Fix inspect.getsourcelines() for the case when a decorator is followed by a comment or an empty line #139836
- [3.14] gh-139783: Fix inspect.getsourcelines() for the case when a decorator is followed by a comment or an empty line (GH-139836) #139889
- [3.13] gh-139783: Fix inspect.getsourcelines() for the case when a decorator is followed by a comment or an empty line (GH-139836) #139890