polyfactory
Installation
SKILL.md
polyfactory
Polyfactory is a typed mock-data factory library: declare ModelFactory[T] (or DataclassFactory, MsgspecFactory, AttrsFactory, TypedDictFactory) and .build() returns a populated instance of T. The matching factory base inspects the model's annotations and supported constraints so tests do not need hand-written happy-path fixtures.
In Litestar projects, polyfactory's pytest plugin is the canonical way to feed TestClient.post(...) / AsyncTestClient.put(...) payloads. The companion skill litestar:litestar-testing covers the request side.
Code Style Rules
- PEP 604 unions:
T | None, neverOptional[T]. - One factory per model. Don't reuse a factory across unrelated models — clarity beats DRY when the test fails at 3am.
- Pick the right factory base by backend:
ModelFactory(Pydantic),DataclassFactory,MsgspecFactory,AttrsFactory,TypedDictFactory. A mismatched base raisesConfigurationException. - Prefer
register_fixtureover hand-rolled@pytest.fixturewrappers. Call it in a pytest-discovered test module orconftest.pyso the injected fixture is collected.