-
-
Notifications
You must be signed in to change notification settings - Fork 275
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
nonlocal in locals #2617
base: main
Are you sure you want to change the base?
nonlocal in locals #2617
Conversation
Hmmm... Why pylint consider |
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 also don't understand the pylint
issue. Pinging @jacobtylerwalls to see if this sounds familiar, they have a very good grasp of recurring pylint
issues
astroid/rebuilder.py
Outdated
@@ -61,6 +61,11 @@ def __init__( | |||
self._manager = manager | |||
self._data = data.split("\n") if data else None | |||
self._global_names: list[dict[str, list[nodes.Global]]] = [] | |||
# In _nonlocal_names, |
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 is formatted a bit strange. Does it fit on two lines?
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've modified it to
# In _nonlocal_names saves the FunctionDef where the variable is created,
# rather than Nonlocal, since we don't really need the Nonlocal statement.
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.
Thanks for working up the PR, but if you don't mind I'd like to pause to consider this in a little more detail first. My first impression is to wonder if we need to make a change here at all, see comment on issue.
Type of Changes
Description
Closes #2616
I did this by adding a
_nonlocal_names
property inTreeRebuilder
to store those nonlocal names, like what_global_names
does. However it storesFunctionDef
s where the variables are created, instead ofNonlocal
nodes. (The savedGlobal
nodes in_global_names
is not used for as well, but I didn't touch it, in case we may need that one day.)The items of
_nonlocal_names
are added invisit_nonlocal
, which go through the tree and find the nearestFunctionDef
whoselocals
contains that name.And it is used in
_save_assignment
. Ifnode.name
is inself._nonlocal_names[-1]
, then it will be put intoself._nonlocal_names[-1][node.name]
(the foundFunctionDef
) rather than intonode.parent
.I have also added a unit test
test_locals_with_global_and_nonlocal
intest_builder.py
, but I'm not really sure if I put it in the correct place.There might be some other problems since it's my first PR for this project. Really need someone to check it before merging.