feat: Add Pydantic BaseModel and dataclass support for Python type inference#7329
Closed
devdattatalele wants to merge 3 commits intowindmill-labs:mainfrom
Closed
feat: Add Pydantic BaseModel and dataclass support for Python type inference#7329devdattatalele wants to merge 3 commits intowindmill-labs:mainfrom
devdattatalele wants to merge 3 commits intowindmill-labs:mainfrom
Conversation
…ference - Add AST-based detection of Pydantic BaseModel inheritance patterns - Add AST-based detection of @DataClass decorator (all variants) - Implement recursive field schema extraction with type inference - Add thread-safe stack-based module storage for nested parsing - Add RAII cleanup guard to ensure memory safety on all code paths - Add security limits: 200 fields max, 10 recursion levels max - Add comprehensive test coverage: 3 new tests for Pydantic/dataclass - Maintain 100% backward compatibility with existing type system This enables ML/AI practitioners to use Pydantic models as function parameters with automatic UI generation from model schemas. Implementation highlights: - Zero code execution: Pure AST analysis for safety - Thread-safe: Stack-based storage prevents race conditions - Memory-safe: RAII pattern guarantees cleanup - Security-hardened: Field count and recursion depth limits - Performance-optimized: Depth-limited recursion, lazy parsing Test results: All 12 tests passing (9 existing + 3 new) Closes windmill-labs#4700 🤖 Generated with Claude Code (https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit addresses critical bugs and improves error handling in the Python parser for Pydantic BaseModel and dataclass support. ## Critical Fixes 1. **Thread-local storage RAII pattern**: Fixed bug where parse failures could leave the module stack in an inconsistent state. Now uses proper functional composition with .ok().map() to ensure cleanup always happens. 2. **Recursion depth warnings**: Added explicit warning messages when the recursion depth limit (10 levels) is reached during type extraction. Made the limit a named constant for clarity. 3. **Unsupported type warnings**: Added informative warning messages for unsupported type annotations (Union types and forward references) to help users understand why their types aren't being inferred. ## Improvements - Added 10 comprehensive test cases covering: - Empty Pydantic models - List[T] and Optional[T] types - Dataclass with decorator arguments - Dict types - Regular classes (non-model types) - Invalid syntax handling - Datetime fields - Multiple model definitions - Nested models - All 21 tests pass successfully ## Testing Verified that: - Parser handles malformed code gracefully - RAII cleanup works correctly with early returns - Warning messages are clear and actionable - No memory leaks or panics Closes windmill-labs#4700
…ated src/pydantic_parser.rs with thread-local storage, model detection, and type extraction logic. Moved 12 Pydantic tests to tests/pydantic_tests.rs and removed duplicate code from lib.rs. All 21 tests passing.
Collaborator
|
moved to #7497 |
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Summary
Adds Pydantic BaseModel and Python dataclass support to the Python parser, enabling automatic UI generation for complex data structures.
Closes #4700
Changes
Modified
backend/parsers/windmill-parser-py/src/lib.rsto detect Pydantic models and dataclasses via AST analysis. The parser now recognizes these custom types and extracts their field schemas without executing any Python code.Implementation
The parser uses rustpython AST to pattern match class inheritance and decorators, then recursively extracts field types. Added thread-safe module storage with RAII cleanup and security limits (200 fields max, 10 recursion levels).
Example
Before:
After:
Windmill now automatically generates a form with name, age, and email fields from the User model.
Testing
Added 3 test cases covering basic models, dataclasses, and nested models. All 12 tests pass. No breaking changes to existing functionality.
Known Limitations
Field default values and validation metadata are not yet extracted. Forward references and union types are not supported. These can be addressed in follow-up PRs if needed.