You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
inspect.getclosurevars returns incorrect variable when using class member with the same name as other variable #70764
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=Nonecreated_at=<Date2016-03-17.07:40:30.852>labels= ['type-bug', 'library', '3.11']
title='inspect.getclosurevars returns incorrect variable when using class member with the same name as other variable'updated_at=<Date2021-12-18.20:30:07.566>user='https://bugs.python.org/RyanFox'
If a variable 'x' exists in the global or local scope, and a function (also defined in the same scope as 'x', or lower) refers only to a member named 'x' of an object, inspect.getclosurevars will include a reference to the variable, rather than the member.
Okay, that's kind of confusing to describe, so here's a small example in code form:
It is expected that 'x' would not exist in cv.globals, since func does not refer to it. Also, there should be a 'f.x' included somewhere in the ClosureVariables object returned.
Why is expected that 'x' would not exist in cv.globals? I think it works normally, you can see x in func.__globals__.
RyanFox commented on Dec 18, 2021
RyanFoxmannequin
MannequinAuthor
More actions
If you change the class member 'x' to a different name like 'y', then cv
doesn't include 'x', but does include an unbound 'y'.
In both cases, the function isn't referring to a global variable, just the
class member of that name. Besides the function itself, no other globals
are included in cv.globals.
Activity
RyanFox commentedon Mar 17, 2016
If a variable 'x' exists in the global or local scope, and a function (also defined in the same scope as 'x', or lower) refers only to a member named 'x' of an object, inspect.getclosurevars will include a reference to the variable, rather than the member.
Okay, that's kind of confusing to describe, so here's a small example in code form:
It is expected that 'x' would not exist in cv.globals, since func does not refer to it. Also, there should be a 'f.x' included somewhere in the ClosureVariables object returned.
iritkatriel commentedon Nov 26, 2021
Reproduced on 3.11.
hongweipeng commentedon Dec 18, 2021
Why is expected that 'x' would not exist in cv.globals? I think it works normally, you can see
xinfunc.__globals__.RyanFox commentedon Dec 18, 2021
If you change the class member 'x' to a different name like 'y', then cv
doesn't include 'x', but does include an unbound 'y'.
In both cases, the function isn't referring to a global variable, just the
class member of that name. Besides the function itself, no other globals
are included in cv.globals.
On Sat, Dec 18, 2021 at 10:34 AM hongweipeng <report@bugs.python.org> wrote:
dansalvato commentedon Jul 4, 2023
This had me scratching my head when a class member happened to be named the same as an imported module. Example:
Output:
Clearly,
self.inspectrefers to a class member and not the global module, butgetclosurevars()doesn't understand this.It seems pretty easy for this to happen; in my case, it was a setter for
self.imagebeing conflated with an imported moduleimage.pythongh-70764: getclosurevars now identifies global variables from L…
pythongh-70764: improved performance by collecting global names in a …
gh-70764: inspect.getclosurevars now identifies global variables with…
pythongh-70764: inspect.getclosurevars now identifies global variable…
pythongh-70764: inspect.getclosurevars now identifies global variable…
[3.12] gh-70764: inspect.getclosurevars now identifies global variabl…
[3.13] gh-70764: inspect.getclosurevars now identifies global variabl…
pythongh-70764: inspect.getclosurevars now identifies global variable…
pythongh-70764: inspect.getclosurevars now identifies global variable…