Skip to main content

Semantic, typed wrappers for Faker with automatic Polyfactory integration

Project description

Capper

PyPI Python CI Ruff mypy

Semantic, typed wrappers for Faker with automatic Polyfactory integration.

Source: github.com/eddiethedean/capper

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.
  • Optional Pydantic — Install capper alone for dataclasses/attrs; add capper[pydantic] when you use Pydantic models.

Install

pip install capper

Requires Python 3.9+, Faker >= 20.0, and Polyfactory >= 2.0. For Pydantic models, install the optional extra:

pip install capper[pydantic]

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

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.

Compatibility

Capper targets Faker >= 20.0 and Polyfactory >= 2.0. Major Faker upgrades may change or rename provider methods; if a type fails, check Faker's changelog and docs/FAKER_PROVIDERS.md and update the provider name if needed.

Development

pip install -e ".[dev]"
pytest capper/tests

Run tests with coverage: pytest capper/tests --cov=capper --cov-report=term-missing.

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.

Publishing

Releases are built and published to PyPI via GitHub Actions. To publish:

  1. Add a PYPI_API_TOKEN secret (PyPI API token) to the repo.
  2. Create a GitHub release (tag e.g. v0.1.0). 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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

capper-0.1.1.tar.gz (10.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

capper-0.1.1-py3-none-any.whl (12.2 kB view details)

Uploaded Python 3

File details

Details for the file capper-0.1.1.tar.gz.

File metadata

  • Download URL: capper-0.1.1.tar.gz
  • Upload date:
  • Size: 10.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for capper-0.1.1.tar.gz
Algorithm Hash digest
SHA256 117feb3fa9db50d39c00a1c2403ebe5dd07a09118cda18dff9cbc4b782892329
MD5 566d1da229f0c37dc3731a5cb197e95b
BLAKE2b-256 a1806c34df7c3c8afcb361d8e89e65cb146b43b40274b142604cfdebd12acf03

See more details on using hashes here.

File details

Details for the file capper-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: capper-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 12.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for capper-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 19e7cd4a956ad392970329f29ca14e6656bb3c96e79f87110d011b8fd8778445
MD5 bf7595934c52b77dbb608ceff56dd8af
BLAKE2b-256 602adf1f3290b0c4705f7573c6e78346001ade34fefed7a7a4d33e0b31a8d59f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page