Skip to content

Enhancement: static type checking on factory build method calls #753

@ollie-bell

Description

@ollie-bell

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions