Skip to content

Commit c6ff560

Browse files
committed
started a colloection of prompts
1 parent b587604 commit c6ff560

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Prompt
2+
3+
Please convert all pydantic model fields that use `Field()` with default values to use the Annotated pattern instead.
4+
Follow these guidelines:
5+
6+
1. Move default values outside of `Field()` like this: `field_name: Annotated[field_type, Field(description="")] = default_value`.
7+
2. Keep all other parameters like validation_alias and descriptions inside `Field()`.
8+
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`.
9+
4. Add the import: `from common_library.basic_types import DEFAULT_FACTORY` if it's not already present.
10+
5. If `Field()` has no parameters (empty), don't use Annotated at all. Just use: `field_name: field_type = default_value`.
11+
6. Leave any model validations, `model_config` settings, and `field_validators` untouched.
12+
13+
## Examples
14+
15+
### Before:
16+
17+
```python
18+
from pydantic import BaseModel, Field
19+
20+
class UserModel(BaseModel):
21+
name: str = Field(default="Anonymous", description="User's display name")
22+
age: int = Field(default=18, ge=0, lt=120)
23+
tags: list[str] = Field(default_factory=list, description="User tags")
24+
metadata: dict[str, str] = Field(default_factory=dict)
25+
is_active: bool = Field(default=True)
26+
```
27+
28+
- **After**
29+
30+
```python
31+
from typing import Annotated
32+
from pydantic import BaseModel, Field
33+
from common_library.basic_types import DEFAULT_FACTORY
34+
35+
class UserModel(BaseModel):
36+
name: Annotated[str, Field(description="User's display name")] = "Anonymous"
37+
age: Annotated[int, Field(ge=0, lt=120)] = 18
38+
tags: Annotated[list[str], Field(default_factory=list, description="User tags")] = DEFAULT_FACTORY
39+
metadata: Annotated[dict[str, str], Field(default_factory=dict)] = DEFAULT_FACTORY
40+
is_active: bool = True
41+
```
42+
43+
## Another Example with Complex Fields
44+
45+
### Before:
46+
47+
```python
48+
from pydantic import BaseModel, Field, field_validator
49+
from datetime import datetime
50+
51+
class ProjectModel(BaseModel):
52+
id: str = Field(default_factory=uuid.uuid4, description="Unique project identifier")
53+
name: str = Field(default="Untitled Project", min_length=3, max_length=50)
54+
created_at: datetime = Field(default_factory=datetime.now)
55+
config: dict = Field(default={"version": "1.0", "theme": "default"})
56+
57+
@field_validator("name")
58+
def validate_name(cls, v):
59+
if v.isdigit():
60+
raise ValueError("Name cannot be only digits")
61+
return v
62+
```
63+
64+
### After:
65+
66+
```python
67+
from typing import Annotated
68+
from pydantic import BaseModel, Field, field_validator
69+
from datetime import datetime
70+
from common_library.basic_types import DEFAULT_FACTORY
71+
72+
class ProjectModel(BaseModel):
73+
id: Annotated[str, Field(default_factory=uuid.uuid4, description="Unique project identifier")] = DEFAULT_FACTORY
74+
name: Annotated[str, Field(min_length=3, max_length=50)] = "Untitled Project"
75+
created_at: Annotated[datetime, Field(default_factory=datetime.now)] = DEFAULT_FACTORY
76+
config: dict = {"version": "1.0", "theme": "default"}
77+
78+
@field_validator("name")
79+
def validate_name(cls, v):
80+
if v.isdigit():
81+
raise ValueError("Name cannot be only digits")
82+
return v
83+
```

0 commit comments

Comments
 (0)