This is an automated email from the ASF dual-hosted git repository.
tqchen pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tvm-ffi.git
The following commit(s) were added to refs/heads/main by this push:
new 43ffe57 fix: Only attach docstring when it's defined (#60)
43ffe57 is described below
commit 43ffe571bfef2a3f2c2dc254ca3e5dc10e093daa
Author: Junru Shao <[email protected]>
AuthorDate: Fri Sep 26 12:03:39 2025 -0700
fix: Only attach docstring when it's defined (#60)
PR #49 introduced a behavior that generates a default docstring if it's
not available. However, it seems to conflict with upstream Sphinx doc
generation. This PR reverts such behavior.
---
python/tvm_ffi/cython/type_info.pxi | 15 +++++++++------
src/ffi/extra/testing.cc | 4 ++--
tests/python/test_object.py | 8 ++++++++
3 files changed, 19 insertions(+), 8 deletions(-)
diff --git a/python/tvm_ffi/cython/type_info.pxi
b/python/tvm_ffi/cython/type_info.pxi
index ca893b4..0ef53e3 100644
--- a/python/tvm_ffi/cython/type_info.pxi
+++ b/python/tvm_ffi/cython/type_info.pxi
@@ -79,18 +79,21 @@ class TypeField:
def as_property(self, object cls):
"""Create a Python ``property`` object for this field on ``cls``."""
cdef str name = self.name
- cdef str doc = self.doc or
f"{cls.__module__}.{cls.__qualname__}.{name}"
cdef FieldGetter fget = self.getter
cdef FieldSetter fset = self.setter
+ cdef object ret
fget.__name__ = fset.__name__ = name
fget.__module__ = fset.__module__ = cls.__module__
fget.__qualname__ = fset.__qualname__ = f"{cls.__qualname__}.{name}"
- fget.__doc__ = fset.__doc__ = f"Property `{name}` of class
`{cls.__qualname__}`"
- return property(
+ ret = property(
fget=fget,
fset=fset if (not self.frozen) else None,
- doc=doc,
)
+ if self.doc:
+ ret.__doc__ = self.doc
+ fget.__doc__ = self.doc
+ fset.__doc__ = self.doc
+ return ret
@dataclasses.dataclass(eq=False)
@@ -105,14 +108,14 @@ class TypeMethod:
def as_callable(self, object cls):
"""Create a Python method attribute for this method on ``cls``."""
cdef str name = self.name
- cdef str doc = self.doc or f"Method `{name}` of class
`{cls.__qualname__}`"
cdef object func = self.func
if not self.is_static:
func = _member_method_wrapper(func)
func.__module__ = cls.__module__
func.__name__ = name
func.__qualname__ = f"{cls.__qualname__}.{name}"
- func.__doc__ = doc
+ if self.doc:
+ func.__doc__ = self.doc
if self.is_static:
func = staticmethod(func)
return func
diff --git a/src/ffi/extra/testing.cc b/src/ffi/extra/testing.cc
index 5d7ef58..43ab8d2 100644
--- a/src/ffi/extra/testing.cc
+++ b/src/ffi/extra/testing.cc
@@ -58,8 +58,8 @@ class TestIntPair : public tvm::ffi::ObjectRef {
TVM_FFI_STATIC_INIT_BLOCK() {
namespace refl = tvm::ffi::reflection;
refl::ObjectDef<TestIntPairObj>()
- .def_ro("a", &TestIntPairObj::a)
- .def_ro("b", &TestIntPairObj::b)
+ .def_ro("a", &TestIntPairObj::a, "Field `a`")
+ .def_ro("b", &TestIntPairObj::b, "Field `b`")
.def_static("__ffi_init__", refl::init<TestIntPairObj, int64_t,
int64_t>);
}
diff --git a/tests/python/test_object.py b/tests/python/test_object.py
index d23aa10..ee5dc4e 100644
--- a/tests/python/test_object.py
+++ b/tests/python/test_object.py
@@ -45,6 +45,14 @@ def test_method() -> None:
assert type(obj0).v_i64.__doc__ == "i64 field" # type:
ignore[attr-defined]
+def test_attribute() -> None:
+ obj = tvm_ffi.testing.TestIntPair(3, 4)
+ assert obj.a == 3
+ assert obj.b == 4
+ assert type(obj).a.__doc__ == "Field `a`"
+ assert type(obj).b.__doc__ == "Field `b`"
+
+
def test_setter() -> None:
# test setter
obj0 = tvm_ffi.testing.create_object("testing.TestObjectBase", v_i64=10,
v_str="hello")