-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
bpo-38307:completes the Stack implementation to yield ending line for each class. #16466
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bpo-38307:completes the Stack implementation to yield ending line for each class. #16466
Conversation
Hello, and thanks for your contribution! I'm a bot set up to make sure that the project can legally accept this contribution by verifying everyone involved has signed the PSF contributor agreement (CLA). CLA MissingOur records indicate the following people have not signed the CLA: @avisrivastava254084 For legal reasons we need all the people listed to sign the CLA before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue. If you have recently signed the CLA, please wait at least one business day You can check yourself to see if the CLA has been received. Thanks again for the contribution, we look forward to reviewing it! |
@the-knights-who-say-ni I have done the CLA. When will it get updated? |
Thanks @avisrivastava254084, and welcome to CPython! Regarding the CLA, the link the bot provided says the following:
It's my understanding that these signatures are processed by a human at the PSF, so it probably won't happen on a Saturday night / Sunday morning! Do you mind updating the PR title to with "bpo-" in front of the issue number, so this can be linked appropriately? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@avisrivastava254084: this is a very noisy patch. The real changes only affect about 8 lines, but you've changed over 30. Please revert all of the unrelated formatting/whitespace changes (as well as a few others) that I've highlighted in the review below.
Do you mind explaining why this is necessary? In particular, I'm concerned about:
- ...why we need
endline
. Do you have a specific use case? - ...if
frames[1].frame.f_locals['start'][0] - 1
is the correct way to calculate this. - ...if doing it in
__delitem__
of a customlist
subclass is the right way to do this (I'm almost certain it's not).
At a minimum, this will need tests and a NEWS entry.
Lib/pyclbr.py
Outdated
@@ -51,6 +51,7 @@ | |||
|
|||
class _Object: | |||
"Information about Python class or function." | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove all these whitespace lines that you've added.
Lib/pyclbr.py
Outdated
@@ -80,6 +83,20 @@ def _addmethod(self, name, lineno): | |||
self.methods[name] = lineno | |||
|
|||
|
|||
class Stack(list): | |||
|
|||
def __init__(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This __init__
method isn't needed, since all it does is import inspect
into the function's scope.
Lib/pyclbr.py
Outdated
@@ -161,7 +182,8 @@ def _readmodule(module, path, inpackage=None): | |||
search_path = path + sys.path | |||
spec = importlib.util._find_spec_from_path(fullmodule, search_path) | |||
if spec is None: | |||
raise ModuleNotFoundError(f"no module named {fullmodule!r}", name=fullmodule) | |||
raise ModuleNotFoundError( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please don't reformat this line. It's fine as-is!
Lib/pyclbr.py
Outdated
@@ -227,14 +249,14 @@ def _create_tree(fullmodule, path, fname, source, tree, inpackage): | |||
del stack[-1] | |||
tokentype, class_name, start = next(g)[0:3] | |||
if tokentype != NAME: | |||
continue # Skip class with syntax error. | |||
continue # Skip class with syntax error. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please revert this whitespace change as well. Below also...
Lib/pyclbr.py
Outdated
@@ -271,7 +293,7 @@ def _create_tree(fullmodule, path, fname, source, tree, inpackage): | |||
if stack: | |||
cur_obj = stack[-1][0] | |||
cur_class = _nest_class( | |||
cur_obj, class_name, lineno, inherit) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please revert this.
Lib/pyclbr.py
Outdated
@@ -377,7 +399,7 @@ def _main(): | |||
else: | |||
path = [] | |||
tree = readmodule_ex(mod, path) | |||
lineno_key = lambda a: getattr(a, 'lineno', 0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unidiomatic, sure. But fine as-is, and unrelated to the change. Please revert.
Lib/pyclbr.py
Outdated
@@ -193,8 +215,8 @@ def _create_tree(fullmodule, path, fname, source, tree, inpackage): | |||
""" | |||
f = io.StringIO(source) | |||
|
|||
stack = [] # Initialize stack of (class, indent) pairs. | |||
|
|||
# stack = [] # Initialize stack of (class, indent) pairs. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove this rather than just commenting it out. It makes for a better diff.
# stack = [] # Initialize stack of (class, indent) pairs. |
Lib/pyclbr.py
Outdated
stack = [] # Initialize stack of (class, indent) pairs. | ||
|
||
# stack = [] # Initialize stack of (class, indent) pairs. | ||
stack = Stack() # changing the source code so as to get the ending line |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment belongs in a commit message, not in the source code. The old comment is fine.
stack = Stack() # changing the source code so as to get the ending line | |
stack = Stack() # Initialize stack of (class, indent) pairs. |
@brandtbucher
This is useful in drawing and generating UML diagrams, especially in knowing which class is dependent on which classes. I"ll make the rest of the changes as suggested. |
I'd want your suggestions or some guiding material that you think I should go through.
Same as above. |
Rather than subclassing A couple of other things: if this is accepted, it will need tests, updated docs, and a NEWS entry. All of the classes and comments in this file should also reference the new attribute. And please change the title of this PR to start with "bpo-38307:" so that the bot (and humans) can find your issue! |
Hello, and thanks for your contribution! I'm a bot set up to make sure that the project can legally accept this contribution by verifying everyone involved has signed the PSF contributor agreement (CLA). Recognized GitHub usernameWe couldn't find a bugs.python.org (b.p.o) account corresponding to the following GitHub usernames: This might be simply due to a missing "GitHub Name" entry in one's b.p.o account settings. This is necessary for legal reasons before we can look at this contribution. Please follow the steps outlined in the CPython devguide to rectify this issue. You can check yourself to see if the CLA has been received. Thanks again for the contribution, we look forward to reviewing it! |
…nstead to the stack
@brandtbucher I was wondering if you could also help me with the "CLA Not Signed" issue? |
@brandtbucher |
Codecov Report
@@ Coverage Diff @@
## master #16466 +/- ##
===========================================
- Coverage 82.92% 79.46% -3.46%
===========================================
Files 1864 384 -1480
Lines 569838 169290 -400548
Branches 42011 0 -42011
===========================================
- Hits 472515 134530 -337985
+ Misses 88124 34760 -53364
+ Partials 9199 0 -9199
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, this still needs tests (in Lib/test/test_pyclbr.py
) and a NEWS entry. The documentation and docstrings will also need to be updated (basically, anywhere the current lineno
attribute is referenced).
Regarding the CLA, I think you will need to link your new GitHub username to your BPO account and re-sign the CLA. Not sure, though.
Also, just so you don't get discouraged: it's great that you're taking the time to contribute to CPython, but this module has no current experts or maintainers. I am not able to merge this PR once it's ready, and there is a fair chance that no active core developer would feel comfortable adding a new feature like this - especially without significant demand.
Lib/pyclbr.py
Outdated
@@ -202,11 +202,13 @@ def _create_tree(fullmodule, path, fname, source, tree, inpackage): | |||
lineno, thisindent = start | |||
# Close previous nested classes and defs. | |||
while stack and stack[-1][1] >= thisindent: | |||
setattr(stack[-1][0], 'end_lineno', start[0] - 1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did you switch to using setattr
here? This should be fine, right?
setattr(stack[-1][0], 'end_lineno', start[0] - 1) | |
stack[-1][0].end_lineno = start[0] - 1 |
Also on the two other lines, below...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have to set the attribute, right? Otherwise, it results into the error: <> has no attribute end_lineno.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, just so you don't get discouraged: it's great that you're taking the time to contribute to CPython, but this module has no current experts or maintainers. I am not able to merge this PR once it's ready, and there is a fair chance that no active core developer would feel comfortable adding a new feature like this - especially without significant demand.
I received some positive responses on Stackoverflow for this. Don't you think there should be a starting and an end line for this?
Even if you think and abide by to go with the popular demand, should I opt for a survey, etc? Is there some platform where I can ask for votes: "Should there be end_lineno?"
Thank you
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have to set the attribute, right? Otherwise, it results into the error: <> has no attribute end_lineno.
They should both be equivalent. You're certain that applying my suggested change results in an error?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. My bad, apologies. I tried the direct approach to stack[-1]
instead of stack[-1][0]
.
@kebab-mai-haddi, on the bug tracker, you should be able to edit your GitHub profile name. Select 'Your details' on the left and it should take you to a screen where you can update your info. |
Sorry, I should have been clearer. I myself am not really opposed to adding this feature; I just don't have the necessary permissions to actually merge this PR, and I've never even seen this module before. Once I deem that it's ready, my next step is usually to request the review of a maintainer or owner of this module who does have the ability to merge it. It doesn't look like we have anybody like that, though. If you wanted to get somebody's attention, responding to Steven's questions on the BPO issue would be a great place to start (actually, it should probably happen regardless). If that fizzles out, you could also make a post on the Python-Ideas mailing list outlining your use case, and gathering feedback. I would also link this PR and the open BPO issue there, as well. |
…va254084/cpython into endline-in-readmodule-module
@brandtbucher
the actual code of which is available in the diff of this branch. The test is failing as the children being compared are of different types. The error is:
Now, I am failing to understand what and how should I write the test for the new change of |
@kebab-mai-haddi I'm not too familiar with this code, but a cursory scan tells me this:
I believe your patch only adds the new |
I have done that. If you check here, you will see that if the object is of type Thank you in advance! |
38307: Provide class' end line as well in order to provide the scope of the class in a module.
https://bugs.python.org/issue38307