junrushao opened a new pull request, #491:
URL: https://github.com/apache/tvm-ffi/pull/491
## Summary
Extend the Python stubgen tool to emit proper `__init__` signatures derived
from C++ reflection metadata, and wire `register_object` to automatically
install `__init__` from `__ffi_init__` — so that registered classes with C++
constructors work out of the box without `@c_class`.
## Architecture
- **`stub/utils.py`**: New `InitFieldInfo` / `ObjectStubInfo` classes walk
the parent chain of `TypeInfo` fields to collect `c_init`, `c_kw_only`,
`c_has_default` flags, then emit `__init__` stubs via `_gen_auto_init`
(KWARGS-protocol with proper signature) or `_gen_c_init` (positional
pass-through from `__c_ffi_init__`).
- **`registry.py`**: `register_object._register` now calls
`_install_init(cls, enabled=True)` after `_add_class_attrs`, so any class whose
C++ type has `refl::init()` metadata automatically gets a Python `__init__`.
- **`registry.py` (`_install_init`)**: When `enabled=True` and no
`__ffi_init__` exists, the function returns silently instead of installing a
`TypeError` guard — preserving backward compatibility for `Object()` and other
base classes.
- Removed a duplicate `_install_init` definition that shadowed the primary
one after the rebase onto `main`.
## Public Interfaces
- `register_object` now auto-wires `__init__` when C++ reflection provides
`__ffi_init__`. Previously only `@c_class` did this.
- `slots` docstrings updated: recommend `__slots__ = ("__dict__",)` instead
of the now-removed `slots=False` metaclass keyword.
## Behavioral Changes
- Registered classes with `__ffi_init__` (non-auto-init) get `__init__ =
__ffi_init__` automatically. This fixes `IntPair(1, 2)` in
`examples/python_packaging/`.
- Classes without `__ffi_init__` keep default `object.__init__` behavior (no
guard installed).
- `@c_class(init=False)` still installs a `TypeError` guard as before.
## Test Plan
- [x] Pre-commit hooks pass (ruff check, ruff format, ty check, cython-lint,
clang-format, ASF headers)
- [ ] Full `pytest tests/python` (requires build)
- [ ] `examples/python_packaging` end-to-end (requires build + `uv pip
install`)
- [ ] C++ tests unaffected (no C++ changes)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]