Development Guidelines
The rules a change to this repository follows. How to set up, run and test the stack is in CONTRIBUTING.md, and the conventions, with the reasons for them, are in CLAUDE.md.
Python
- Write for Python 3.11, with type annotations.
- Use Pydantic v2:
field_validator,model_validator,ConfigDict, andBaseSettingsfrompydantic_settings. -
Import application code by its full path, rooted at
backend.app:from backend.app.models.metrics.metric import RawMetric. Never root an import atapp: Python loadsapp.models.metrics.metricas a different module frombackend.app.models.metrics.metric, with different classes. This search finds such an import, and prints nothing when there is none: -
A new core model's module goes in
CORE_MODEL_MODULESinbackend/app/models/__init__.py, whichregister_core_models()imports.
Lint and format
- Ruff formats and lints the Python and sorts its imports; its configuration is
[tool.ruff]inpyproject.toml. Do not add black, isort or flake8 back. make formatformats and fixes the Python in place.make lintruns what thelintcheck runs.- CI, the pre-commit hook and the virtual environment install the same ruff version.
Dialogs
- A modal dialog in the dashboard is
Dialog, fromfrontend/src/components/ui/Dialog.tsx. Do not build an overlay by hand:frontend/src/tests/a11y/dialog-sweep.test.tsrefusesaria-modal, a<dialog>element, andfixedwithinset-0in one class string anywhere else. - While a
Dialogis open, Tab and Shift+Tab stay inside it, the page behind it isinert, and the page does not scroll. Escape callsonClose, unlessdismissible={false}orbusyis set. - Content inside a
Dialognever receives Escape: the dialog takes the key in the capture phase ondocumentand stops it there, even when it refuses to close, so a tooltip or combobox inside a dialog cannot use Escape. - Focus opens on
initialFocusRef, else on the first element markeddata-autofocus, else on the first control. Give a confirmation that deletes something its Cancel button asinitialFocusRef. - On close, focus goes to
returnFocusif you pass one, else back to the element that opened the dialog. - Set a live region's text as the dialog closes with the third value of
useDeferredAnnouncement: it writes the text after the page behind the dialog is no longer inert. - In a Jest test, render with
renderInAppRootfromfrontend/src/tests/helpers/renderInAppRoot.tsx. Under Jest, aDialogrendered without it throws.
import { useRef, useState } from 'react';
import { Dialog, useDeferredAnnouncement } from '@/components/ui/Dialog';
export function RenameFlag({ rename }: { rename: (name: string) => Promise<void> }) {
const [open, setOpen] = useState(false);
const [name, setName] = useState('');
const [status, , announceAfterClose] = useDeferredAnnouncement();
const nameField = useRef<HTMLInputElement>(null);
const save = async () => {
await rename(name);
setOpen(false);
announceAfterClose(`Renamed to ${name}.`);
};
return (
<>
<button type="button" onClick={() => setOpen(true)}>
Rename
</button>
<p role="status">{status}</p>
<Dialog open={open} onClose={() => setOpen(false)} title="Rename flag" initialFocusRef={nameField}>
<label htmlFor="flag-name">Name</label>
<input id="flag-name" ref={nameField} value={name} onChange={(e) => setName(e.target.value)} />
<button type="button" onClick={() => setOpen(false)}>
Cancel
</button>
<button type="button" onClick={save}>
Save
</button>
</Dialog>
</>
);
}
- An in-page confirmation, a question shown beside the button that opened it
rather than over the page, is
InlineConfirm, fromfrontend/src/components/ui/InlineConfirm.tsx. Mark its Cancel buttondata-autofocus: focus opens there, Escape cancels, and on close focus goes back to the button that opened it, or tofallbackFocuswhen that button is gone. It traps no focus and leaves the page behind it usable.frontend/src/tests/a11y/dialog-sweep.test.tsrefuses a string literal equal todialogoralertdialogoutsideDialog.tsxandInlineConfirm.tsx.
Pull requests
- Write the title as a conventional commit,
type(optional scope): description, withtypeone offeat fix perf deps docs build ci refactor test chore revert style. Theconventional-titlecheck refuses any other title. - Sign off every commit and add a regression test to every bug fix:
CONTRIBUTING.mdand the Testing Guide say how.