polyfactory icon indicating copy to clipboard operation
polyfactory copied to clipboard

Enhancement: static type checking on factory build method calls

Open ollie-bell opened this issue 2 months ago • 1 comments

Summary

I don't see the same level of static type checking on calls to factory build that I would see if I was instantiating the underlying pydantic model directly. Example below is illustrated, mypy and pyright both don't see any issues when calling build, but they do if creating a Foo object directly.

Basic Example

from __future__ import annotations

from typing import Literal

from polyfactory.factories.pydantic_factory import ModelFactory
from pydantic import BaseModel

type Baz = Literal["hello"]


class Foo(BaseModel):
    bar: Baz


class FooFactory(ModelFactory[Foo]): ...


foo = FooFactory.build(bar="world")   # mypy & pyright should complain since "world" is not valid

Drawbacks and Impact

Unlikely to fully adopt polyfactory without this feature.

Unresolved questions

No response

ollie-bell avatar Oct 01 '25 13:10 ollie-bell

This would be great to support. This is probably a limitation of Python typing as does not have the ability to extract partial args in this case

adhtruong avatar Oct 05 '25 17:10 adhtruong