Skip to content

Commit 4aea3e7

Browse files
committed
fix: provides __repr__, __module__, and __qualname__ for documentation
1 parent b984dd7 commit 4aea3e7

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

src/experimaestro/experiments/mockmodule.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,29 @@ def __mro_entries__(self, bases):
153153

154154

155155
class _FakeClassProxy:
156-
"""Wrapper that intercepts attribute access on fake classes."""
156+
"""Wrapper that intercepts attribute access on fake classes.
157+
158+
Provides ``__repr__``, ``__module__``, and ``__qualname__`` so that
159+
Sphinx renders type annotations using the original dotted path
160+
(e.g. ``torch.Tensor``) instead of the proxy's internal class name.
161+
"""
157162

158163
def __init__(self, fake_class):
164+
path = getattr(fake_class, "_path", "") or "MockedClass"
165+
parts = path.rsplit(".", 1)
159166
object.__setattr__(self, "_fake_class", fake_class)
167+
object.__setattr__(self, "_path", path)
168+
# Set dunder attributes so Sphinx renders type annotations correctly
169+
# (e.g. "torch.Tensor" instead of "_FakeClassProxy object at 0x...")
170+
object.__setattr__(self, "__qualname__", parts[-1])
171+
object.__setattr__(self, "__name__", parts[-1])
172+
object.__setattr__(self, "__module__", parts[0] if len(parts) > 1 else path)
173+
174+
def __repr__(self):
175+
return object.__getattribute__(self, "_path")
176+
177+
def __str__(self):
178+
return self.__repr__()
160179

161180
def __getattr__(self, name):
162181
if name.startswith("_"):

0 commit comments

Comments
 (0)