You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
inspect.BlockFinder handles tokenize.NEWLINE too strictly #139783
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.
changed the title [-]`inspect.getsourcelines` incomplete when called on decorated function with a comment[/-][+]`inspect.BlockFinder` handles `tokenize.NEWLINE` too strictly[/+]on Oct 8, 2025
So it's not only comments that are poorly handled, it's anything that "breaks" the "prelude" of the function (that is decorators). I'm not entirely sure we can do much thing here without adding a layer of complexity. It shouldn't be possible to say that the source lines of something only contains decorators or new lines/comments as this is not valid Python code. So, we should do something else.
It would be quite easy to actually preprocess the source and eliminate "un-necessary" lines but then we'll need to handle the offset properly. In a single pass, we'll need to determine whether the block ended correctly or not.
Similarly:
@\dummydeff():
return1
This is equivalent to use @dummy but again, the source lines will only consist of dummy and nothing else! But this is a separate issue actually, where findsource already struggles with that (it will not put @ and dummy together...)
I am a bit time constrained so I would appreciate your help especially if it is something you recently worked on (I only skimmed through the implementation)
Activity
picnixz commentedon Oct 8, 2025
I'll have a look at this.
picnixz commentedon Oct 8, 2025
The issue is with the BlockFinder which, because of the comment, considers that the body is not part of the function anymore.
[-]`inspect.getsourcelines` incomplete when called on decorated function with a comment[/-][+]`inspect.BlockFinder` handles `tokenize.NEWLINE` too strictly[/+]picnixz commentedon Oct 8, 2025
Similar issues:
So it's not only comments that are poorly handled, it's anything that "breaks" the "prelude" of the function (that is decorators). I'm not entirely sure we can do much thing here without adding a layer of complexity. It shouldn't be possible to say that the source lines of something only contains decorators or new lines/comments as this is not valid Python code. So, we should do something else.
It would be quite easy to actually preprocess the source and eliminate "un-necessary" lines but then we'll need to handle the offset properly. In a single pass, we'll need to determine whether the block ended correctly or not.
Similarly:
This is equivalent to use
@dummybut again, the source lines will only consist ofdummyand nothing else! But this is a separate issue actually, wherefindsourcealready struggles with that (it will not put@anddummytogether...)picnixz commentedon Oct 8, 2025
I need to go now but I'll try to do something tomorrow or later this week. Thanks for reporting this!
BabakkGraphcore commentedon Oct 8, 2025
Great, thanks for picking it up so quickly1
[NFC] Python3.13 fix (#8403)
serhiy-storchaka commentedon Oct 9, 2025
I worked with that code recently, so if you need help, I can help.
picnixz commentedon Oct 9, 2025
I am a bit time constrained so I would appreciate your help especially if it is something you recently worked on (I only skimmed through the implementation)
albertvillanova commentedon Oct 9, 2025
I confirm this issue and I give some information about severity and impact.
Additional Impact: Breaking PyTorch JIT + HuggingFace Transformers
This bug is affecting production code in the PyTorch and HuggingFace ecosystem.
Real-world Example
The HuggingFace
transformerslibrary contains multiple instances of this pattern. For example, intransformers/models/deberta_v2/modeling_deberta_v2.py(lines 105-108): https://github.com/huggingface/transformers/blob/be3fa93b298eec4182a0bb6c5e64270a5f1e8020/src/transformers/models/deberta_v2/modeling_deberta_v2.py#L105-L108When this module is imported on Python 3.13.8, the @torch.jit.script decorator calls inspect.getsourcelines(), which returns only:
PyTorch then tries to parse this truncated source with ast.parse(), which fails with:
Root Cause Confirmed
I traced through the tokenization and can confirm the issue is in BlockFinder.tokeneater():
Regression Confirmation
✅ Python 3.13.7: Works correctly
❌ Python 3.13.8: Broken
This appears to be a side effect of the fix for gh-137477 (generator expressions):
inspect.getsourcelines()mishandles multi-line generator expressions #13747724 remaining items
picnixz commentedon Oct 25, 2025
Could you ban the user above for spam @python/organization-owners? TiA.
[NFC] Python3.13 fix (triton-lang#8403)
[triton][beta] [Cherry-pick] '[NFC] Python3.13 fix (#8403)'
Work around CPython 3.13.8 inspect.getsourcelines() bug in _overload_…
Work around CPython 3.13.8 inspect.getsourcelines() bug in _overload_…
[JIT] Silence CPython 3.13.8 inspect.getsourcelines() bug (#179066)
[JIT] Silence CPython 3.13.8 inspect.getsourcelines() bug (#179066)
[JIT] Silence CPython 3.13.8 inspect.getsourcelines() bug (pytorch#17…
[JIT] Silence CPython 3.13.8 inspect.getsourcelines() bug (pytorch#17…
python-torch: fix import failure caused by cpython 3.13.8 inspect bug
[JIT] Silence CPython 3.13.8 inspect.getsourcelines() bug (pytorch#17…
[JIT] Silence CPython 3.13.8 inspect.getsourcelines() bug (pytorch#17…
[NFC] Python3.13 fix (#8403)
Ask uv for a Python that can import torch, and skip the one that cannot
Ask uv for a Python that can import torch, and skip the one that cann…