Comments (4)
Works like a charm, thank you! :)
from polyfactory.
Hi @Goldziher first of all, thank you, thank you and thank you. I've been close to create something like this several times. You're a life saver :)
In most of my pydantic models I have some property of type
Optional[...]
. It would be awesome ifpydantic-factories
was able to understand them and randomly returnNone
on some of the objects.I dealt with this problem in the past, and created
faker-optional
afaker
provider just for that, maybe it's interesting for this use case.Thanks!
Why you're welcome 😄
So, regarding your question, given:
class MyModel(BaseModel):
prop: Optional[str]
You would like the factory to occasionally return None and occasionally return a string for prop?
I can certainly add this, or if you like - you can add a PR adding this functionality. Let me know how you'd like to proceed. I wouldn't though like to add a dependency for this - it can be done quite easily with the existing implementation.
from polyfactory.
Yup, that functionality is exactly what I had in mind, and I fully understand that it's not reason to add another dependency.
Regarding who implements it, if you can (and want to) do it, probably you'll be able to do it faster, else I can do it myself. In that case, I'd be grateful if you can point me on how you think it should be implemented.
from polyfactory.
@lyz-code - implemented, and released as v0.4.1. The factory will randomly output None values for optional fields - except for dataclasses, for these it will always output values for now. It will require more custom logic to handle this because the pydantic helper function create_model
neither allows for setting required via field definitions, nor runs the pydantic parsing logic.
I, or someone else, will need to solve this somehow. But I think this should be sufficient for most use-cases.
from polyfactory.
Related Issues (20)
- Use defaults values from BaseModel in ModelFactory HOT 4
- Support for relationship building models in `SQLAlchemyFactory` HOT 10
- Bug: Incomplete Dependency Specification HOT 1
- Bug: settings `__set_as_default_factory_for_type__ ` to `True` on base factory fails
- Bug: Pydantic fields with constraints and multiple type hints cause `ParameterException` HOT 1
- Bug: Optional constrained types do not work HOT 8
- Bug: ModelFactory can not generate list of classes with a Pydantic field alias HOT 1
- bug: support column.type `impl` attribute HOT 1
- Bug: Setting `ge` and `multiple_of` but not `le` constraints causes invalid values to be generated HOT 2
- Docs: Add example to use `PydanticFactory` with SQLAlchemy persistence HOT 5
- Bug: Data generated incorrectly from nested Annotated HOT 4
- Bug: Try to use SQLAlchemyFactory for model with column_property HOT 4
- Bug: UUIDBase ID Value Changed from UUID to Bytes HOT 4
- Enhancement: Support for dictionaries with Pydantic models as value, e.g. dict[str, PydanticClass] HOT 1
- Bug: Invalid Coverage for Optional Fields with Annotated Constraint
- Bug: pyright incompatability HOT 1
- Any not supported in provider_map
- Doubt in usage of polyfactory HOT 1
- Bug: Optional lists in coverage produce nested lists HOT 2
- Bug: sqlalchemy factory `column_property` in `get_model_fields` not working HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from polyfactory.