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

Pydantic 2.10 handling for dataclasses that are fields of base models does not propagate allow_arbitrary_fields #727

Open
DiamondJoseph opened this issue Nov 21, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@DiamondJoseph
Copy link
Contributor

DiamondJoseph commented Nov 21, 2024

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:

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

@DiamondJoseph DiamondJoseph added the bug Something isn't working label Nov 21, 2024
@DiamondJoseph
Copy link
Contributor Author

This behaviour is restored in Pydantic 2.10.1. However documenting how to use a Composite should still be done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant