You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Seems like Polyfactory don't respect forward type annotations when generating factories for Pydantic models.
When Pydantic model references another model and the latter is not yet defined, it is advised to either explicitly put type annotation in quotes to annotate type in forward or use implicit automatic forward annotations by doing from __future__ import annotations.
For such models, Polyfactory fails to create proper working ModelFactory which raises exceptions upon model build.
1. Run example
Expected: No errors, model built
Actual:
E polyfactory.exceptions.ParameterException: Unsupported type: ForwardRef('Coll | None') on field 'coll' from class RespFactory.
E
E Either use 'add_provider', extend the providers map, or add a factory functionfor the field on the model.
(partial) workaround:
Sometimes <Model>.model_rebuild() helps fix forward annotations.
Sometimes if one to remove from __future__ import annotations and reorder model classes definitions so that no forward annotations occur, then ModelFactory works as expected.
However, this is not always the case, due to e.g. legacy imported code or circular referencing data structures.
Screenshots
"In the format of: "
Logs
Release Version
2.20.0
Platform
Linux
Mac
Windows
Other (Please specify in the description above)
The text was updated successfully, but these errors were encountered:
PRs welcome to fix. I think for main use case here can call BaseModel.model_rebuild - either in your base class or in ModelFactory.__init_subclass__.
Probably out of scope to support all models for unbuilt schema and try resolve references in general and try delegate this to exposed pydantic api if possible
Description
Seems like Polyfactory don't respect forward type annotations when generating factories for Pydantic models.
When Pydantic model references another model and the latter is not yet defined, it is advised to either explicitly put type annotation in quotes to annotate type in forward or use implicit automatic forward annotations by doing
from __future__ import annotations
.For such models, Polyfactory fails to create proper working ModelFactory which raises exceptions upon model build.
URL to code causing the issue
No response
MCVE
Steps to reproduce
(partial) workaround:
Sometimes
<Model>.model_rebuild()
helps fix forward annotations.Sometimes if one to remove
from __future__ import annotations
and reorder model classes definitions so that no forward annotations occur, thenModelFactory
works as expected.However, this is not always the case, due to e.g. legacy imported code or circular referencing data structures.
Screenshots
"In the format of:

"Logs
Release Version
2.20.0
Platform
The text was updated successfully, but these errors were encountered: