GithubHelp home page GithubHelp logo

Comments (4)

lyz-code avatar lyz-code commented on July 20, 2024 1

Works like a charm, thank you! :)

from polyfactory.

Goldziher avatar Goldziher commented on July 20, 2024

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 if pydantic-factories was able to understand them and randomly return None on some of the objects.

I dealt with this problem in the past, and created faker-optional a faker 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.

lyz-code avatar lyz-code commented on July 20, 2024

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.

Goldziher avatar Goldziher commented on July 20, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.