Closed yueyinqiu closed 2 weeks ago
Hmmm... Why pylint consider scope
as Nonlocal
since it's inside if isinstance(scope, nodes.FunctionDef)
... Should I add # type: ignore
here to suppress the warning?
Thanks for engaging in discussion. Let us know if the nodes_of_class() solution doesn't work well for you.
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.