litestar-org / litestar-org/polyfactory
Enhancement: static type checking on factory build method calls
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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