Title: Misbehavior of `f.__code__.co_firstlineno` for decorated functions · Issue #139838 · python/cpython · GitHub
Open Graph Title: Misbehavior of `f.__code__.co_firstlineno` for decorated functions · Issue #139838 · python/cpython
X Title: Misbehavior of `f.__code__.co_firstlineno` for decorated functions · Issue #139838 · python/cpython
Description: Bug report Bug description: See also: #139783 Compare: dummy = lambda f: f # L1 @dummy # L2 def f(): # L3 return # L4 print(f.__code__.co_firstlineno) # L5 # result = 2 with dummy = lambda f: f # L1 pass # L2 def f(): # L3 return # L4 pr...
Open Graph Description: Bug report Bug description: See also: #139783 Compare: dummy = lambda f: f # L1 @dummy # L2 def f(): # L3 return # L4 print(f.__code__.co_firstlineno) # L5 # result = 2 with dummy = lambda f: f # L...
X Description: Bug report Bug description: See also: #139783 Compare: dummy = lambda f: f # L1 @dummy # L2 def f(): # L3 return # L4 print(f.__code__.co_firstlineno) # L5 # result = 2 with dummy = lambda f: f # L...
Opengraph URL: https://github.com/python/cpython/issues/139838
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Misbehavior of `f.__code__.co_firstlineno` for decorated functions","articleBody":"# Bug report\n\n### Bug description:\n\nSee also: #139783\n\nCompare:\n\n```py\ndummy = lambda f: f # L1\n@dummy # L2\ndef f(): # L3\n return # L4\nprint(f.__code__.co_firstlineno) # L5 # result = 2\n```\n\nwith\n\n```py\ndummy = lambda f: f # L1\npass # L2\ndef f(): # L3\n return # L4\nprint(f.__code__.co_firstlineno) # L5 # result = 3\n```\n\nand\n\n```py\ndummy = lambda f: f # L1\n@\\\ndummy # L3\ndef f(): # L4\n return # L5\nprint(f.__code__.co_firstlineno) # L6 # result = 3\n```\n\nIn the last example, the line continuation after `@` is not handled correctly and the first line for `f.__code__` then becomes the line at dummy. Because of that, the values returned by `inspect.findsource(f)` will not be consistent and `inspect.getsourcelines` will not use the correct source (it will only start at \"dummy\" and will not contain the \"@\\\" part).\n\n@serhiy-storchaka suggested that this is an issue in the AST parser. I however don't know how we should actually fix this.\n\n### Consequences to `inspect.getsource` and other introspection functions:\n\n```python\nimport inspect\n\ndef outer(*args, **kwargs):\n def inner(f):\n return f\n return deco\n\n@(\n outer(\n ...\n )\n)\ndef f():\n return \"hello\"\n\nprint(inspect.getsource(f))\n```\n\nOn Python 3.12, the output is expected:\n\n```py\n@(\n outer(\n ...\n )\n)\ndef func():\n return \"hello\"\n```\n\nOn Python 3.13.7, the output is missing the first line `@(`:\n\n```py\n outer(\n ...\n )\n)\ndef func():\n return \"hello\"\n```\n\nOn Python 3.13.9 and `main`, we are missing `@` and outer parentheses:\n\n```py\n outer(\n ...\n )\n```\n\n\n### CPython versions tested on:\n\nCPython main branch\n\n### Operating systems tested on:\n\n_No response_","author":{"url":"https://github.com/picnixz","@type":"Person","name":"picnixz"},"datePublished":"2025-10-09T11:13:28.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":7},"url":"https://github.com/139838/cpython/issues/139838"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:eb5edebf-95a7-5326-9334-891facc615c1 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E04C:18AAA8:E870BC:13FF7F0:6A53B7E5 |
| html-safe-nonce | c89ce3436681d8b817914fe5177fd37f9bcbc9dd5d033260978b8a6e7847d6ea |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFMDRDOjE4QUFBODpFODcwQkM6MTNGRjdGMDo2QTUzQjdFNSIsInZpc2l0b3JfaWQiOiI4NjYwODg2Mzk1ODI2ODEwNjEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 4860658e2bd4fc93a868a4b719b0a94fd7c54878727bf655daa5e6505d79b40f |
| hovercard-subject-tag | issue:3498710308 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/139838/issue_layout |
| twitter:image | https://opengraph.githubassets.com/105bddf60b86e0ef00231c75b315948e2d1f1b14fe61b4071fa2c2fd7d6c62be/python/cpython/issues/139838 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/105bddf60b86e0ef00231c75b315948e2d1f1b14fe61b4071fa2c2fd7d6c62be/python/cpython/issues/139838 |
| og:image:alt | Bug report Bug description: See also: #139783 Compare: dummy = lambda f: f # L1 @dummy # L2 def f(): # L3 return # L4 print(f.__code__.co_firstlineno) # L5 # result = 2 with dummy = lambda f: f # L... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | picnixz |
| hostname | github.com |
| expected-hostname | github.com |
| None | b9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 07a982c1d40157c619b364352b704c3ce66bb332 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width