litestar-org / litestar-org/polyfactory

Enhancement: static type checking on factory build method calls

Open
#753 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Python
Stars
1.5k
Forks
120
PR merge metrics
No merged PRs in 30d

Description

### 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

```python
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_

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at ModelFactory.build in polyfactory.factories.pydantic_factory and trace how its call signature is exposed to mypy and pyright. Compare it with direct Foo construction; done when both type checkers reject the invalid Literal value shown in the example.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
developer-experience
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.