You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
from collections.abc import Callable
from dataclasses import dataclass
from inspect import signature
from typing import Any, get_type_hints
from pydantic import create_model
from pydantic.config import ConfigDict
from pydantic.fields import FieldInfo
ModelConfig = ConfigDict(
arbitrary_types_allowed=True,
)
class Foo:
...
@dataclass(config=ModelConfig) # fails
class PlanParam:
foo: Foo
def plan(plan_param: PlanParam) -> int:
return 0
def _type_spec_for_function(func: Callable[..., Any]) -> dict[str, tuple[type, FieldInfo]]:
types = get_type_hints(func)
return {
name: (types.get(name), FieldInfo())
for name in signature(func).parameters
} # type: ignore
create_model(
plan.__name__,
__config__=ModelConfig,
**_type_spec_for_function(plan) # type: ignore
)
Acceptance Criteria
The above code works
OR
A supported alternative is documented
e.g.
from pydantic.dataclasses import dataclass
CompositeConfig = ConfigDict(arbitrary_types_allowed=True)
@dataclass(config=ModelConfig) # works
class PlanParam:
foo: Foo
There may be additional config that is desired for Composite, e.g. frozen=True, extra=forbid, strict=True
The text was updated successfully, but these errors were encountered:
Pydantic 2.10 fixed a "bug" that dataclasses propagated ConfigDicts from classes that held them. This behaviour was used when we had plans that used dataclass Composite classes with devices as parameters.
Steps To Reproduce
Minimal example:
Acceptance Criteria
e.g.
There may be additional config that is desired for Composite, e.g. frozen=True, extra=forbid, strict=True
The text was updated successfully, but these errors were encountered: