litestar-org/polyfactory

Enhancement: Add proper typings to `create_factory` function

g0di opened this issue · 0 comments

g0di commented

Summary

When using the imperative code style to create factories, the returned factory is not properly type. Objects built with those factories are typed as Any .

Instead, I would the returned factory to be typed accordingly to the actual model passed allowing type checkers and code editors to take advantage of it.

Basic Example

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


class Foo(BaseModel):
    foo: int
    bar: str


fac = ModelFactory.create_factory(Foo)

If you check the type of fac here, you'll get: type[BaseFactory[Any]].
I would have instead type[ModelFactory[Foo]]

Drawbacks and Impact

No response

Unresolved questions

I actually tried to do it myself but could not find a suitable solution


Funding

  • If you would like to see an issue prioritized, make a pledge towards it!
  • We receive the pledge once the issue is completed & verified
Fund with Polar