Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON serialization with custom json encoder #143

Open
roedoejet opened this issue Dec 8, 2022 · 1 comment
Open

JSON serialization with custom json encoder #143

roedoejet opened this issue Dec 8, 2022 · 1 comment
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@roedoejet
Copy link

roedoejet commented Dec 8, 2022

Hi there,

I have a Pydantic class that inherits another Pydantic class that allows for custom handling of function serialization:

class ConfigModel(BaseModel):
    class Config:
        extra = Extra.forbid
        use_enum_values = True
        json_encoders = {
            Callable: lambda fn: ".".join(
                [fn.__module__, fn.__name__]
            ),
            FunctionType: lambda fn: ".".join(
                [fn.__module__, fn.__name__]
            ),
        }
class TextConfig(ConfigModel):
    symbols: Symbols = Field(default_factory=Symbols)
    cleaners: List[Callable] = [lower, collapse_whitespace, nfc_normalize]

    def __init__(self, **data) -> None:
        """Custom init to process cleaners"""
        with contextlib.suppress(KeyError):
            cleaners = data["cleaners"]
            for i, c in enumerate(cleaners):
                cleaners[i] = string_to_callable(c)
        super().__init__(**data)

With this configuration, I can do the following:

>>> tc = TextConfig()
>>> tc.json()
'{"symbols": {"silence": ["<SIL>"], "pad": "_", "punctuation": "-\';:,.!?\\u00a1\\u00bf\\u2014\\u2026\\"\\u00ab\\u00bb\\u201c\\u201d "}, "cleaners": ["smts.utils.lower", "smts.utils.collapse_whitespace", "smts.utils.nfc_normalize"]}'

But when I try to autodoc TextConfig, I get the following:

Exception occurred:
  File "pydantic/json.py", line 90, in pydantic.json.pydantic_encoder
TypeError: Object of type 'function' is not JSON serializable
The full traceback has been saved in /home/aip000/aip000/tmp/sphinx-err-u13_waob.log, if you want to report the issue to the developers.

There are ways to ignore this seemingly, but how can I get autodoc_pydantic to just use the models json serialization?

@mansenfranzen
Copy link
Owner

Hi @roedoejet,

thanks for reporting this bug! I agree, autodoc_pydantic should be able to use custom json encoders for generating the schema JSON.

As you've pointed out, it's possible to ignore it which should serve as a workaround in the meantime.

At moment, I'm short on time and won't be able to provide a quick fix. However, I'm more than happy to help with code review and guidance if you want to provide a PR.

@mansenfranzen mansenfranzen added bug Something isn't working help wanted Extra attention is needed labels Apr 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants