Skip to content

Support for Prior class in Pydantic models #506

Open
@williambdean

Description

@williambdean

The following code:

from pydantic import BaseModel
from pymc_marketing.prior import Prior


class MyParameters(BaseModel):
    beta: Prior
    gamma: Prior

raises the following error:

PydanticSchemaGenerationError: Unable to generate pydantic-core schema for
<class 'pymc_marketing.prior.Prior'>. Set `arbitrary_types_allowed=True` in the
model_config to ignore this error or implement `__get_pydantic_core_schema__`
on your type to fully support it.

If you got this error by calling handler(<some type>) within
`__get_pydantic_core_schema__` then you likely need to call
`handler.generate_schema(<some type>)` since we do not call
`__get_pydantic_core_schema__` on `<some type>` otherwise to avoid infinite
recursion.

For further information visit
https://errors.pydantic.dev/2.10/u/schema-for-unknown-type   gamma: Prior

There is the workaround to use InstanceOf from pydantic

from pydantic import BaseModel, InstanceOf
from pymc_marketing.prior import Prior


class MyParameters(BaseModel):
    beta: InstanceOf[Prior]
    gamma: InstanceOf[Prior]

This issue was transferred from pymc-labs/pymc-marketing#1247

Comments

juanitorduz

What would be the usage of this in the code base? Or is it mostly for users to be able to use this?


williambdean

What would be the usage of this in the code base? Or is it mostly for users to be able to use this?

There are a few times that the InstanceOf[Prior] is used. I am assuming that implementation of that __ method would be a stronger check

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions