You are viewing a plain text version of this content. The canonical link for it is here.
Posted to discuss-archive@tvm.apache.org by "junrushao (via GitHub)" <gi...@apache.org> on 2026/02/21 23:20:50 UTC
[PR] feat(stub): use distinct container type origins in type schema and stub generation [tvm-ffi]
junrushao opened a new pull request, #469:
URL: https://github.com/apache/tvm-ffi/pull/469
## Summary
- **Distinct container type origins**: TypeSchema now emits `Array`, `List`, `Map`, `Dict` instead of collapsing all sequence types to `list` and all mapping types to `dict`. This preserves the immutable (copy-on-write) vs mutable (shared-reference) distinction through the entire schema → annotation pipeline.
- **Correct stub annotations**: Generated stubs now use `MutableSequence`/`MutableMapping` for `List`/`Dict` parameters and `Sequence`/`Mapping` for `Array`/`Map`, matching the actual Python ABCs each container implements.
- **New containers concept page**: Added `docs/concepts/containers.rst` documenting all five container types, their mutability semantics, thread safety, and usage guidance. Updated C++ and Python language guides with List and Dict sections.
## Details
### Type schema changes
- `_TYPE_SCHEMA_ORIGIN_CONVERTER` in `type_info.pxi`: `ffi.Array` → `Array`, `ffi.List` → `List`, `ffi.Map` → `Map`, `ffi.Dict` → `Dict`
- `TY_MAP_DEFAULTS` in `stub/consts.py`: `Array` → `Sequence`, `List` → `MutableSequence`, `Map` → `Mapping`, `Dict` → `MutableMapping`
- Backward compat: raw `list`/`dict` origins still parse correctly in `TypeSchema`
### Stub codegen improvements
- Collision avoidance: when a function name matches a type import suffix, the type import gets an alias (e.g. `_Foo`) to avoid shadowing
- Dunder method stubs (`__ffi_shallow_copy__`) now emit `# noqa: D105`
- New `DeepCopy` global function stub added
### Documentation
- New `docs/concepts/containers.rst` concept page
- Updated `docs/concepts/any.rst` (List/Dict in type table)
- Updated `docs/guides/cpp_lang_guide.md` (List, Dict sections)
- Updated `docs/guides/python_lang_guide.md` (List, Dict, Tuple sections)
- Updated `CLAUDE.md` and `.claude/skills/devtools/SKILL.md` (stub generation workflow)
## Test plan
- [x] `uv run pytest -vvs tests/python/test_metadata.py tests/python/test_stubgen.py tests/python/test_container.py` — 169 passed
- [ ] CI: lint, clang-tidy, full test suite
- [ ] Verify Sphinx doc build renders new containers page correctly
🤖 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: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org
Re: [PR] feat(stub): use distinct container type origins in type schema and stub generation [tvm-ffi]
Posted by "gemini-code-assist[bot] (via GitHub)" <gi...@apache.org>.
gemini-code-assist[bot] commented on PR #469:
URL: https://github.com/apache/tvm-ffi/pull/469#issuecomment-3939680943
## Summary of Changes
Hello @junrushao, I'm Gemini Code Assist[^1]! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request significantly enhances the type system and stub generation for TVM-FFI by introducing distinct type schema origins for different container types (Array, List, Map, Dict). This refinement allows for more precise type annotations in generated Python stubs, accurately reflecting mutability semantics. Additionally, comprehensive documentation has been added to clarify the behavior and usage of these container types across C++ and Python interfaces.
### Highlights
* **Distinct Container Type Origins**: TypeSchema now emits 'Array', 'List', 'Map', 'Dict' instead of collapsing all sequence types to 'list' and all mapping types to 'dict'. This change preserves the distinction between immutable (copy-on-write) and mutable (shared-reference) container types throughout the schema and annotation pipeline.
* **Correct Stub Annotations**: Generated stubs now utilize 'MutableSequence'/'MutableMapping' for 'List'/'Dict' parameters and 'Sequence'/'Mapping' for 'Array'/'Map', aligning with the actual Python Abstract Base Classes (ABCs) that each container implements. This improves type accuracy in the generated Python stubs.
* **New Containers Concept Page and Documentation Updates**: A new documentation page, 'docs/concepts/containers.rst', has been added to detail all five container types, their mutability semantics, thread safety, and usage guidance. Existing C++ and Python language guides have also been updated with dedicated sections for List and Dict.
🧠 **New Feature in Public Preview:** You can now enable **Memory** to help **Gemini Code Assist** learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. **Click [here](https://codeassist.google/code-review/login) to enable Memory in your admin console.**
<details>
<summary><b>Changelog</b></summary>
* **.claude/skills/devtools/SKILL.md**
* Added a new section detailing how to update Python stubs after C++/Cython reflection changes.
* **CLAUDE.md**
* Inserted a new section explaining the process for updating Python stubs.
* **docs/concepts/any.rst**
* Updated the description of container storage to include List and Dict.
* Added `ListObj*` and `DictObj*` to the table of heap-allocated objects.
* **docs/concepts/containers.rst**
* Added a new documentation file introducing and detailing the five built-in container types (Array, List, Tuple, Map, Dict), their mutability, semantics, and thread safety.
* **docs/guides/cpp_lang_guide.md**
* Added a table summarizing the container types, their headers, mutability, and semantics.
* Included a new section with examples and usage guidance for `List<T>`.
* Included a new section with examples and usage guidance for `Dict<K, V>`.
* **docs/guides/python_lang_guide.md**
* Rewrote the 'Container Types' section to introduce immutable and mutable variants.
* Added detailed sections for `Array` (immutable sequence), `List` (mutable sequence), `Map` (immutable mapping), and `Dict` (mutable mapping) with Python examples.
* Added a section explaining Tuple conversion to `tvm_ffi.Array`.
* **docs/index.rst**
* Added `concepts/containers.rst` to the table of contents under the 'Concepts' section.
* **python/tvm_ffi/_ffi_api.py**
* Updated `TYPE_CHECKING` imports to include `MutableMapping`, `MutableSequence`, `Object`, and `_StructuralKey`.
* Reordered and refined type hints for `ArrayContains`, `Dict` and `List` methods to use `MutableMapping` and `MutableSequence` where appropriate.
* Added a stub for the new `DeepCopy` global function.
* Updated the return type of `GetInvalidObject` to `Object`.
* Updated the return type of `StructuralKey` to `_StructuralKey`.
* **python/tvm_ffi/access_path.py**
* Added `__ffi_shallow_copy__` method stub with `noqa: D105` to `AccessStep`.
* Added `__ffi_shallow_copy__` method stub with `noqa: D105` to `AccessPath`.
* **python/tvm_ffi/cython/type_info.pxi**
* Modified `_TYPE_SCHEMA_ORIGIN_CONVERTER` to map `ffi.Array`, `ffi.List`, `ffi.Map`, `ffi.Dict` to their distinct origins: `Array`, `List`, `Map`, `Dict` respectively.
* Updated `TypeSchema` validation logic to correctly handle `Array`, `List`, `Map`, and `Dict` origins, ensuring they accept 0 or 1 argument for sequences and 0 or 2 for mappings.
* Updated `repr` method documentation examples to reflect the new container type origins.
* **python/tvm_ffi/structural.py**
* Added `__ffi_shallow_copy__` method stub with `noqa: D105` to `StructuralKey`.
* **python/tvm_ffi/stub/codegen.py**
* Modified `_type_suffix_and_record` to introduce type import aliasing (e.g., `_Foo`) when a type name collides with a function name, preventing shadowing issues.
* **python/tvm_ffi/stub/consts.py**
* Updated `TY_MAP_DEFAULTS` to map `Array` to `collections.abc.Sequence`, `List` to `collections.abc.MutableSequence`, `Map` to `collections.abc.Mapping`, and `Dict` to `collections.abc.MutableMapping`.
* **python/tvm_ffi/stub/utils.py**
* Added `noqa: D105` comments to generated dunder method stubs (e.g., `__ffi_shallow_copy__`) to suppress D105 (missing docstring) linting errors.
* **python/tvm_ffi/testing/_ffi_api.py**
* Updated `TYPE_CHECKING` imports to include `MutableMapping` and `MutableSequence`.
* Modified type hints for `schema_id_dict_str_int`, `schema_id_dict_str_str` to use `MutableMapping`.
* Modified type hints for `schema_id_list_int`, `schema_id_list_obj`, `schema_id_list_str` to use `MutableSequence`.
* **python/tvm_ffi/testing/testing.py**
* Added `__ffi_shallow_copy__` method stub to `TestObjectBase`.
* Added `__ffi_shallow_copy__` method stub to `TestIntPair`.
* Added `__ffi_shallow_copy__` method stub to `TestObjectDerived`.
* Added `__ffi_shallow_copy__` method stub to `_SchemaAllTypes`.
* **tests/python/test_container.py**
* Added `type: ignore[invalid-argument-type]` comments to several cross-conversion tests (`test_seq_cross_conv_array_to_list_int`, `test_seq_cross_conv_array_to_list_str`, `test_seq_cross_conv_empty_array_to_list`, `test_map_cross_conv_map_to_dict_str_int`, `test_map_cross_conv_map_to_dict_str_str`, `test_map_cross_conv_empty_map_to_dict`) to address type checker warnings introduced by more specific type hints.
* **tests/python/test_metadata.py**
* Renamed `_replace_list_dict` function to `_replace_container_types` and updated its logic to map `Array`, `List`, `Map`, `Dict` to their respective `collections.abc` types.
* Updated expected type schema strings in `test_schema_global_func` and `test_schema_field` to reflect the new `Array`, `List`, `Map`, `Dict` origins instead of generic `list` and `dict`.
* **tests/python/test_stubgen.py**
* Added `test_type_schema_container_origins` to verify that `TypeSchema` correctly handles and validates the distinct `Array`, `List`, `Map`, `Dict` origins.
* Added `test_objectinfo_gen_fields_container_types` to confirm that `ObjectInfo` fields render distinct container annotations.
* Added `test_generate_global_funcs_aliases_colliding_type` to ensure that type imports are aliased when their names collide with function names.
</details>
<details>
<summary><b>Activity</b></summary>
* The pull request introduces significant changes to the type schema and stub generation, impacting how container types are handled and annotated.
* Documentation has been extensively updated with new concept pages and language guide sections to reflect the new container types and their semantics.
* The author has provided a test plan, indicating that `pytest` has been run for `test_metadata.py`, `test_stubgen.py`, and `test_container.py`, with 169 tests passing.
* The pull request was generated using Claude Code, suggesting an AI-assisted development process.
</details>
<details>
<summary><b>Using Gemini Code Assist</b></summary>
<br>
The full guide for Gemini Code Assist can be found on our [documentation page](https://developers.google.com/gemini-code-assist/docs/review-github-code), here are some quick tips.
<b>Invoking Gemini</b>
You can request assistance from Gemini at any point by creating a comment using either `/gemini <command>` or `@gemini-code-assist <command>`. Below is a summary of the supported commands on the current page.
Feature | Command | Description
--- | --- | ---
Code Review | `/gemini review` | Performs a code review for the current pull request in its current state.
Pull Request Summary | `/gemini summary` | Provides a summary of the current pull request in its current state.
Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help | `/gemini help` | Displays a list of available commands.
<b>Customization</b>
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a `.gemini/` folder in the base of the repository. Detailed instructions can be found [here](https://developers.google.com/gemini-code-assist/docs/customize-gemini-behavior-github).
<b>Limitations & Feedback</b>
Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with :thumbsup: and :thumbsdown: on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up [here](https://google.qualtrics.com/jfe/form/SV_2cyuGuTWsEw84yG).
<b>You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the [Gemini Code Assist IDE Extension](https://cloud.google.com/products/gemini/code-assist).</b>
</details>
[^1]: Review the [Privacy Notices](https://policies.google.com/privacy), [Generative AI Prohibited Use Policy](https://policies.google.com/terms/generative-ai/use-policy), [Terms of Service](https://policies.google.com/terms), and learn how to configure Gemini Code Assist in GitHub [here](https://developers.google.com/gemini-code-assist/docs/customize-gemini-behavior-github). Gemini can make mistakes, so double check it and [use code with caution](https://support.google.com/legal/answer/13505487).
--
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: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org
Re: [PR] feat(stub): use distinct container type origins in type schema and stub generation [tvm-ffi]
Posted by "junrushao (via GitHub)" <gi...@apache.org>.
junrushao merged PR #469:
URL: https://github.com/apache/tvm-ffi/pull/469
--
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: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org