Add Python stub module generation for IDE autocomplete#8
Merged
Conversation
Member
|
look good, merging, but I am also opening an issue to update the docs accordingly (regarding |
Member
|
wait i think we need to also bump the version here Line 7 in d428706 |
This was referenced Apr 3, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
MaxPyLang lets users build Max patches in Python, but there's no way for IDE autocomplete or LLM agents (like Claude, Cursor, Copilot) to know what Max objects exist. Users have to memorize object names and manually write
MaxObject("cycle~")every time. The generated stub modules solve both problems: IDEs can autocomplete object names, and LLM agents can read the docstrings to understand what each object does, what arguments it takes, and how to use it.Additionally, when a user writes
MaxObject('multislir')(typo formultislider), the code silently creates a broken object with 0 inlets/outlets and only prints to stdout — easy to miss. Agents and IDEs have no way to detect this.Example
Before:
After:
An LLM agent can read the docstring for
cycle_tildeand learn it's a sinusoidal oscillator, what inlets/outlets it has, what attributes are available, and what related objects exist — all without needing access to Max documentation.Summary
objects/max.py,objects/msp.py,objects/jit.py) containing pre-instantiatedMaxObjectvariables with rich docstrings, so users get IDE autocomplete and can writepatch.place(cycle_tilde)instead ofpatch.place(MaxObject("cycle~"))sanitize_py_name()to convert Max names to valid Python identifiers (cycle~→cycle_tilde,jit.gl.render→jit_gl_render,2d.wave~→_2d_wave_tilde,if→if_,dict→dict_)print()error messages withwarnings.warn()usingUnknownObjectWarningfor unknown objects and bad arguments — IDEs highlight the issue, agents can detect it, and users can suppress withwarnings.filterwarnings__init__.pymodule docstring so agents can access everything viahelp(maxpylang)after pip installHow it works
import_objs("vanilla")(one-time setup, Max must be open)generate_stubs()reads the JSON and writesmaxpylang/objects/{package}.pywith one variable per objectfrom maxpylang.objects import cycle_tilde, metro, dac_tildeMaxObjectinstance —cycle_tilde.namereturns"cycle~"Generated stub format
Warnings for unknown objects
Before:
MaxObject('multislir')prints"ObjectError: multislir : creation : object unknown"to stdout — easy to miss.After: raises
UnknownObjectWarningwith file/line pointing to the user's code:warnings.filterwarnings("ignore", category=UnknownObjectWarning)obj.notknown()Agent discoverability
The
maxpylang/__init__.pymodule docstring now contains a complete API reference including:This means any LLM agent can read
__init__.pyand build correct patches without needing access to the repo's CLAUDE.md or Max documentation.Files changed
maxpylang/exceptions.py— New file.UnknownObjectWarning(UserWarning)classmaxpylang/tools/objfuncs/reffile.py— Replacedprint()withwarnings.warn()for unknown objectsmaxpylang/tools/objfuncs/args.py— Replacedprint()withwarnings.warn()for bad argumentsmaxpylang/tools/patchfuncs/placing.py— Removed duplicate print (already warned by constructor)maxpylang/objects/__init__.py— Suppresses warnings during stub import; re-exports from generated sub-modulesmaxpylang/__init__.py— Full API reference docstring; optionalfrom . import objectsmaxpylang/importobjs.py— Addedsanitize_py_name(),_build_docstring(),generate_stubs(); wired intoimport_objs()maxpylang/CLAUDE.md— Added available objects table and warning documentationEdge cases handled
~→_tilde,.→_,-→___objects/__init__.pydynamically regenerated based on existing stub files (supports third-party packages)Automated tests passed (43 tests)
Abstraction mode
When users create Max abstractions (custom
.maxpatsub-patches),MaxObject("my_synth")only works if the.maxpatfile exists in the current working directory. Otherwise it triggersUnknownObjectWarningand creates a broken object with 0 inlets/outlets. This blocks common workflows: building an abstraction and its parent patch in the same script, referencing abstractions in Max's search path or packages folder, or prototyping patch structure before the abstractions are built.abstraction=Truelets users declare an abstraction with known I/O counts, skipping the file look up entirely: