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 "guan404ming (via GitHub)" <gi...@apache.org> on 2026/01/06 04:51:31 UTC
[PR] feat: add kw_only support for dataclass init generation [tvm-ffi]
guan404ming opened a new pull request, #384:
URL: https://github.com/apache/tvm-ffi/pull/384
## Related Issue
#356
## Why
Python's standard dataclasses support kw_only parameter to make fields keyword-only in __init__. This feature was missing from @c_class decorator.
## How
- Add `KW_ONLY` sentinel class for marking keyword-only fields
- Add `kw_only` parameter to field() function and @c_class decorator
- Update `method_init()` to generate proper signature
- Add tests
--
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: add kw_only support for dataclass init generation [tvm-ffi]
Posted by "junrushao (via GitHub)" <gi...@apache.org>.
junrushao commented on code in PR #384:
URL: https://github.com/apache/tvm-ffi/pull/384#discussion_r2702065841
##########
python/tvm_ffi/dataclasses/field.py:
##########
@@ -24,6 +24,31 @@
_FieldValue = TypeVar("_FieldValue")
+class _KW_ONLY_TYPE:
Review Comment:
No need to redefine a new class - You may want to reuse `dataclasses.KW_ONLY`
--
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: add kw_only support for dataclass init generation [tvm-ffi]
Posted by "junrushao (via GitHub)" <gi...@apache.org>.
junrushao merged PR #384:
URL: https://github.com/apache/tvm-ffi/pull/384
--
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: add kw_only support for dataclass init generation [tvm-ffi]
Posted by "junrushao (via GitHub)" <gi...@apache.org>.
junrushao commented on PR #384:
URL: https://github.com/apache/tvm-ffi/pull/384#issuecomment-3764985408
OK it turns out KW_ONLY may not exist in `dataclasses` in Python 3.8. This is silly :)
If import fails, let's do this:
```python
_KW_ONLY_TYPE = type("_KW_ONLY_TYPE", (object, ), {})
KW_ONLY = _KW_ONLY_TYPE()
```
--
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: add kw_only support for dataclass init generation [tvm-ffi]
Posted by "gemini-code-assist[bot] (via GitHub)" <gi...@apache.org>.
gemini-code-assist[bot] commented on PR #384:
URL: https://github.com/apache/tvm-ffi/pull/384#issuecomment-3713104794
## Summary of Changes
Hello @guan404ming, 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 `tvm_ffi.dataclasses` module by integrating support for keyword-only arguments in the automatically generated `__init__` methods. This feature, inspired by standard Python dataclasses, provides greater control over constructor signatures, improving API clarity and preventing accidental positional argument usage for designated fields. The changes involve modifications to field processing, `__init__` signature generation, and the introduction of a new sentinel for marking keyword-only sections.
### Highlights
* **Keyword-Only Argument Support**: Introduced the `kw_only` parameter to the `c_class` decorator and the `field()` function, allowing fields to be designated as keyword-only arguments in the generated `__init__` method.
* **KW_ONLY Sentinel**: Added a `KW_ONLY` sentinel class and instance, mirroring `dataclasses.KW_ONLY`, to mark all subsequent fields within a `c_class` as keyword-only.
* **Improved `__init__` Generation**: Refactored and updated the `method_init()` function to correctly generate `__init__` signatures that respect `kw_only` settings, handling both class-level and field-level specifications, as well as the `KW_ONLY` sentinel.
* **New Test Cases**: Added comprehensive test cases to validate the new `kw_only` functionality, including class-level settings, field-specific overrides, and the behavior of the `KW_ONLY` sentinel.
🧠**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>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: add kw_only support for dataclass init generation [tvm-ffi]
Posted by "junrushao (via GitHub)" <gi...@apache.org>.
junrushao commented on code in PR #384:
URL: https://github.com/apache/tvm-ffi/pull/384#discussion_r2702066004
##########
tests/python/test_dataclasses_c_class.py:
##########
@@ -94,3 +98,61 @@ def test_cxx_class_init_subset_positional() -> None:
assert obj.optional_field == -1
obj.optional_field = 11
assert obj.optional_field == 11
+
+
+@c_class("testing.TestCxxKwOnly", kw_only=True)
+class _TestKwOnly:
+ x: int
+ y: int
+ z: int
+ w: int = 100
Review Comment:
Move this to `testing.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: 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: add kw_only support for dataclass init generation [tvm-ffi]
Posted by "gemini-code-assist[bot] (via GitHub)" <gi...@apache.org>.
gemini-code-assist[bot] commented on code in PR #384:
URL: https://github.com/apache/tvm-ffi/pull/384#discussion_r2663593511
##########
python/tvm_ffi/dataclasses/c_class.py:
##########
@@ -140,20 +177,36 @@ def decorator(super_type_cls: Type[_InputClsType]) -> Type[_InputClsType]: # no
return decorator
-def _inspect_c_class_fields(type_cls: type, type_info: TypeInfo) -> list[TypeField]:
+def _inspect_c_class_fields(
+ type_cls: type, type_info: TypeInfo
+) -> tuple[list[TypeField], int | None]:
if sys.version_info >= (3, 9):
type_hints_resolved = get_type_hints(type_cls, include_extras=True)
else:
type_hints_resolved = get_type_hints(type_cls)
- type_hints_py = {
- name: type_hints_resolved[name]
- for name in getattr(type_cls, "__annotations__", {}).keys()
- if get_origin(type_hints_resolved[name])
- not in [ # ignore non-field annotations
- ClassVar,
- InitVar,
- ]
- }
+
+ # Filter out ClassVar, InitVar, and detect KW_ONLY
+ annotations = getattr(type_cls, "__annotations__", {})
+ type_hints_py: dict[str, type] = {}
+ kw_only_start_idx: int | None = None
+ field_count = 0
+
+ for name in annotations.keys():
+ resolved_type = type_hints_resolved.get(name)
+ if resolved_type is None:
+ continue
+ origin = get_origin(resolved_type)
+ # Skip ClassVar and InitVar
+ if origin in [ClassVar, InitVar]:
+ continue
+ # Detect KW_ONLY sentinel
+ if resolved_type is _KW_ONLY_TYPE or isinstance(resolved_type, _KW_ONLY_TYPE):
Review Comment:

The check for the `KW_ONLY` sentinel can be simplified. The class `_KW_ONLY_TYPE` is an internal detail, and users are expected to use the `KW_ONLY` instance, as shown in the documentation. Supporting annotations with the internal class `_KW_ONLY_TYPE` directly is likely unnecessary and could lead to confusion. A simpler check, `isinstance(resolved_type, _KW_ONLY_TYPE)`, would correctly handle cases where the annotation is the `KW_ONLY` instance and aligns better with how the standard library `dataclasses` handles its `KW_ONLY` sentinel.
```suggestion
if isinstance(resolved_type, _KW_ONLY_TYPE):
```
##########
python/tvm_ffi/dataclasses/field.py:
##########
@@ -118,14 +150,28 @@ class PyBase:
obj = PyBase(v_i64=4)
obj.v_i32 # -> 16
+ Use ``kw_only=True`` to make a field keyword-only:
+
+ .. code-block:: python
+
+ @c_class("testing.TestCxxClassBase")
+ class PyBase:
+ v_i64: int
+ v_i32: int = field(kw_only=True)
+
+
+ obj = PyBase(4, v_i32=8) # v_i32 must be keyword
+
"""
if default is not MISSING and default_factory is not MISSING:
raise ValueError("Cannot specify both `default` and `default_factory`")
if not isinstance(init, bool):
raise TypeError("`init` must be a bool")
+ if kw_only is not MISSING and not isinstance(kw_only, bool):
+ raise TypeError("`kw_only` must be a bool or MISSING")
Review Comment:

The error message for the `kw_only` type check could be more precise for the user. The `MISSING` value is an internal sentinel for a non-provided argument and not a value a user can pass. The message could be simplified to state that `kw_only` must be a boolean, which is the expected type from a user's perspective.
```suggestion
raise TypeError("`kw_only` must be a bool")
```
--
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: add kw_only support for dataclass init generation [tvm-ffi]
Posted by "guan404ming (via GitHub)" <gi...@apache.org>.
guan404ming commented on PR #384:
URL: https://github.com/apache/tvm-ffi/pull/384#issuecomment-3764986299
Thanks!
--
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