pydantic-core icon indicating copy to clipboard operation
pydantic-core copied to clipboard

Add to_python(..., serialize_generators=False)

Open yorickvP opened this issue 1 year ago • 3 comments

This disables wrapping/consuming generators while serializing, while calling the fallback function on them.

In many cases, we want to implement some custom serialization logic for generators (such as io.IOBase), instead of consuming them whole.

Change Summary

Add serialize_generators parameter to to_python, to_json, etc. This changes the serialization to handle generators as if they were 'unknown' types. By default, this calls fallback or leaves them alone in to_python, and calls fallback in to_json or errors there.

Related issue number

pydantic-core part of a fix for https://github.com/pydantic/pydantic/issues/8907 . If this change gets merged, we'll submit another PR to pydantic to have model_dump forward a serialize_generators argument.

Checklist

  • [x] Unit tests for the changes exist
  • [x] Documentation reflects the changes where applicable
  • [ ] Pydantic tests pass with this pydantic-core (except for expected changes)
  • [x] My PR is ready to review, please add a comment including the phrase "please review" to assign reviewers

yorickvP avatar Aug 12 '24 15:08 yorickvP