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.6, Python 3.5
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Ryan Fox, yselivanov
Priority: normal Keywords:

Created on 2016-03-17 07:40 by Ryan Fox, last changed 2016-03-17 07:50 by SilentGhost.

Messages (1)
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.
Date User Action Args
2016-03-17 07:50:06SilentGhostsetnosy: + yselivanov

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