-
Notifications
You must be signed in to change notification settings - Fork 32
♻️ Maintenance: Updates on new Annotated type style and llm-prompts
#7749
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d047aa9
doc
pcrespov 5581d6e
✨ Refactor project and rabbitmq basic types to use TypeAlias with Ann…
pcrespov 4364d1e
✨ Add SafeQueryStr type alias for safe URL query parameters
pcrespov 7713f06
✨ Refactor string truncation types to use Annotated with StringConstr…
pcrespov 332e7a7
✨ Enhance ShortTruncatedStr tests to cover additional edge cases for …
pcrespov b587604
✨ Update get_solvers_filters to use SafeQueryStr for solver_id and ve…
pcrespov c6ff560
started a colloection of prompts
pcrespov 71cf70a
fixes mypy
pcrespov 913b627
@bisgaard-itis review: verbatim
pcrespov 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,84 @@ | ||
| # Prompt | ||
|
|
||
| ``` | ||
| Please convert all pydantic model fields that use `Field()` with default values to use the Annotated pattern instead. | ||
| Follow these guidelines: | ||
|
|
||
| 1. Move default values outside of `Field()` like this: `field_name: Annotated[field_type, Field(description="")] = default_value`. | ||
| 2. Keep all other parameters like validation_alias and descriptions inside `Field()`. | ||
| 3. For fields using default_factory, keep that parameter as is in the `Field()` constructor, but set the default value outside to DEFAULT_FACTORY from common_library.basic_types. Example: `field_name: Annotated[dict_type, Field(default_factory=dict)] = DEFAULT_FACTORY`. | ||
| 4. Add the import: `from common_library.basic_types import DEFAULT_FACTORY` if it's not already present. | ||
| 5. If `Field()` has no parameters (empty), don't use Annotated at all. Just use: `field_name: field_type = default_value`. | ||
| 6. Leave any model validations, `model_config` settings, and `field_validators` untouched. | ||
| ``` | ||
| ## Examples | ||
|
|
||
| ### Before: | ||
|
|
||
| ```python | ||
| from pydantic import BaseModel, Field | ||
|
|
||
| class UserModel(BaseModel): | ||
| name: str = Field(default="Anonymous", description="User's display name") | ||
| age: int = Field(default=18, ge=0, lt=120) | ||
| tags: list[str] = Field(default_factory=list, description="User tags") | ||
| metadata: dict[str, str] = Field(default_factory=dict) | ||
| is_active: bool = Field(default=True) | ||
| ``` | ||
|
|
||
| - **After** | ||
|
|
||
| ```python | ||
| from typing import Annotated | ||
| from pydantic import BaseModel, Field | ||
| from common_library.basic_types import DEFAULT_FACTORY | ||
|
|
||
| class UserModel(BaseModel): | ||
| name: Annotated[str, Field(description="User's display name")] = "Anonymous" | ||
| age: Annotated[int, Field(ge=0, lt=120)] = 18 | ||
| tags: Annotated[list[str], Field(default_factory=list, description="User tags")] = DEFAULT_FACTORY | ||
| metadata: Annotated[dict[str, str], Field(default_factory=dict)] = DEFAULT_FACTORY | ||
| is_active: bool = True | ||
| ``` | ||
|
|
||
| ## Another Example with Complex Fields | ||
|
|
||
| ### Before: | ||
|
|
||
| ```python | ||
| from pydantic import BaseModel, Field, field_validator | ||
| from datetime import datetime | ||
|
|
||
| class ProjectModel(BaseModel): | ||
| id: str = Field(default_factory=uuid.uuid4, description="Unique project identifier") | ||
| name: str = Field(default="Untitled Project", min_length=3, max_length=50) | ||
| created_at: datetime = Field(default_factory=datetime.now) | ||
| config: dict = Field(default={"version": "1.0", "theme": "default"}) | ||
|
|
||
| @field_validator("name") | ||
| def validate_name(cls, v): | ||
| if v.isdigit(): | ||
| raise ValueError("Name cannot be only digits") | ||
| return v | ||
| ``` | ||
|
|
||
| ### After: | ||
|
|
||
| ```python | ||
| from typing import Annotated | ||
| from pydantic import BaseModel, Field, field_validator | ||
| from datetime import datetime | ||
| from common_library.basic_types import DEFAULT_FACTORY | ||
|
|
||
| class ProjectModel(BaseModel): | ||
| id: Annotated[str, Field(default_factory=uuid.uuid4, description="Unique project identifier")] = DEFAULT_FACTORY | ||
| name: Annotated[str, Field(min_length=3, max_length=50)] = "Untitled Project" | ||
| created_at: Annotated[datetime, Field(default_factory=datetime.now)] = DEFAULT_FACTORY | ||
| config: dict = {"version": "1.0", "theme": "default"} | ||
|
|
||
| @field_validator("name") | ||
| def validate_name(cls, v): | ||
| if v.isdigit(): | ||
| raise ValueError("Name cannot be only digits") | ||
| return v | ||
| ``` |
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
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
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
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
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.