forked from qodo-benchmark/prefect
-
Notifications
You must be signed in to change notification settings - Fork 0
Add documentation for prefect sdk generate CLI #155
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
tomerqodo
wants to merge
8
commits into
qodo_action_req_1_base_add_documentation_for_prefect_sdk_generate_cli_pr1
Choose a base branch
from
qodo_action_req_1_head_add_documentation_for_prefect_sdk_generate_cli_pr1
base: qodo_action_req_1_base_add_documentation_for_prefect_sdk_generate_cli_pr1
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
8 commits
Select commit
Hold shift + click to select a range
544ae48
Add documentation for prefect sdk generate CLI
desertaxle cfbaa0f
Revert unrelated CLI doc regeneration
desertaxle 8c5bd5c
Skip markdown doc tests for SDK how-to guide
desertaxle 0611e54
Rename page
desertaxle 17a4233
Rewrite SDK how-to guide to match docs style
desertaxle ce6ecab
Rewrite custom SDK how-to guide to match docs style
desertaxle 17c7fab
Move custom SDK guide to Advanced section
desertaxle 37e272d
update pr
tomerqodo 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
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,174 @@ | ||
| --- | ||
| title: How to generate a custom SDK for your deployments | ||
| sidebarTitle: Generate a Custom SDK | ||
| description: Generate a custom Python SDK from your deployments for IDE autocomplete and type checking. | ||
| --- | ||
|
|
||
| The `prefect sdk generate` command creates a typed Python file from your [deployments](/v3/concepts/deployments). This gives you IDE autocomplete and static type checking when triggering deployment runs programmatically. | ||
|
|
||
| <Note> | ||
| This feature is in **beta**. APIs may change in future releases. | ||
| </Note> | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| - An active Prefect API connection (Prefect Cloud or self-hosted server) | ||
| - At least one [deployment](/v3/how-to-guides/deployments/create-deployments) in your workspace | ||
|
|
||
| ## Generate an SDK from the CLI | ||
|
|
||
| Generate a typed SDK for all deployments in your workspace: | ||
|
|
||
| ```bash | ||
| prefect sdk generate --output ./my_sdk.py | ||
| ``` | ||
|
|
||
| ### Filter to specific flows or deployments | ||
|
|
||
| Generate an SDK for specific flows: | ||
|
|
||
| ```bash | ||
| prefect sdk generate --output ./my_sdk.py --flow my-etl-flow | ||
| ``` | ||
|
|
||
| Generate an SDK for specific deployments: | ||
|
|
||
| ```bash | ||
| prefect sdk generate --output ./my_sdk.py --deployment my-flow/production | ||
| ``` | ||
|
|
||
| Combine multiple filters: | ||
|
|
||
| ```bash | ||
| prefect sdk generate --output ./my_sdk.py \ | ||
| --flow etl-flow \ | ||
| --flow data-sync \ | ||
| --deployment analytics/daily | ||
| ``` | ||
|
|
||
| ## Run deployments with the generated SDK | ||
|
|
||
| The generated SDK provides a `deployments.from_name()` method that returns a typed deployment object: | ||
|
|
||
| {/* pmd-metadata: notest */} | ||
| ```python | ||
| from my_sdk import deployments | ||
|
|
||
| # Get a deployment by name | ||
| deployment = deployments.from_name("my-etl-flow/production") | ||
|
|
||
| # Run with parameters | ||
| future = deployment.run( | ||
| source="s3://my-bucket/data", | ||
| batch_size=100, | ||
| ) | ||
|
|
||
| # Get the flow run ID immediately | ||
| print(f"Started flow run: {future.flow_run_id}") | ||
|
|
||
| # Wait for completion and get result | ||
| result = future.result() | ||
| ``` | ||
|
|
||
| ### Configure run options | ||
|
|
||
| Use `with_options()` to set tags, scheduling, and other run configuration: | ||
|
|
||
| {/* pmd-metadata: notest */} | ||
| ```python | ||
| from my_sdk import deployments | ||
| from datetime import datetime, timedelta | ||
|
|
||
| future = deployments.from_name("my-etl-flow/production").with_options( | ||
| tags=["manual", "production"], | ||
| idempotency_key="daily-run-2024-01-15", | ||
| scheduled_time=datetime.now() + timedelta(hours=1), | ||
| flow_run_name="custom-run-name", | ||
| ).run( | ||
| source="s3://bucket", | ||
| ) | ||
| ``` | ||
|
|
||
| Available options: | ||
| - `tags`: Tags to apply to the flow run | ||
| - `idempotency_key`: Unique key to prevent duplicate runs | ||
| - `work_queue_name`: Override the work queue | ||
| - `as_subflow`: Run as a subflow of the current flow | ||
| - `scheduled_time`: Schedule the run for a future time | ||
| - `flow_run_name`: Custom name for the flow run | ||
|
|
||
| ### Override job variables | ||
|
|
||
| Use `with_infra()` to override work pool job variables: | ||
|
|
||
| {/* pmd-metadata: notest */} | ||
| ```python | ||
| from my_sdk import deployments | ||
|
|
||
| future = deployments.from_name("my-etl-flow/production").with_infra( | ||
| image="my-registry/my-image:latest", | ||
| cpu_request="2", | ||
| memory="8Gi", | ||
| ).run( | ||
| source="s3://bucket", | ||
| ) | ||
| ``` | ||
|
|
||
| The available job variables depend on your work pool type. The generated SDK provides type hints for the options available on each deployment's work pool. | ||
|
|
||
| ### Async usage | ||
|
|
||
| In an async context, use `run_async()`: | ||
|
|
||
| {/* pmd-metadata: notest */} | ||
| ```python | ||
| import asyncio | ||
| from my_sdk import deployments | ||
|
|
||
| async def trigger_deployment(): | ||
| future = await deployments.from_name("my-etl-flow/production").run_async( | ||
| source="s3://bucket", | ||
| ) | ||
| result = await future.result() | ||
| return result | ||
|
|
||
| # Run it | ||
| result = asyncio.run(trigger_deployment()) | ||
| ``` | ||
|
|
||
| ### Chain methods together | ||
|
|
||
| {/* pmd-metadata: notest */} | ||
| ```python | ||
| from my_sdk import deployments | ||
|
|
||
| future = ( | ||
| deployments.from_name("my-etl-flow/production") | ||
| .with_options(tags=["production"]) | ||
| .with_infra(memory="8Gi") | ||
| .run(source="s3://bucket", batch_size=100) | ||
| ) | ||
| ``` | ||
|
|
||
| ## Regenerate the SDK after changes | ||
|
|
||
| The SDK is generated from server-side metadata. Regenerate it when: | ||
| - Deployments are added, removed, or renamed | ||
| - Flow parameter schemas change | ||
| - Work pool job variable schemas change | ||
|
|
||
| The `generate` command overwrites the existing file: | ||
|
|
||
| ```bash | ||
| prefect sdk generate --output ./my_sdk.py | ||
| ``` | ||
|
|
||
| <Tip> | ||
| Add SDK regeneration to your CI/CD pipeline to keep it in sync with your deployments. | ||
| </Tip> | ||
|
|
||
| ## Further reading | ||
|
|
||
| - [Create deployments](/v3/how-to-guides/deployments/create-deployments) | ||
| - [Trigger ad-hoc deployment runs](/v3/how-to-guides/deployments/run-deployments) | ||
| - [Override job configuration](/v3/how-to-guides/deployments/customize-job-variables) |
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,82 @@ | ||
| --- | ||
| title: " " | ||
| sidebarTitle: prefect sdk | ||
| --- | ||
|
|
||
| # `prefect sdk` | ||
|
|
||
|
|
||
|
|
||
| ```command | ||
| prefect sdk [OPTIONS] COMMAND [ARGS]... | ||
| ``` | ||
|
|
||
|
|
||
|
|
||
| <Info> | ||
| Manage Prefect SDKs. (beta) | ||
| </Info> | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| ## `prefect sdk generate` | ||
|
|
||
|
|
||
|
|
||
| ```command | ||
| prefect sdk generate [OPTIONS] | ||
| ``` | ||
|
|
||
|
|
||
|
|
||
| <Info> | ||
| (beta) Generate a typed Python SDK from workspace deployments. | ||
|
|
||
| The generated SDK provides IDE autocomplete and type checking for your deployments. | ||
| Requires an active Prefect API connection (use `prefect cloud login` or configure | ||
| PREFECT_API_URL). | ||
|
|
||
| Examples: | ||
| Generate SDK for all deployments: | ||
| \$ prefect sdk generate --output ./my_sdk.py | ||
|
|
||
| Generate SDK for specific flows: | ||
| \$ prefect sdk generate --output ./my_sdk.py --flow my-etl-flow | ||
|
|
||
| Generate SDK for specific deployments: | ||
| \$ prefect sdk generate --output ./my_sdk.py --deployment my-flow/production | ||
| </Info> | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| <AccordionGroup> | ||
|
|
||
|
|
||
|
|
||
|
|
||
| <Accordion title="Options" defaultOpen> | ||
|
|
||
| <ResponseField name="--output"> | ||
| Output file path for the generated SDK. | ||
| </ResponseField> | ||
|
|
||
| <ResponseField name="--flow"> | ||
| Filter to specific flow(s). Can be specified multiple times. | ||
| </ResponseField> | ||
|
|
||
| <ResponseField name="--deployment"> | ||
| Filter to specific deployment(s). Can be specified multiple times. Use 'flow-name/deployment-name' format for exact matching. | ||
| </ResponseField> | ||
|
|
||
| </Accordion> | ||
|
|
||
| </AccordionGroup> |
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 |
|---|---|---|
|
|
@@ -8,12 +8,16 @@ | |
| from __future__ import annotations | ||
|
|
||
| import asyncio | ||
| import logging | ||
| from dataclasses import dataclass, field | ||
| from datetime import datetime, timezone | ||
| from typing import TYPE_CHECKING, Any | ||
| from uuid import UUID | ||
|
|
||
| import prefect | ||
|
|
||
| # Logger for SDK fetcher operations | ||
| logger = logging.getLogger(__name__) | ||
| from prefect._sdk.models import ( | ||
| DeploymentInfo, | ||
| FlowInfo, | ||
|
|
@@ -175,9 +179,7 @@ async def _fetch_work_pool( | |
| job_vars_schema: dict[str, Any] = {} | ||
| base_job_template = work_pool.base_job_template | ||
| if base_job_template and "variables" in base_job_template: | ||
| variables = base_job_template["variables"] | ||
| if isinstance(variables, dict): | ||
| job_vars_schema = variables | ||
| job_vars_schema = base_job_template["variables"] | ||
|
Comment on lines
179
to
+182
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. 2. Unchecked base_job_template variables • _fetch_work_pool() assigns job_vars_schema = base_job_template["variables"] without validating the value is a dict, even though job_vars_schema is typed as dict[str, Any]. • If base_job_template["variables"] is missing expected shape (e.g., None/list/str), later code that assumes a dict (e.g., calling .get(...)) can raise runtime exceptions instead of degrading gracefully. • This violates the requirement to explicitly handle null/empty/boundary cases and potential failure points for robust behavior. Agent prompt
|
||
|
|
||
| return WorkPoolInfo( | ||
| name=work_pool.name, | ||
|
|
@@ -215,7 +217,7 @@ async def _fetch_work_pools_parallel( | |
| results = await asyncio.gather(*tasks, return_exceptions=True) | ||
|
|
||
| work_pools: dict[str, WorkPoolInfo] = {} | ||
| for name, result in zip(pool_names_list, results, strict=True): | ||
| for name, result in zip(pool_names_list, results): | ||
| if isinstance(result, BaseException): | ||
| warnings.append( | ||
| f"Could not fetch work pool '{name}' - `with_infra()` will not be " | ||
|
|
@@ -316,6 +318,7 @@ async def fetch_sdk_data( | |
| errors: list[str] = [] | ||
|
|
||
| # Check authentication first | ||
| logger.debug("Checking authentication with Prefect API") | ||
| await _check_authentication(client) | ||
|
|
||
| # Build filters | ||
|
|
@@ -391,7 +394,7 @@ async def fetch_sdk_data( | |
|
|
||
| # If filtering by deployment name, check the full name matches | ||
| full_name = f"{flow_name}/{dep.name}" | ||
| if deployment_names and full_name not in deployment_names: | ||
| if deployment_names and dep.name not in deployment_names: | ||
| # Only include if the full name matches (filter was by name parts) | ||
| # Skip if user specified full names and this doesn't match | ||
| found_match = False | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1. Nonstandard logger initialization
📘 Rule violation✧ Quality• src/prefect/_sdk/fetcher.py initializes logger via logging.getLogger(__name__) without the required get_logger(...) pattern and without the required type annotation. • This breaks the standardized logging configuration/type-safety requirement and can lead to inconsistent logging behavior across modules. • It directly violates the compliance rule that mandates `logger: "logging.Logger" = get_logger("module_name")` for all logger instances.Agent prompt
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools