-
Notifications
You must be signed in to change notification settings - Fork 100
feat: add top level async streaming #655
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jakelorocco
wants to merge
7
commits into
main
Choose a base branch
from
jal/finalize-async
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7a9a36f
feat: add top level async streaming
jakelorocco 14b00ce
Merge branch 'main' into jal/finalize-async
jakelorocco fa9b43c
feat: add examples for top level streaming
jakelorocco 9768fd9
fix: pr comments
jakelorocco e6216e8
fix: pr comments and add simpler example
jakelorocco fffa659
fix: issues with docs gen / quality
jakelorocco 4652cdf
feat: add test files for mypy to catch overload typing issues
jakelorocco File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| """CLI for fixing async calls after top-level ainstruct, aquery, and aact contract change..""" | ||
|
|
||
| from enum import StrEnum | ||
|
|
||
| import typer | ||
|
|
||
| fix_app = typer.Typer(name="fix", help="Fix code for API changes.") | ||
|
|
||
|
|
||
| class _FixMode(StrEnum): | ||
| """Types of fixes that can be applied.""" | ||
|
|
||
| ADD_AWAIT_RESULT = "add-await-result" | ||
| ADD_STREAM_LOOP = "add-stream-loop" | ||
|
|
||
|
|
||
| from cli.fix.commands import fix_async # noqa: E402 | ||
|
|
||
| fix_app.command("async")(fix_async) |
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not reviewed. |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| """CLI command for `m fix async`.""" | ||
|
|
||
| from pathlib import Path | ||
|
|
||
| import typer | ||
|
|
||
| from cli.fix import _FixMode | ||
|
|
||
|
|
||
| def fix_async( | ||
| path: str = typer.Argument(..., help="File or directory to scan"), | ||
| mode: _FixMode = typer.Option( | ||
| _FixMode.ADD_AWAIT_RESULT, "--mode", "-m", help="Fix strategy to apply" | ||
| ), | ||
| dry_run: bool = typer.Option( | ||
| False, "--dry-run", help="Report locations without modifying files" | ||
| ), | ||
| ): | ||
| """Fix async calls (aact, ainstruct, aquery) for the await_result default change. | ||
|
|
||
| Args: | ||
| path: File or directory to scan. | ||
| mode: Fix strategy to apply. | ||
| dry_run: If ``True``, report locations without modifying files. | ||
|
|
||
| Raises: | ||
| typer.Exit: If *path* does not exist. | ||
|
|
||
| \b | ||
| Modes: | ||
| add-await-result (default) Adds await_result=True to each call so it | ||
| blocks until the result is ready. Use this if you don't | ||
| need to stream partial results. | ||
| add-stream-loop Inserts a `while not r.is_computed(): await r.astream()` | ||
| loop after each call. This only works if you passed a | ||
| streaming model option (e.g. stream=True) to the call; | ||
| otherwise the loop will finish immediately. | ||
|
|
||
| \b | ||
| Best practices: | ||
| - Run with --dry-run first to review what will be changed. | ||
| - Only run a given mode once per file. The tool detects prior fixes and | ||
| skips calls that already have await_result=True or a stream loop, but | ||
| it is safest to treat it as a one-shot migration. | ||
| - Do not run both modes on the same file. If a stream loop is already | ||
| present, add-await-result will skip that call (and vice versa). | ||
|
|
||
| \b | ||
| Detection notes: | ||
| - Most import styles are detected: `import mellea`, | ||
| `from mellea import MelleaSession`, | ||
| `from mellea.stdlib.functional import aact`, module aliases, etc. | ||
| - Calls that are already followed by `await r.avalue()`, | ||
| `await r.astream()`, or a `while not r.is_computed()` loop are | ||
| automatically skipped, even when nested inside if/try/for blocks. | ||
| """ | ||
| from cli.fix.fixer import fix_path | ||
|
|
||
| target = Path(path) | ||
| if not target.exists(): | ||
| typer.echo(f"Error: {path} does not exist", err=True) | ||
| raise typer.Exit(code=1) | ||
|
|
||
| result = fix_path(target, mode, dry_run=dry_run) | ||
|
|
||
| if result.total_fixes == 0: | ||
| typer.echo("No fixable calls found.") | ||
| return | ||
|
|
||
| action = "Found" if dry_run else "Fixed" | ||
| typer.echo( | ||
| f"{action} {result.total_fixes} call(s) in {result.files_affected} file(s):" | ||
| ) | ||
| for loc in result.locations: | ||
| typer.echo( | ||
| f" {loc.filepath}:{loc.line} - {loc.function_name}() [{loc.call_style}]" | ||
| ) |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.