RavenPy icon indicating copy to clipboard operation
RavenPy copied to clipboard

Pydantic classes for model emulators are non JSON serializable

Open Zeitsperre opened this issue 11 months ago • 1 comments

Setup Information

  • ravenpy version: 0.16.1
  • Python version: Any
  • Operating System: openSUSE Tumbleweed x86_64

Description

This problem occurs when exposing the Emulator models in the generated documentation. The issue appears to be brought on because the ListCommand class can't serialize what root is when generating the documentation:

lass ListCommand(RootModel, _Command):
    """Use so that commands with __root__: Sequence[Command] behave like a list."""

    root: Sequence[Any]

    def __iter__(self):
        return iter(self.root)

    def __getitem__(self, item):
        return self.root[item]

    def __len__(self):
        return len(self.root)

    model_config = ConfigDict(arbitrary_types_allowed=True, populate_by_name=True)

This means that any emulated models built using it are raising errors when trying to parse the documentation. I'm not familiar enough with pydantic to understand how to fix this, so I'm removing them from the docs to see if that fixes things.

The warnings summary:

/home/tjs/git/RavenPy/docs/autodoc_pydantic.rst: WARNING: JSON schema can't be generated for 'ravenpy.config.commands.SeasonalRelativeHeight' because the following pydantic fields can't be serialized properly: ['root'].
/home/tjs/git/RavenPy/docs/autodoc_pydantic.rst: WARNING: JSON schema can't be generated for 'ravenpy.config.commands.SeasonalRelativeLAI' because the following pydantic fields can't be serialized properly: ['root'].
/home/tjs/git/RavenPy/docs/autodoc_pydantic.rst: WARNING: JSON schema can't be generated for 'ravenpy.config.rvs.Config' because the following pydantic fields can't be serialized properly: ['seasonal_relative_lai', 'seasonal_relative_height'].
/home/tjs/git/RavenPy/docs/autodoc_pydantic.rst: WARNING: JSON schema can't be generated for 'ravenpy.config.rvs.RVP' because the following pydantic fields can't be serialized properly: ['seasonal_relative_lai', 'seasonal_relative_height'].
/home/tjs/git/RavenPy/docs/autodoc_pydantic.rst: WARNING: JSON schema can't be generated for 'ravenpy.config.emulators.BasicRoute' because the following pydantic fields can't be serialized properly: ['seasonal_relative_lai', 'seasonal_relative_height'].
/home/tjs/git/RavenPy/docs/autodoc_pydantic.rst: WARNING: JSON schema can't be generated for 'ravenpy.config.emulators.Blended' because the following pydantic fields can't be serialized properly: ['seasonal_relative_lai', 'seasonal_relative_height'].
/home/tjs/git/RavenPy/docs/autodoc_pydantic.rst: WARNING: JSON schema can't be generated for 'ravenpy.config.emulators.CanadianShield' because the following pydantic fields can't be serialized properly: ['seasonal_relative_lai', 'seasonal_relative_height'].
/home/tjs/git/RavenPy/docs/autodoc_pydantic.rst: WARNING: JSON schema can't be generated for 'ravenpy.config.emulators.GR4JCN' because the following pydantic fields can't be serialized properly: ['seasonal_relative_lai', 'seasonal_relative_height'].
/home/tjs/git/RavenPy/docs/autodoc_pydantic.rst: WARNING: JSON schema can't be generated for 'ravenpy.config.emulators.HBVEC' because the following pydantic fields can't be serialized properly: ['seasonal_relative_lai', 'seasonal_relative_height'].
/home/tjs/git/RavenPy/docs/autodoc_pydantic.rst: WARNING: JSON schema can't be generated for 'ravenpy.config.emulators.HMETS' because the following pydantic fields can't be serialized properly: ['seasonal_relative_lai', 'seasonal_relative_height'].
/home/tjs/git/RavenPy/docs/autodoc_pydantic.rst: WARNING: JSON schema can't be generated for 'ravenpy.config.emulators.HYPR' because the following pydantic fields can't be serialized properly: ['seasonal_relative_lai', 'seasonal_relative_height'].
/home/tjs/git/RavenPy/docs/autodoc_pydantic.rst: WARNING: JSON schema can't be generated for 'ravenpy.config.emulators.Mohyse' because the following pydantic fields can't be serialized properly: ['seasonal_relative_lai', 'seasonal_relative_height'].
/home/tjs/git/RavenPy/docs/autodoc_pydantic.rst: WARNING: JSON schema can't be generated for 'ravenpy.config.emulators.SACSMA' because the following pydantic fields can't be serialized properly: ['seasonal_relative_lai', 'seasonal_relative_height'].
/home/tjs/git/RavenPy/docs/autodoc_pydantic.rst: WARNING: JSON schema can't be generated for 'ravenpy.config.emulators.blended.Blended' because the following pydantic fields can't be serialized properly: ['seasonal_relative_lai', 'seasonal_relative_height'].
/home/tjs/git/RavenPy/docs/autodoc_pydantic.rst: WARNING: JSON schema can't be generated for 'ravenpy.config.emulators.canadianshield.CanadianShield' because the following pydantic fields can't be serialized properly: ['seasonal_relative_lai', 'seasonal_relative_height'].
/home/tjs/git/RavenPy/docs/autodoc_pydantic.rst: WARNING: JSON schema can't be generated for 'ravenpy.config.emulators.gr4jcn.GR4JCN' because the following pydantic fields can't be serialized properly: ['seasonal_relative_lai', 'seasonal_relative_height'].
/home/tjs/git/RavenPy/docs/autodoc_pydantic.rst: WARNING: JSON schema can't be generated for 'ravenpy.config.emulators.hbvec.HBVEC' because the following pydantic fields can't be serialized properly: ['seasonal_relative_lai', 'seasonal_relative_height'].
/home/tjs/git/RavenPy/docs/autodoc_pydantic.rst: WARNING: JSON schema can't be generated for 'ravenpy.config.emulators.hmets.HMETS' because the following pydantic fields can't be serialized properly: ['seasonal_relative_lai', 'seasonal_relative_height'].
/home/tjs/git/RavenPy/docs/autodoc_pydantic.rst: WARNING: JSON schema can't be generated for 'ravenpy.config.emulators.hypr.HYPR' because the following pydantic fields can't be serialized properly: ['seasonal_relative_lai', 'seasonal_relative_height'].
/home/tjs/git/RavenPy/docs/autodoc_pydantic.rst: WARNING: JSON schema can't be generated for 'ravenpy.config.emulators.mohyse.Mohyse' because the following pydantic fields can't be serialized properly: ['seasonal_relative_lai', 'seasonal_relative_height'].
/home/tjs/git/RavenPy/docs/autodoc_pydantic.rst: WARNING: JSON schema can't be generated for 'ravenpy.config.emulators.routing.BasicRoute' because the following pydantic fields can't be serialized properly: ['seasonal_relative_lai', 'seasonal_relative_height'].
/home/tjs/git/RavenPy/docs/autodoc_pydantic.rst: WARNING: JSON schema can't be generated for 'ravenpy.config.emulators.sacsma.SACSMA' because the following pydantic fields can't be serialized properly: ['seasonal_relative_lai', 'seasonal_relative_height'].

Steps To Reproduce

  1. Install autodoc-pydantic;
  2. Enable the sphinxcontrib-autodoc-pydantic extension in docs/conf.py;
  3. Add the imported models found in src/ravenpy/config/emulators/__init__.py to the __all__ listing;
  4. Run $ make docs.

Additional context

It could be the case that the magic methods for the underlying classes aren't written to support the data they might be initialized with (Sequence[Any] could be a lot of things)? I'm not sure.

Relevant resources:

  • https://github.com/pydantic/pydantic/discussions/7510
  • https://stackoverflow.com/questions/78550600/how-to-fix-pydantic-default-value-is-not-json-serializable-warning-when-using

Contribution

  • [ ] I would be willing/able to open a Pull Request to address this bug.

Zeitsperre avatar Jan 15 '25 20:01 Zeitsperre

Raised from discussion in https://github.com/CSHS-CWRA/RavenPy/pull/438

Zeitsperre avatar Jan 15 '25 20:01 Zeitsperre