API Reference
Programmatic API for Capper: the main package, the shared Faker instance, and the base type.
Public API surface
Capper’s public API (stable pre-1.0) consists of:
- Modules
capper— semantic types, Faker proxy, and helpers.capper.strategies— Hypothesis integration (for_typeandst.from_type(...)once imported).- Objects
- Semantic types imported from
capper(e.g.Name,Email,Address,Price,UUID,EAN13). FakerType,faker,faker_field,seed,use_fakerfromcapper.for_typefromcapper.strategies.
Internal helpers and modules not listed here may change between releases.
Package: capper
Top-level exports: semantic types (Name, Email, etc.), FakerType, faker, faker_field, seed, and use_faker. Import with from capper import Name, Email, faker_field, seed.
Capper: semantic Faker types with automatic Polyfactory integration.
Import types (e.g. Name, Email) and use them in Pydantic models, dataclasses,
or attrs; Polyfactory will generate values via Faker. Use seed(n) for reproducibility.
With Hypothesis installed (pip install capper[hypothesis]), import capper.strategies
and use st.from_type(Name) for property-based tests.
Address
City
Company
Country
CountryCallingCode
CreditCardExpiry
CreditCardNumber
CreditCardProvider
Currency
Date
DateTime
EAN13
EAN8
Email
FakerType
Bases: str
Base class for semantic Faker types; subclasses auto-register with Polyfactory.
Subclasses must set a non-empty faker_provider (the Faker method name).
Optional faker_kwargs is a dict of keyword arguments passed to that provider
(e.g. faker_kwargs = {"nb_words": 10} for sentence).
When Hypothesis is installed, use st.from_type(YourFakerType) for property-based tests.
FakerType subclasses are nominal string types: they distinguish fields in models
and factories but do not validate string format when values are supplied manually
(e.g. via Pydantic model_validate).
FileExtension
FileName
FilePath
FirstName
HexColor
IP
Job
LastName
Name
Paragraph
PhoneNumber
Price
Product
Sentence
Time
URL
UUID
UserName
faker_field(type_class, **kwargs)
Return a callable for use as a Polyfactory field override with Faker kwargs.
Assign the result to a factory attribute so that field is generated using
the given FakerType's provider with the specified keyword arguments.
Polyfactory invokes the callable at build time. Uses the shared per-thread
Faker (same as seed() and use_faker()).
Example::
from pydantic import BaseModel
from polyfactory.factories.pydantic_factory import ModelFactory
from capper import Sentence, faker_field
class Post(BaseModel):
summary: Sentence
body: Sentence
class PostFactory(ModelFactory[Post]):
summary = faker_field(Sentence, nb_words=5)
body = faker_field(Sentence, nb_words=20)
| Parameters: |
|
|---|
| Returns: |
|
|---|
| Raises: |
|
|---|
seed(seed_value)
Seed the current thread's Faker instance for reproducible data.
| Parameters: |
|
|---|
| Raises: |
|
|---|
use_faker(instance)
Use a custom Faker instance for the current thread only.
Sets the Faker used by Capper and Polyfactory for this thread (e.g. a locale-specific Faker). Other threads are unaffected. Call before building any models in this thread. Pass None to reset this thread to a new default Faker (e.g. after temporarily using a custom instance).
| Parameters: |
|
|---|
| Raises: |
|
|---|
Base: FakerType, faker, seed, use_faker
The base class and shared per-thread Faker instance. Use seed(n) for reproducibility; use use_faker(instance) to switch to a custom Faker (e.g. locale-specific) for the current thread.
Bases: str
Base class for semantic Faker types; subclasses auto-register with Polyfactory.
Subclasses must set a non-empty faker_provider (the Faker method name).
Optional faker_kwargs is a dict of keyword arguments passed to that provider
(e.g. faker_kwargs = {"nb_words": 10} for sentence).
When Hypothesis is installed, use st.from_type(YourFakerType) for property-based tests.
FakerType subclasses are nominal string types: they distinguish fields in models
and factories but do not validate string format when values are supplied manually
(e.g. via Pydantic model_validate).
Seed the current thread's Faker instance for reproducible data.
| Parameters: |
|
|---|
| Raises: |
|
|---|
Use a custom Faker instance for the current thread only.
Sets the Faker used by Capper and Polyfactory for this thread (e.g. a locale-specific Faker). Other threads are unaffected. Call before building any models in this thread. Pass None to reset this thread to a new default Faker (e.g. after temporarily using a custom instance).
| Parameters: |
|
|---|
| Raises: |
|
|---|
Field helpers (Polyfactory-style)
Use faker_field(Type, **kwargs) on a factory class to generate a field with custom Faker provider arguments. Polyfactory invokes the returned callable at build time.
Return a callable for use as a Polyfactory field override with Faker kwargs.
Assign the result to a factory attribute so that field is generated using
the given FakerType's provider with the specified keyword arguments.
Polyfactory invokes the callable at build time. Uses the shared per-thread
Faker (same as seed() and use_faker()).
Example::
from pydantic import BaseModel
from polyfactory.factories.pydantic_factory import ModelFactory
from capper import Sentence, faker_field
class Post(BaseModel):
summary: Sentence
body: Sentence
class PostFactory(ModelFactory[Post]):
summary = faker_field(Sentence, nb_words=5)
body = faker_field(Sentence, nb_words=20)
| Parameters: |
|
|---|
| Returns: |
|
|---|
| Raises: |
|
|---|
Semantic types
All semantic types are subclasses of FakerType and are used as type annotations in your models. For the full list and the Faker provider used by each, see Faker providers.
Categories: 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).