Skip to content

Commit

Permalink
ffi (Object): make class dict visible in instances (#5843)
Browse files Browse the repository at this point in the history
  • Loading branch information
t-vi authored Jun 18, 2020
1 parent 082874c commit 9ba98be
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
3 changes: 2 additions & 1 deletion python/tvm/runtime/object.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ def __repr__(self):
return _ffi_node_api.AsRepr(self)

def __dir__(self):
class_names = dir(self.__class__)
fnames = _ffi_node_api.NodeListAttrNames(self)
size = fnames(-1)
return [fnames(i) for i in range(size)]
return sorted([fnames(i) for i in range(size)] + class_names)

def __getattr__(self, name):
try:
Expand Down
7 changes: 7 additions & 0 deletions tests/python/unittest/test_node_reflection.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ def test_pass_config():
"tir.UnrollLoop": 1
})

def test_dict():
x = tvm.tir.const(1) # a class that has Python-defined methods
# instances should see the full class dict
assert set(dir(x.__class__)) <= set(dir(x))


if __name__ == "__main__":
test_string()
test_env_func()
Expand All @@ -138,3 +144,4 @@ def test_pass_config():
test_const_saveload_json()
test_make_sum()
test_pass_config()
test_dict()

0 comments on commit 9ba98be

Please sign in to comment.