python

package
v0.21.0-rc.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 29, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package python implements a tree-sitter based Python parser for extracting Cog predictor signatures. It walks the concrete syntax tree to extract imports, class definitions, function parameters with type annotations and default values, and Input() call keyword arguments.

This parser is Python-specific. Future languages (e.g. Node.js) would get their own parser package under pkg/schema/.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllChildren added in v0.19.0

func AllChildren(n *sitter.Node) []*sitter.Node

AllChildren returns all children (named and anonymous) of a node.

func CollectImports added in v0.19.0

func CollectImports(root *sitter.Node, source []byte) *schema.ImportContext

func Content added in v0.19.0

func Content(n *sitter.Node, source []byte) string

Content returns the source text for a node.

func InheritsFromBaseModel added in v0.19.0

func InheritsFromBaseModel(classNode *sitter.Node, source []byte, imports *schema.ImportContext) bool

func NamedChildren added in v0.19.0

func NamedChildren(n *sitter.Node) []*sitter.Node

NamedChildren returns all named children of a node.

func ParsePredictor

func ParsePredictor(source []byte, targetRef string, mode schema.Mode, sourceDir string) (*schema.PredictorInfo, error)

ParsePredictor parses Python source and extracts predictor information. targetRef is the class or function name (e.g. "Predictor" or "run"). mode controls whether we look for predict or train method. sourceDir is the project root for resolving cross-file imports. Pass "" if unknown.

func ParsePredictorWithSourcePath

func ParsePredictorWithSourcePath(source []byte, targetRef string, mode schema.Mode, sourceDir string, sourcePath string) (*schema.PredictorInfo, error)

ParsePredictorWithSourcePath parses Python source and uses sourcePath, relative to sourceDir, to resolve package-relative imports.

func ParseWithOptions

func ParseWithOptions(opts ParserOptions) (*schema.PredictorInfo, error)

func TypedDictClassInfo added in v0.19.1

func TypedDictClassInfo(classNode *sitter.Node, source []byte, imports *schema.ImportContext, typedDicts map[string]bool) (bool, bool, []string)

func UnwrapClass added in v0.19.0

func UnwrapClass(node *sitter.Node) *sitter.Node

func UnwrapFunction added in v0.19.0

func UnwrapFunction(node *sitter.Node) *sitter.Node

Types

type ModuleSummary

type ModuleSummary struct {
	Imports    *schema.ImportContext
	Models     schema.ModelClassMap
	TypedDicts map[string]bool
	SourcePath string
}

type ParseState

type ParseState struct {
	Options ParserOptions
	Phase   parsePhase
	Tree    *sitter.Tree
	Root    *sitter.Node

	Imports           *schema.ImportContext
	Scope             moduleScope
	ModelCtx          *modelParseContext
	Models            schema.ModelClassMap
	LoadedModules     map[string]ModuleSummary
	Registry          *inputRegistry
	Target            *TargetCallable
	FileCtx           *pythonFileContext
	TargetFunc        *targetFunction
	Inputs            *schema.OrderedMap[string, schema.InputField]
	Output            schema.SchemaType
	SupportsStreaming bool
	OutputSet         bool
}

type ParserOptions

type ParserOptions struct {
	Source               []byte
	TargetRef            string
	Mode                 schema.Mode
	SourceDir            string
	SourcePath           string
	DisableLegacyPredict bool
}

type TargetCallable

type TargetCallable struct {
	ClassName    string
	FunctionName string
	MethodName   string
	Node         *sitter.Node
	IsMethod     bool
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL