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: inspect.getclosurevars returns incorrect variable when using class member with the same name as other variable
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.11
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Ryan Fox, hongweipeng, iritkatriel, yselivanov
Priority: normal Keywords:

Created on 2016-03-17 07:40 by Ryan Fox, last changed 2022-04-11 14:58 by admin.

Messages (4)
msg261897 - (view) Author: Ryan Fox (Ryan Fox) Date: 2016-03-17 07:40
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:

import inspect

class Foo:
    x = int()

x = 1
f = Foo()
assert(f.x != x)

func = lambda: f.x == 0

cv = inspect.getclosurevars(func)
assert(cv.globals['f'] == f)
assert(cv.globals.get('x') != x) # <--- Assertion fails

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.
msg407082 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-11-26 18:44
Reproduced on 3.11.
msg408856 - (view) Author: hongweipeng (hongweipeng) * Date: 2021-12-18 15:34
Why is expected that 'x' would not exist in cv.globals? I think it works normally, you can see `x` in `func.__globals__`.
msg408866 - (view) Author: Ryan Fox (Ryan Fox) Date: 2021-12-18 20:30
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 <> wrote:

> hongweipeng <> added the comment:
> Why is expected that 'x' would not exist in cv.globals? I think it works
> normally, you can see `x` in `func.__globals__`.
> ----------
> nosy: +hongweipeng
> _______________________________________
> Python tracker <>
> <>
> _______________________________________
Date User Action Args
2022-04-11 14:58:28adminsetgithub: 70764
2021-12-18 20:30:07Ryan Foxsetmessages: + msg408866
2021-12-18 15:34:16hongweipengsetnosy: + hongweipeng
messages: + msg408856
2021-11-26 18:44:24iritkatrielsetnosy: + iritkatriel
messages: + msg407082
2021-11-26 18:37:43iritkatrielsetversions: + Python 3.11, - Python 3.5, Python 3.6
2016-03-17 07:50:06SilentGhostsetnosy: + yselivanov

type: behavior
versions: - Python 3.3, Python 3.4
2016-03-17 07:40:30Ryan Foxcreate