Skip to content
Advertisement

`pyyaml` can’t parse `pydantic` object if `typing` module is used

Let me start off by saying I wanted to open an issue in pydantic repo. Once I started rubber duck debugging I came up to the conclusion it’s actually pyyaml that isn’t working right but I’m not so sure anymore.

JavaScript

which prints

JavaScript
  • list of pydantic objects is parsed correctly if list is used in static typing
  • list of pydantic objects is NOT parsed correctly if List is used in static typing
  • list of pydantic objects is NOT parsed correctly if List[Bar] is used in static typing
  • list of dataclass objects is always parsed correctly

The constructor seems to be returning the correct object in all examples so I don’t understand where the problem lies.

JavaScript

Advertisement

Answer

I had the same exact issue as you.

The thing that solved the issue for me was setting the deep to true in the construct_mapping method.

Example:

JavaScript
Advertisement