Semantic, typed wrappers for Faker with automatic Polyfactory integration
Project description
Capper
Semantic, typed wrappers for Faker with automatic Polyfactory integration.
Source: github.com/eddiethedean/capper
CI pipeline
The ci.yml workflow runs on pushes and PRs to main and includes:
- Linting:
ruff check .andruff format --check . - Type checking:
mypy capper - Tests:
pytest -n auto capper/tests -v -m "not benchmark" --cov=capper --cov-report=term-missing --cov-fail-under=98 - Docs:
mkdocs build --strict
Why Capper?
- Zero config — Import a type; Polyfactory uses the right Faker provider. No manual registration.
- Typed — Use
Name,Email,PhoneNumber, etc. in your models for clear intent and IDE support. - Multi-backend — Works with Pydantic, dataclasses, attrs, and other Polyfactory-supported model types.
- Thread-safe — Per-thread Faker via a proxy; seeding and locales are isolated per thread, so concurrent tests are safe.
- Optional Pydantic — Install
capperalone for dataclasses/attrs; addcapper[pydantic]when you use Pydantic models.
Install
pip install capper
Requires Python 3.10+, Faker >= 20.0, and Polyfactory >= 2.0. Optional extras:
- Pydantic (for Pydantic models):
pip install capper[pydantic] - Hypothesis (for property-based tests with
st.from_type(...)):pip install capper[hypothesis]
Usage
With Pydantic (requires capper[pydantic]):
from pydantic import BaseModel
from capper import Name, Email
from polyfactory.factories.pydantic_factory import ModelFactory
class User(BaseModel):
name: Name
email: Email
class UserFactory(ModelFactory[User]):
pass
user = UserFactory.build()
print(user.name)
print(user.email)
Example output (varies each run):
Paul Blair
linda00@example.net
With dataclasses (no Pydantic needed):
from dataclasses import dataclass
from capper import Name, Email
from polyfactory.factories import DataclassFactory
@dataclass
class User:
name: Name
email: Email
class UserFactory(DataclassFactory[User]):
pass
user = UserFactory.build()
print(user.name)
print(user.email)
Example output (varies each run):
Carly Jenkins
oevans@example.com
Works automatically. No extra steps. IDE autocompletion.
New to Capper? See the Getting started guide and run the examples in docs/examples/.
Available types
- Person:
Name,FirstName,LastName,Job - Geo:
Address,City,Country - Internet:
Email,URL,IP,UserName - Commerce:
Company,Product,Currency,Price - Date/time:
Date,DateTime,Time - Text:
Paragraph,Sentence - Phone:
PhoneNumber,CountryCallingCode - Finance:
CreditCardNumber,CreditCardExpiry,CreditCardProvider - File:
FilePath,FileName,FileExtension - Misc:
UUID - Color:
HexColor - Barcode:
EAN13,EAN8
Import from the top level: from capper import Name, Email, Address, ...
See docs/FAKER_PROVIDERS.md for the Faker provider used by each type.
Optional kwargs: Subclass FakerType and set faker_kwargs to pass arguments to the Faker provider (e.g. faker_kwargs = {"nb_words": 10} for Sentence).
Custom types: Subclass FakerType, set faker_provider to the Faker method name (e.g. "company"), and optionally faker_kwargs. The type auto-registers with Polyfactory when the class is defined.
CLI
Generate fake values from the command line:
capper generate Name Email --count 5
capper generate Name Email --count 3 --seed 42
Use -n/--count for the number of rows and -s/--seed for reproducible output. Type names are the same as the Python types (e.g. Name, Email, Address).
Compatibility
Capper targets Python 3.10+, Faker >= 20.0, and Polyfactory >= 2.0. For version ranges, upgrade guidance, the versioning policy, and the deprecation policy, see Compatibility.
What's new in 0.4.0
- Thread safety: Capper is now thread-safe via a per-thread Faker proxy;
seed()anduse_faker()only affect the current thread. - Reliability and coverage: Phase 9 adds a coverage gate (≥ 98% for
capper/), targeted edge-case tests, and a lightweight performance check in CI. - Tooling and docs: CI runs Ruff, mypy, tests (with coverage gate), and a strict MkDocs build on all supported Python versions; docs and roadmap have been updated to reflect Phase 9.
Development
pip install -e ".[dev]"
pytest capper/tests
Lint and type-check: ruff check ., ruff format ., mypy capper.
Run tests with coverage: pytest capper/tests --cov=capper --cov-report=term-missing. CI requires coverage ≥ 98% for the capper/ package (--cov-fail-under=98).
Reproducibility: Capper and Polyfactory share the same Faker instance, so one seed controls both capper types and built-in types (str, int, etc.):
from capper import seed, Name
from polyfactory.factories.pydantic_factory import ModelFactory
from pydantic import BaseModel
class User(BaseModel):
name: Name
class UserFactory(ModelFactory[User]):
pass
# Either way seeds the shared Faker (same effect):
seed(42)
user1 = UserFactory.build()
UserFactory.seed_random(42)
user2 = UserFactory.build() # same data as user1 if you seed the same before each
Use UserFactory.__random_seed__ = 42 to seed once when the factory class is created, or call seed(42) / UserFactory.seed_random(42) before each build for identical builds. For a custom locale (e.g. German names), use use_faker(Faker('de_DE')) so both Capper and Polyfactory use the same Faker instance; see Reproducible data.
Publishing
Releases are built and published to PyPI via GitHub Actions. To publish:
- Update CHANGELOG.md: move Unreleased entries into a new version section and date it.
- Add a
PYPI_API_TOKENsecret (PyPI API token) to the repo. - Create a GitHub release (tag e.g.
v0.4.1). The workflow runs tests, builds the package, and uploads to PyPI.
To build and upload manually: pip install build twine, python -m build, twine upload dist/*.
Documentation
- Docs index — overview and links to all documentation
- API reference — generated API docs (build with
mkdocs serve; see Contributing) - Contributing — dev setup and how to add new types
- User guides (step-by-step, with runnable examples):
- Getting started — install, first model, first factory
- Models and factories — Pydantic, dataclasses, batches
- Reproducible data — seeding for tests and demos
- Custom types —
FakerTypesubclasses andfaker_kwargs - FastAPI + Pydantic — API payloads and tests using Capper-backed Pydantic models
- Django patterns — Django-style schemas, factories, and service tests
- Dataclasses and attrs — non-Pydantic projects with
DataclassFactory - Test setup templates — pytest/Hypothesis fixtures and seeding patterns
- Project structure — organizing Capper types, factories, and type packs
- Package plan — design and rationale
- Roadmap — development phases and status
- Faker provider mapping — which Faker method each type uses
- Example notebooks — Jupyter notebooks in
docs/notebooks/
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file capper-0.5.0.tar.gz.
File metadata
- Download URL: capper-0.5.0.tar.gz
- Upload date:
- Size: 21.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
df4ee3cea2a9db4655531acf3733f9178418a2cf229d3a09895342d4e7f0a618
|
|
| MD5 |
26fdd9f24790614432130dbd6e7932a9
|
|
| BLAKE2b-256 |
82612f90ca77bb0b06ee67a88e52bcb25660bfb411b7a49cc405f3733d76d42d
|
File details
Details for the file capper-0.5.0-py3-none-any.whl.
File metadata
- Download URL: capper-0.5.0-py3-none-any.whl
- Upload date:
- Size: 25.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8fca40fa8cd6688800391142b6197e1eb28e8e19abb978f557e1251d86ef67a0
|
|
| MD5 |
04f5503b1a2a1749f50ce331201febd8
|
|
| BLAKE2b-256 |
305873721da04a228f328499004433e04ae7435bf6d023032342c08323283722
|