polyfactory
polyfactory copied to clipboard
Enhancement: static type checking on factory build method calls
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
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