Documentation
¶
Overview ¶
Package python is the Python-specific runner: build, test, and run Python services across native, Docker, and Nix backends.
It handles virtualenv / Poetry resolution, dependency installation, pytest invocation with filter wiring, and the agent-side helpers used by the python and python-fastapi agents.
Index ¶
- Constants
- func HasUVRuntime() bool
- func RunPythonLint(ctx context.Context, sourceDir string) (string, error)
- func SetPythonRuntimeContext(runtimeContext *basev0.RuntimeContext) *basev0.RuntimeContext
- type PythonAgentSettings
- type StructuredCase
- type StructuredFailure
- type StructuredSuite
- type StructuredTestRun
- type TestEvent
- type TestOptions
- type TestSummary
Constants ¶
const MaxCapturedOutputBytesPerCase = 32 * 1024
MaxCapturedOutputBytesPerCase mirrors the Go-runner cap. Pytest's JUnit `<failure>` elements bundle the traceback + captured stdout/stderr in the body — typically a few KB per case; very rarely exceeds 32KiB unless the test prints debug logs.
Variables ¶
This section is empty.
Functions ¶
func HasUVRuntime ¶
func HasUVRuntime() bool
HasUVRuntime checks if the uv Python package manager is available.
func RunPythonLint ¶
RunPythonLint runs ruff check and returns the output.
func SetPythonRuntimeContext ¶
func SetPythonRuntimeContext(runtimeContext *basev0.RuntimeContext) *basev0.RuntimeContext
SetPythonRuntimeContext determines the runtime context based on the requested context and available Python toolchain (uv).
Types ¶
type PythonAgentSettings ¶
type PythonAgentSettings struct {
PythonVersion string `yaml:"python-version"`
SourceDir string `yaml:"source-dir"`
}
PythonAgentSettings holds settings common to all Python service agents.
func (*PythonAgentSettings) PythonSourceDir ¶
func (s *PythonAgentSettings) PythonSourceDir() string
PythonSourceDir returns the configured source directory. Python convention: source is at the service root (not a subdirectory).
type StructuredCase ¶ added in v0.1.157
type StructuredCase struct {
Name string // local case name
FullName string // classname.name
State runtimev0.TestCaseState
Duration time.Duration
File string
Line int32
Output string // captured_output (populated only for FAILED/ERRORED)
Truncated bool
Failure *StructuredFailure
}
StructuredCase is one pytest invocation.
type StructuredFailure ¶ added in v0.1.157
type StructuredFailure struct {
Message string // one-line pytest summary
Detail string // traceback + captured output
Kind runtimev0.TestFailureKind
}
StructuredFailure carries the diagnostic detail for a failing case.
type StructuredSuite ¶ added in v0.1.157
type StructuredSuite struct {
Name string // file path (or class name when file unknown)
File string // absolute or repo-relative source path
Duration time.Duration
Cases []*StructuredCase
}
StructuredSuite is one source-file's worth of cases.
type StructuredTestRun ¶ added in v0.1.157
type StructuredTestRun struct {
// Suites — one per source file. Pytest's JUnit emits a flat list
// of `<testcase>`s; we group by file to mirror the Go runner's
// "one suite per package" shape. Source-file grouping is what
// developers actually reason about ("did the users tests pass?").
Suites []*StructuredSuite
// CoveragePct is scraped from pytest-cov's terminal output passed
// in alongside the JUnit XML. JUnit XML doesn't carry coverage.
CoveragePct float32
// contains filtered or unexported fields
}
StructuredTestRun is the python-side equivalent of the Go runner's StructuredTestRun. Built by ParsePytestJUnit; convertible to runtimev0.TestResponse via ToProtoResponse.
func ParsePytestJUnit ¶ added in v0.1.157
func ParsePytestJUnit(rawXML string, coverage float32) *StructuredTestRun
ParsePytestJUnit parses the contents of pytest's --junitxml output into a StructuredTestRun. Empty raw, malformed XML, and "no tests collected" all surface as a StructuredTestRun with zero suites and zero cases — the caller decides whether that's an error.
coverage is scraped separately (pytest-cov writes to stdout, not the XML); pass it through if available, 0 otherwise.
func RunPythonTestsStructured ¶ added in v0.1.157
func RunPythonTestsStructured(ctx context.Context, sourceDir string, envVars []*resources.EnvironmentVariable, opts ...TestOptions) (*StructuredTestRun, error)
RunPythonTestsStructured runs pytest with --junitxml output, parses the XML into the SOTA structured run, and returns it. Preferred entry point for new code that consumes the structured TestResponse shape.
Pytest's JUnit XML carries per-case file:line, captured stdout/stderr in the `<failure>` body for failed tests, and zero-body `<testcase>` for passed tests — fits the response-size discipline rule perfectly.
Coverage is scraped from pytest-cov's terminal output (XML doesn't carry coverage); the OnEvent callback still works against the verbose stream.
func (*StructuredTestRun) LegacyTestSummary ¶ added in v0.1.157
func (r *StructuredTestRun) LegacyTestSummary() *TestSummary
LegacyTestSummary returns the flat shape callers that haven't migrated still consume. Computed from the structured tree — single source of truth.
func (*StructuredTestRun) ToProtoResponse ¶ added in v0.1.157
func (r *StructuredTestRun) ToProtoResponse(runner, suiteName string, duration time.Duration) *runtimev0.TestResponse
ToProtoResponse constructs the runtimev0.TestResponse with both the structured tree (preferred) AND legacy flat fields populated (back-compat for non-migrated consumers).
runner is "pytest"; suiteName echoes TestRequest.suite. duration is the wall-clock for the whole run.
type TestEvent ¶
type TestEvent struct {
Action string // "pass" | "fail" | "skip"
Test string // test node id (e.g. "tests/test_admin.py::test_version")
}
TestEvent is a single per-test signal extracted from pytest's verbose output. Mirrors the go-side TestEvent shape so consumers can drive a uniform UI across both languages.
type TestOptions ¶
type TestOptions struct {
// OnEvent, when non-nil, is called for each per-test result line as
// pytest emits it. Lets the agent forward live progress to the TUI
// without waiting for the full summary at the end.
OnEvent func(TestEvent)
// CacheDir, when non-empty, persists the raw pytest output to
// <CacheDir>/last-test.txt for post-mortem debugging.
CacheDir string
// Target is a directory or node id (e.g. "tests/unit",
// "tests/test_admin.py::test_version"). Empty runs the default scope.
Target string
// Filters are name patterns passed to pytest's -k expression.
// Multiple values are joined with " or " — pytest's standard idiom.
Filters []string
// Verbose toggles pytest's -v flag. Defaults on at the helper level
// for backward compat; agents that want quieter output should pass
// false explicitly via this struct.
Verbose bool
// VerboseSet distinguishes "use default" from "explicitly false".
VerboseSet bool
// Timeout maps to pytest's --timeout=<sec>. Empty leaves the
// default. Accepts Go duration syntax ("30s", "2m") which we coerce
// into seconds for pytest.
Timeout string
// Coverage enables coverage instrumentation via pytest-cov when the
// project has it installed. Off by default.
Coverage bool
// ExtraArgs are appended verbatim to the pytest command — power-user
// passthrough for flags codefly does not model.
ExtraArgs []string
}
TestOptions controls pytest invocation.
type TestSummary ¶
type TestSummary struct {
Run int32
Passed int32
Failed int32
Skipped int32
Coverage float32
Failures []string
}
TestSummary holds the parsed results of a pytest run.
func ParsePytestVerbose ¶
func ParsePytestVerbose(output string) *TestSummary
ParsePytestVerbose parses the verbose text output from pytest -v --tb=short.
func RunPythonTests ¶
func RunPythonTests(ctx context.Context, sourceDir string, envVars []*resources.EnvironmentVariable, opts ...TestOptions) (*TestSummary, error)
RunPythonTests runs pytest and returns parsed results. Backward-compat wrapper: calls RunPythonTestsStructured internally and converts to the flat *TestSummary shape so existing consumers keep working unchanged.
New code should call RunPythonTestsStructured directly to get the full structured run (per-case file:line, captured-on-failure-only output, proto-friendly hierarchy).
func (*TestSummary) SummaryLine ¶
func (s *TestSummary) SummaryLine() string
SummaryLine formats a one-line summary string.