Skip to content

[STUBGEN] Let a file declare a namespace with a prefix directive - #752

Open
Seven-Streams wants to merge 4 commits into
apache:mainfrom
Seven-Streams:main-dev/2026-09-05/stubgen_prefix
Open

[STUBGEN] Let a file declare a namespace with a prefix directive#752
Seven-Streams wants to merge 4 commits into
apache:mainfrom
Seven-Streams:main-dev/2026-09-05/stubgen_prefix

Conversation

@Seven-Streams

@Seven-Streams Seven-Streams commented Sep 6, 2026

Copy link
Copy Markdown
Contributor

Summary

A file that starts with

// tvm-ffi-stubgen(prefix): tirx
// tvm-ffi-stubgen(skip): tirx.DispatchContext

owns every object registered directly under tirx (tirx.transform.* is another file). On each run the tool appends an empty object/<key> block for every such object that no file of the run defines yet, parents first, after the file's last block, and fills it as usual; an import-section is added after the prefix line when the file has none. skip leaves one object out, and a skipped object counts as not asked for, so the dependency error from #748 names it if something else needs it. Code outside the blocks is untouched, so a one-line skeleton is a valid starting point. Rust target only: a Python object/ block needs a class around it.

The roll-out happens in memory before stage 3, so the new blocks join declared and --check (#746) reports a file with pending blocks as stale. It runs after --init, which rewrites files on disk; --init honours skip as well. Builtin ffi.* keys are never rolled out. Two prefix lines in one file, or one prefix declared by two files, fail the run (exit 2); an unknown prefix is only a warning.

Changes

  • stub/cli.py: _roll_out_prefixes and _new_blocks, called once after --init, returning a failure count; _stage_2 subtracts the run's skip keys.
  • stub/consts.py: prefix and skip join the pipeline directive kinds.
  • docs/packaging/stubgen.rst: the directive in the reference.
  • tests/python/test_stubgen_rust.py: the testing namespace rolled out around an existing block, a skip, and a hand-written tail (--check 1, in place 0, --check 0); the same skeleton under --init (blocks written, skip honoured, --check 0); a prefix declared twice; exact matching, insertion order, and the builtin exclusion with a stubbed registry.

Testing

test_stubgen_rust.py + test_stubgen.py: 109 passed; pre-commit lint clean. Against libtvm_compiler.so, a tirx.rs with the two lines above, one more skip, and thirteen ty-map lines for the ir types rolls out 63 blocks in one run, leaves no super:: reference, keeps the trailing mod tail {}, and passes --check on the next run.

Signed-off-by: yuchuan <yuchuan.7streams@gmail.com>
Signed-off-by: yuchuan <yuchuan.7streams@gmail.com>
Signed-off-by: yuchuan <yuchuan.7streams@gmail.com>
@Seven-Streams
Seven-Streams force-pushed the main-dev/2026-09-05/stubgen_prefix branch from 1a0a02d to 9ee41f1 Compare September 6, 2026 02:50
Signed-off-by: yuchuan <yuchuan.7streams@gmail.com>
@Seven-Streams Seven-Streams changed the title Main dev/2026 09 05/stubgen prefix [STUBGEN] Let a file declare a namespace with a prefix directive Sep 6, 2026
@Seven-Streams
Seven-Streams marked this pull request as ready for review September 6, 2026 03:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant