Registry / type-stubs / types-factory-boy

types-factory-boy

JSON →
library0.4.1pypypi✓ verified 87d ago

types-factory-boy is a PEP 561 type stub package that provides static type checking for the factory-boy library. It enables tools like mypy, PyCharm, and pytype to analyze code that uses factory-boy, helping to catch type-related errors before runtime. The current version is 0.4.1. As a stub package, its release cadence is tied to updates in factory-boy and improvements in its own type definitions.

pip install types-factory-boy
INSTALL
IMPORT
SIG · TYPES-FACTORY-BOY
T
types-factory-boy
type-stubspythonv0.4.1
Install
3.1s avg
Import
327ms
Disk
41MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.4.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.336s · 43.1MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 3.1s · import 0.319s · 44MB
41MB installed
● package 41MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Factory
from factory import Factory
Types-factory-boy provides stubs for factory-boy's modules; actual runtime imports are from 'factory'.
DjangoModelFactory
from factory.django import DjangoModelFactory
Types-factory-boy provides stubs for factory-boy's ORM-specific factories; actual runtime imports are from 'factory.django'.
Sequence
from factory import Sequence
Types-factory-boy provides stubs for factory-boy's declarations; actual runtime imports are from 'factory'.

This example demonstrates how to define a factory using `factory-boy` for a simple `NamedTuple`. When `types-factory-boy` is installed, a static type checker will use its stubs to provide type inference and validation for `UserFactory` and the generated `User` objects without needing direct imports from `types-factory-boy` itself. The `UserFactory.build()` method is correctly inferred to return an instance of `User`.

from typing import NamedTuple import factory class User(NamedTuple): id: int name: str email: str class UserFactory(factory.Factory): class Meta: model = User id = factory.Sequence(lambda n: n) name = factory.Faker('name') email = factory.LazyAttribute(lambda o: f'{o.name.lower().replace(" ", ".")}@example.com') # This code will use factory-boy at runtime. # If types-factory-boy is installed, a type checker like mypy # will use its stubs to verify types. user = UserFactory.build() print(f"Generated User: {user.name} ({user.email})") user_explicit_type: User = UserFactory.build() print(f"Generated User (explicitly typed): {user_explicit_type.name}")
Debug
Known issues
breakingfactory-boy 3.0.0 introduced significant breaking changes, including the deprecation and removal of `FACTORY_FOR`, `ABSTRACT_FACTORY`, `FACTORY_STRATEGY`, `FACTORY_ARG_PARAMETERS`, and `FACTORY_HIDDEN_ARGS` in favor of `Meta` class attributes like `model`, `abstract`, `strategy`, `inline_args`, and `exclude` respectively. Types-factory-boy reflects these changes.
fix
Update your factory definitions to use the `class Meta` attributes (e.g., `model = MyModel`) instead of the old class-level `FACTORY_FOR` and similar attributes. Ensure your factory-boy version is 3.0.0 or higher.
affects: factory-boy < 3.0.0
breakingThe separation of ORM-specific factories (e.g., `DjangoModelFactory`, `SQLAlchemyModelFactory`) into their own modules (`factory.django`, `factory.alchemy`) occurred in older `factory-boy` versions. While `types-factory-boy` handles these imports correctly, older codebases might have incorrect import paths for these specialized factories.
fix
Ensure that ORM-specific factories are imported from their dedicated modules, e.g., `from factory.django import DjangoModelFactory` instead of `from factory import DjangoModelFactory`.
affects: factory-boy < 2.6.0
gotchaWhen using `factory-boy` and `types-factory-boy`, type checkers might sometimes struggle with the inferred return types of `Factory()` or `Factory.build()` when `stub` strategy is implicitly or explicitly used. By default, `Factory()` might sometimes return a `StubObject` which lacks the methods/attributes of the actual model.
fix
Explicitly use `Factory.create()` or `Factory.build()` to ensure the desired strategy and return type. If you need to enforce a specific model type for type checking, you might need to add explicit type annotations, e.g., `user: User = UserFactory.build()` or define a generic metaclass for your factories.
affects: All versions
gotchaChanges related to type annotations were introduced directly in `factory-boy` 3.3.3, which could cause breaking changes in existing pipelines due to more strict type checking. While `types-factory-boy` aims to provide correct stubs, discrepancies or new strictness from the runtime library's own annotations can lead to type errors.
fix
Review and update your code to align with the stricter type annotations in `factory-boy` 3.3.3 and later. This may involve adding more explicit type hints or adjusting how factories are used in type-checked contexts. Consider using `--ignore-errors` for specific modules if immediate fixes are not feasible, but aim for full type compliance.
affects: factory-boy >= 3.3.3
Errors
Common errors & fixes
mypy: Cannot find implementation or stub for module named 'factory'
mypy cannot locate the necessary type stub files for the 'factory-boy' library, indicating `types-factory-boy` is either not installed or not accessible.
fix
Install the `types-factory-boy` stub package: `pip install types-factory-boy`
mypy: error: Module 'factory.declarations' has no 'Sequence' member
mypy is unable to find type information for specific members within `factory-boy` modules, typically because `types-factory-boy` is not installed or not recognized by your mypy configuration.
fix
Ensure `types-factory-boy` is installed and updated to a version compatible with your `factory-boy` installation: `pip install types-factory-boy --upgrade`
mypy: error: "Factory" has no attribute "build"
mypy reports that core `factory-boy` methods (like `build`, `create`, `create_batch`) are missing because the type stubs provided by `types-factory-boy` are not being correctly applied or found.
fix
Install `types-factory-boy` to provide the necessary type hints for `factory-boy` classes and methods: `pip install types-factory-boy`
Upgrade
Version history
0.4.1latest on PyPI · released Mar 12, 2023
Audit
Dependencies
factory-boyrequiredProvides type stubs for the runtime library.
Agent activity
41 hits · last 30 days
node
36
OpenAI (training)
1
Resources
types-factory-boy — pip install types-factory-boy · libregistry