ffi (Object): make class dict visible in instances (#5843)
authorThomas Viehmann <tv.code@beamnet.de>
Thu, 18 Jun 2020 18:14:21 +0000 (20:14 +0200)
committerGitHub <noreply@github.com>
Thu, 18 Jun 2020 18:14:21 +0000 (11:14 -0700)
python/tvm/runtime/object.py
tests/python/unittest/test_node_reflection.py

index ec9f22b..2a34b34 100644 (file)
@@ -46,9 +46,10 @@ class Object(ObjectBase):
         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:
index b109516..3a7318c 100644 (file)
@@ -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()
@@ -138,3 +144,4 @@ if __name__ == "__main__":
     test_const_saveload_json()
     test_make_sum()
     test_pass_config()
+    test_dict()