This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Add .end_lineno attribute to pyclbr _Objects
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.10
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: aviral, brandtbucher, kebab-mai-haddi, steven.daprano, terry.reedy
Priority: normal Keywords: patch

Created on 2019-09-28 17:26 by aviral, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 16466 closed aviral, 2019-10-08 19:24
PR 24348 merged aviral, 2021-01-27 06:28
Messages (8)
msg353467 - (view) Author: Aviral (aviral) * Date: 2019-09-28 17:26
Currently, the `readmodule` returns the starting line of the classes but not the end line. This should be provided as well in order to get the scope of the class, mainly to help check what all imports were used in a class.
msg353468 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2019-09-28 18:26
What `readmodule` are you referring to?

3.6 is in feature-freeze. The earliest that any enhancement could be added is version 3.9.

Can you explain how having an ending line number as well as the starting line number can be used to check which imports are used in a class?
msg385657 - (view) Author: Aviral Srivastava (kebab-mai-haddi) Date: 2021-01-25 20:07
How do I generate the endline no? Initially, I could
do, stack[-1][0].end_lineno = start[0] - 1 but how do I this now given that the recent changes are operating on the AST instead of the token stream?
msg385807 - (view) Author: Aviral Srivastava (kebab-mai-haddi) Date: 2021-01-27 19:46
I have made the changes and tested, my builds were successful. Please review it? 

CC @brandtbucher, @steven.daprano
msg385814 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2021-01-27 22:34
The answer, Aviral discovered,  is that current ast nodes have end_lineno attributes, so the patch amounts to consistently copying them.

I don't understand Aviral's use case either, but an item on my IDLE wish list is to be able to move class and function definitions within a file by moving the Class and Function entries within the browser tree.  This requieres end lines and having it be an attribute is easier and more accurate than recalculating it.  Merely clicking on an entry could highlight the whole definition.  Other people might think of other uses.

An issue in the patch is inserting parameter 'end_line' after 'line'.  Normally, new parameters have to go at the end, but:

1. Inserting 'end_line' after 'line' makes the code easier to read.

2. The parameter or parameters at the end have default values.  If we added 'end_line=None', we would have to consider what to do when it is omitted.

3., which I rewrote in 2017, documents readline and readline_ex as the public call interface.  They return a hierarchical structure that includes Functions and Classes in the dict values.  Their signatures are intentionally omitted and the attributes are only listed after saying "Users are not expected to create instances of these classes."

I posted "What is the pyclbr public API" to pydev asking about this issue.
msg385816 - (view) Author: Aviral Srivastava (kebab-mai-haddi) Date: 2021-01-27 22:57
Hey Terry, thanks for commenting. I have a few questions to ask you, please pardon my lack of awareness.

>This requieres end lines and having it be an attribute is easier and more accurate than recalculating it.

How do you recalculate the end_lineno?

Since all the objects that _start_, have an _end_ too, would `end_lineno=None` make any sense?

I could not make anything out of your third point where you mention `readline` and `readline_ex`. Can you explain that point?

>I posted "What is the pyclbr public API" to pydev asking about this issue.

Can you share the link of your post?

As for my use case, I need the scope of a class and a function in the new tool that I am developing. My tool is to generate a new type of UML diagrams for a given codebase. Do check it out and leave your critical feedback:

I need to know about the scope (start and end) so that I can deduce whether the "used_at" attribute of an object is within a class. This will help me deduce whether a particular class (or a function) uses any imported object. This is to deduce the dependency of a class on other classes and functions.

Currently, I have to edit the pyclbr and make it custom, then, had to Dockerize the whole thing. So, want to contribute to `cpython` so that this feature is present at the source itself.
msg385819 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2021-01-27 23:19
If IDLE were to recalculate the end from start, it would have to more or less parse forward to find the end of the statement.  This is why I considered it a speculative wish.

> would `end_lineno=None` make any sense?  
Only as a kludge if a default were required because of the signature position.

Point 3: The existing doc implies but does not exactly state that the _Object signature are private, so that we could break any possibly existing (but possibly not, and discouraged) use.  I think back-compatibility is discussed in the devguide.

Posts do not have a link until distributed, and then you can find it on  The title is sufficient to do that.
msg386085 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2021-02-01 17:39
New changeset 000cde59847beaf5fa7b73633e1f3c898fe5bf90 by Aviral Srivastava in branch 'master':
bpo-38307: Add end_lineno attribute to pyclbr Objects (GH-24348)
Date User Action Args
2022-04-11 14:59:20adminsetgithub: 82488
2022-03-19 08:04:21iritkatrielsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2021-02-01 17:39:16terry.reedysetmessages: + msg386085
2021-01-27 23:19:18terry.reedysetmessages: + msg385819
2021-01-27 22:57:54kebab-mai-haddisetmessages: + msg385816
2021-01-27 22:34:04terry.reedysetnosy: + terry.reedy
title: Provide Class' end line in pyclbr module -> Add .end_lineno attribute to pyclbr _Objects
messages: + msg385814

versions: + Python 3.10, - Python 3.9
2021-01-27 19:46:36kebab-mai-haddisetmessages: + msg385807
2021-01-27 06:28:31aviralsetpull_requests: + pull_request23168
2021-01-25 20:07:53kebab-mai-haddisetnosy: + kebab-mai-haddi
messages: + msg385657
2019-10-18 13:10:07steven.dapranosettitle: Provide Class' end line in readmodule module -> Provide Class' end line in pyclbr module
2019-10-08 19:51:36brandtbuchersetnosy: + brandtbucher
2019-10-08 19:24:03aviralsetkeywords: + patch
stage: patch review
pull_requests: + pull_request16249
2019-09-28 18:26:47steven.dapranosetnosy: + steven.daprano

messages: + msg353468
versions: + Python 3.9, - Python 3.6
2019-09-28 17:26:30aviralcreate