From c4c8d7c32a3aeb6b4c54f2615b2230dfad6e9f5b Mon Sep 17 00:00:00 2001 From: Aaron Wieczorek Date: Tue, 30 Dec 2025 18:46:09 +0000 Subject: [PATCH 1/2] Doc: Clarify inspect.getsource behavior regarding shared lines --- Doc/library/inspect.rst | 6 ++++++ .../2025-12-30-18-45-07.gh-issue-143299.cNOlMT.rst | 1 + 2 files changed, 7 insertions(+) create mode 100644 Misc/NEWS.d/next/Documentation/2025-12-30-18-45-07.gh-issue-143299.cNOlMT.rst diff --git a/Doc/library/inspect.rst b/Doc/library/inspect.rst index e5abd68f03b9c3..230896bc7f37fa 100644 --- a/Doc/library/inspect.rst +++ b/Doc/library/inspect.rst @@ -698,6 +698,12 @@ Retrieving source code A :exc:`TypeError` is raised if the object is a built-in module, class, or function. + .. note:: + This function returns the *lines* of source code where the object is + defined. If multiple objects are defined on the same line (e.g. + multiple lambdas), or if the object is embedded in a larger statement + (e.g. a list comprehension), the function will return the entire line(s). + .. versionchanged:: 3.3 :exc:`OSError` is raised instead of :exc:`IOError`, now an alias of the former. diff --git a/Misc/NEWS.d/next/Documentation/2025-12-30-18-45-07.gh-issue-143299.cNOlMT.rst b/Misc/NEWS.d/next/Documentation/2025-12-30-18-45-07.gh-issue-143299.cNOlMT.rst new file mode 100644 index 00000000000000..904e53b11a986f --- /dev/null +++ b/Misc/NEWS.d/next/Documentation/2025-12-30-18-45-07.gh-issue-143299.cNOlMT.rst @@ -0,0 +1 @@ +Doc: Clarify inspect.getsource behavior regarding shared lines (GH-143299) From 177a0403391a4429de6b7df29075d83c82f5155c Mon Sep 17 00:00:00 2001 From: Aaron Wieczorek Date: Tue, 30 Dec 2025 19:17:36 +0000 Subject: [PATCH 2/2] Doc: Simplify getsource note per review feedback --- Doc/library/inspect.rst | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Doc/library/inspect.rst b/Doc/library/inspect.rst index 230896bc7f37fa..af27fd41503394 100644 --- a/Doc/library/inspect.rst +++ b/Doc/library/inspect.rst @@ -699,10 +699,9 @@ Retrieving source code function. .. note:: - This function returns the *lines* of source code where the object is - defined. If multiple objects are defined on the same line (e.g. - multiple lambdas), or if the object is embedded in a larger statement - (e.g. a list comprehension), the function will return the entire line(s). + This function returns the lines of source code where the object is + defined, including indentation. It always returns full lines and does not + strip other code that may appear on the same line. .. versionchanged:: 3.3 :exc:`OSError` is raised instead of :exc:`IOError`, now an alias of the