Python configuration
Python generation settings live under:
yaml
targets:
- name: default
gen:
python:
asynchronous: true
models: pydantic
max_params: 10targets[].gen.python
- Optional in the config schema, but required for Python output.
- Python is the only fully supported generator today.
targets[].gen.python.asynchronous
- Type: boolean
- Default:
true(in the template created bynorm init) true- async repository methods and async drivers (psycopg,aiosqlite,asyncmy, etc.)false- sync repository methods and sync drivers (psycopg.Connection,sqlite3, etc.)
Async (default)
python
from psycopg import AsyncConnection as Connection
class Repo:
def __init__(self, db: Connection) -> None:
self.db = db
async def get_author_by_id(self, id: int) -> Author | None:
async with self.db.cursor() as cur:
await cur.execute(query, params)
...go
// coming soonrust
// coming soontypescript
// coming soonSync
Set asynchronous: false in norm.yaml:
yaml
gen:
python:
asynchronous: falseGenerated code uses a synchronous connection and plain def methods:
python
from psycopg import Connection
class Repo:
def __init__(self, db: Connection) -> None:
self.db = db
def get_one(
self,
id: int,
) -> Author | None:
query = """
SELECT
authors.id AS id,
authors.name AS name,
authors.rating AS rating
FROM authors
WHERE
authors.id = %(id)s
"""
params = {
"id": id,
}
with self.db.cursor() as cur:
cur.execute(query, params)
result = cur.fetchone()
if result is None:
return None
result = Author(
id=result[0],
name=result[1],
rating=result[2],
)
return resultgo
// coming soonrust
// coming soontypescript
// coming soonUse sync mode when the application already uses blocking database access, such as scripts, sync workers, or legacy stacks. Async mode fits asyncio applications and async pools (psycopg async, aiosqlite, etc.). Macros, dynamic filters, partial updates, and embeds behave the same in both modes.
targets[].gen.python.models
- Type: string
- Default:
pydantic - Supported values:
pydantic- generatespydantic.BaseModeltypesdataclasses- generates@dataclasstypes
Example generated model with dataclasses:
python
from dataclasses import dataclass
@dataclass
class Author:
id: int
name: str
rating: int | Nonego
// coming soonrust
// coming soontypescript
// coming soontargets[].gen.python.max_params
- Type: integer
- Default:
10in the template fromnorm init(see General configuration) - Controls when nORM groups query parameters into a generated input model instead of many method arguments.
- Lower values (for example
1) mean more queries get a single*Paramsmodel sooner. - See Fetching records.