junrushao opened a new pull request, #495: URL: https://github.com/apache/tvm-ffi/pull/495
## Summary - Introduce `CAny` — a Python-visible Cython class wrapping an owned `TVMFFIAny` struct with proper ref-counting - `TypeSchema.convert()` now returns `CAny` instead of plain Python objects; use `.to_py()` to recover values - Add `TypeSchema.from_annotation()` to parse Python type annotations (`list[int]`, `Optional[str]`, etc.) into `TypeSchema` - Remove redundant `try_check_value` / `try_convert` methods (use `check_value` / `convert` which raise `TypeError`) - `convert().to_py()` guarantees FFI types: `str` → `tvm_ffi.String`, `bytes` → `tvm_ffi.Bytes`, containers → `ffi.Array/List/Map/Dict` ## Architecture - `cdef class CAny` in `object.pxi` owns a `TVMFFIAny`, calls `TVMFFIObjectDecRef` in `__dealloc__` for object types (type_index ≥ 64) - `CAny.__init__(value)` packs Python values via `TVMFFIPyPyObjectToFFIAny` + `TVMFFIAnyViewToOwnedAny` - `CAny.to_py()` calls `make_ret()` with IncRef for objects; promotes `SmallStr`/`SmallBytes` to `String`/`Bytes` - Converter dispatch functions in `type_check.pxi` still return Python objects internally; `CAny(result)` wraps at the boundary - Container converters (`_dispatch_array/list/map/dict`) wrap results in `ffi.Array/List/Map/Dict` via lazy-imported `container` module ## Breaking Changes - `TypeSchema.convert()` returns `CAny` (was `object`) — add `.to_py()` to recover Python values - `TypeSchema.try_check_value()` removed — use `check_value()` + `try/except TypeError` - `TypeSchema.try_convert()` removed — use `convert()` + `try/except TypeError` ## Test plan - [x] `uv run pytest tests/python -x` — 1465 passed, 25 skipped, 1 xfailed - [x] `uv run pre-commit run --all-files` — all 27 hooks pass (ruff, ty, clang-format, cython-lint, etc.) - [x] New `TestCAny` class (20 tests): type_index, to_py roundtrips, repr, multi-call safety, object refcount safety - [x] New `TestConvertToFFITypes` class (17 tests): guarantees FFI types for str, bytes, callable, containers, scalars, objects - [x] New `TestFromAnnotation*` classes: structural equality for scalars, callables, lists, dicts, arrays, tuples, optionals, unions, objects - [x] Rewrote ~500 existing tests for new API (try_check_value → pytest.raises, convert → convert().to_py()) -- 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]
