Loading...
Loading...
Modern Python 3.12+ patterns your AI agent should use. Type hints, async/await, Pydantic v2, uv, match statements, and project structure.
npx skill4agent add ofershap/python-best-practices python-best-practicesfrom typing import Union, List, Dict, Optional
def process(items: List[str]) -> Optional[Dict[str, Union[int, str]]]:
...def process(items: list[str]) -> dict[str, int | str] | None:
...list[str]X | Ytyping.Listtyping.Uniontyping.Optionalfrom typing import TypeVar
T = TypeVar("T")
def max(args: list[T]) -> T:
...def max[T](args: list[T]) -> T:
...class Bag[T]:class Bag(Generic[T]):def http_error(status: int) -> str:
if status == 400:
return "Bad request"
elif status == 404:
return "Not found"
elif status == 418:
return "I'm a teapot"
else:
return "Something's wrong"def http_error(status: int) -> str:
match status:
case 400:
return "Bad request"
case 404:
return "Not found"
case 418:
return "I'm a teapot"
case _:
return "Something's wrong"case 401 | 403 | 404:from pydantic import BaseModel, validator, root_validator
class Model(BaseModel):
x: list[int]
class Config:
validate_assignment = True
@validator("x", each_item=True)
def validate_x(cls, v):
return v * 2
@root_validator
def check_a_b(cls, values):
...from pydantic import BaseModel, field_validator, model_validator, ConfigDict
class Model(BaseModel):
model_config = ConfigDict(validate_assignment=True)
x: list[int]
@field_validator("x", mode="each")
@classmethod
def validate_x(cls, v: int) -> int:
return v * 2
@model_validator(mode="after")
def check_a_b(self) -> "Model":
...
return selfclass Configmodel_configfield_validatormodel_validatorpip install -r requirements.txt
poetry inituv init
uv add requests pydantic
uv sync# setup.py
from setuptools import setup
setup(name="myapp", version="0.1", ...)# pyproject.toml
[project]
name = "myapp"
version = "0.1"
dependencies = ["requests"]import os
path = os.path.join(os.getcwd(), "data", "file.txt")
if os.path.exists(path):
with open(path) as f:
...from pathlib import Path
path = Path.cwd() / "data" / "file.txt"
if path.exists():
path.read_text()Path.read_text()write_text()open()"User %s has %d items" % (name, count)
"User {} has {} items".format(name, count)f"User {name} has {count} items"def get_user() -> dict:
return {"name": "Alice", "age": 30}from dataclasses import dataclass
@dataclass
class User:
name: str
age: int
def get_user() -> User:
return User(name="Alice", age=30)import asyncio
results = await asyncio.gather(f1(), f2(), f3())import asyncio
async with asyncio.TaskGroup() as tg:
t1 = tg.create_task(f1())
t2 = tg.create_task(f2())
t3 = tg.create_task(f3())
results = (t1.result(), t2.result(), t3.result())try:
...
except (ValueError, TypeError) as e:
...try:
...
except* TypeError as e:
print(f"caught {type(e)} with nested {e.exceptions}")
except* OSError as e:
...except*import toml
data = toml.load("pyproject.toml")import tomllib
with open("pyproject.toml", "rb") as f:
data = tomllib.load(f)class Child(Parent):
def method(self) -> str:
return "child"from typing import override
class Child(Parent):
@override
def method(self) -> str:
return "child"match x:
case (a, b):
if a > b:
...match x:
case (a, b) if a > b:
...iffrom typing import Iterable, Mappingfrom collections.abc import Iterable, Mappingclass Bag[T]:
def __iter__(self) -> Iterator[T]:
...
def add(self, arg: T) -> None:
...type ListOrSet[T] = list[T] | set[T]@model_validator(mode="before")
@classmethod
def check_card_number_not_present(cls, data: Any) -> Any:
if isinstance(data, dict) and "card_number" in data:
raise ValueError("'card_number' should not be included")
return datacase 401 | 403 | 404:
return "Not allowed"from typing import List, Dict, Union, OptionallistdictX | YX | None@validator@root_validatorclass Configos.pathpathlib.Pathsetup.pysetup.cfgpyproject.toml%.format()tomltomllibTypeVarasyncio.gatherfrom typing import Iterable, Mappingfrom collections.abc import ...