{"id":6929,"library":"types-factory-boy","title":"Typing stubs for factory-boy","description":"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.","status":"active","version":"0.4.1","language":"en","source_language":"en","source_url":"https://github.com/youtux/types-factory-boy","tags":["typing","stubs","factory-boy","testing","type-checking"],"install":[{"cmd":"pip install types-factory-boy","lang":"bash","label":"Install"}],"dependencies":[{"reason":"Provides type stubs for the runtime library.","package":"factory-boy","optional":false}],"imports":[{"note":"Types-factory-boy provides stubs for factory-boy's modules; actual runtime imports are from 'factory'.","symbol":"Factory","correct":"from factory import Factory"},{"note":"Types-factory-boy provides stubs for factory-boy's ORM-specific factories; actual runtime imports are from 'factory.django'.","symbol":"DjangoModelFactory","correct":"from factory.django import DjangoModelFactory"},{"note":"Types-factory-boy provides stubs for factory-boy's declarations; actual runtime imports are from 'factory'.","symbol":"Sequence","correct":"from factory import Sequence"}],"quickstart":{"code":"from typing import NamedTuple\nimport factory\n\nclass User(NamedTuple):\n    id: int\n    name: str\n    email: str\n\nclass UserFactory(factory.Factory):\n    class Meta:\n        model = User\n\n    id = factory.Sequence(lambda n: n)\n    name = factory.Faker('name')\n    email = factory.LazyAttribute(lambda o: f'{o.name.lower().replace(\" \", \".\")}@example.com')\n\n# This code will use factory-boy at runtime.\n# If types-factory-boy is installed, a type checker like mypy\n# will use its stubs to verify types.\nuser = UserFactory.build()\nprint(f\"Generated User: {user.name} ({user.email})\")\n\nuser_explicit_type: User = UserFactory.build()\nprint(f\"Generated User (explicitly typed): {user_explicit_type.name}\")","lang":"python","description":"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`."},"warnings":[{"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.","message":"factory-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.","severity":"breaking","affected_versions":"factory-boy < 3.0.0"},{"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`.","message":"The 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.","severity":"breaking","affected_versions":"factory-boy < 2.6.0"},{"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.","message":"When 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.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"Changes 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.","severity":"gotcha","affected_versions":"factory-boy >= 3.3.3"}],"env_vars":null,"last_verified":"2026-04-15T00:00:00.000Z","next_check":"2026-07-14T00:00:00.000Z","problems":[]}