Adding a Generator
Generators are plain Python functions registered in the GENERATORS_REGISTRY. Adding one requires writing the function, documenting it, and placing it in the right module.
1. Write the function
A generator is a regular Python function. It must follow these rules:
- It must have a return type annotation
- It must only use types compatible with SQL —
str,int,float,bool,date,datetime,UUID - It must not use
**kwargs - It may use
*args(var positional), but not simultaneously with other positional-or-keyword parameters
Use get_faker() to access the global Faker instance. This ensures the generator respects the active locale and generation settings.
2. Document it
Follow the docstring format to document the generator. Copia uses the docstring to generate the in-app help and the documentation site.
def job() -> str:
"""Generate a random job title.
Locale dependent:
yes
Returns:
A random job title string.
"""
return get_faker().job()
3. Register it
Place the function in the appropriate module under src/copia/generators/ and the rest will be automatically handled by src/copia/generators/__init__.py
4. Verify
-
Run the test with
pytest tests/test_generators_discover.pyto ensure there are no import warnings or errors related to your new generator. -
Run
copiaand press?. to check how your generator docstring looks in the in-app help. If you see any formatting issues, adjust your docstring accordingly.