Skip to content

model config in inheritance doesn't respect MRO #9992

Open

Description

@KotlinIsland

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

in the example below, the output should be True in my opinion because A is first in the mro.

Example Code

from pydantic import BaseModel, ConfigDict


class A(BaseModel):
    model_config = ConfigDict(strict=True)


class B(BaseModel):
    model_config = ConfigDict(strict=False)


class C(A, B):
    pass


print(C.model_config["strict"])  # False

Python, Pydantic & OS Version

pydantic version: 2.8.2
        pydantic-core version: 2.20.1
          pydantic-core build: profile=release pgo=true
                 install path: C:\Users\AMONGUS\projects\test\.venv\Lib\site-packages\pydantic
               python version: 3.12.2 (tags/v3.12.2:6abddd9, Feb  6 2024, 21:26:36) [MSC v.1937 64 bit (AMD64)]
                     platform: Windows-10-10.0.19045-SP0
             related packages: typing_extensions-4.12.2
                       commit: unknown

Activity

  1. sydney-runkle commented on Jul 30, 2024

    @sydney-runkle
    Contributor

    Hmm, I'm not convinced this should change - this would be a breaking change. When we merge fields, we respect the settings on the last field with the highest priority...

  2. sydney-runkle commented on Jul 30, 2024

    @sydney-runkle
    Contributor

    Ah yes, we've established this pattern in our docs:

    When subclassing a model, the model_config attribute is inherited. This is helpful in the case where you'd like to use a base class with a given configuration for many models. Note, if you inherit from multiple BaseModel subclasses, like class MyModel(Model1, Model2), the non-default settings in the model_config attribute from the two models will be merged, and for any settings defined in both, those from Model2 will override those from Model1.

  3. KotlinIsland commented on Jul 30, 2024

    @KotlinIsland
    ContributorAuthor

    i find that very bizarre and unintuitive:

    class A:
        a = 1
    class B:
        a = 2
    class C(A, B):
        pass
    print(C.a)  # 1
  4. KotlinIsland commented on Jul 31, 2024

    @KotlinIsland
    ContributorAuthor

    @sydney-runkle was there any reasoning behind this pattern that you know of? what would it take to get it fixed?

  5. sydney-runkle commented on Jul 31, 2024

    @sydney-runkle
    Contributor

    @KotlinIsland,

    I agree with you, this is a concerning pattern. Perhaps we could address in V3. I'll open and tag it as such.

  6. added
    v3Under consideration for V3
    changeSuggested alteration to Pydantic, not a new feature nor a bug
    and removed
    bug V2Bug related to Pydantic V2
    pendingIs unconfirmed
    on Jul 31, 2024
  7. changed the title [-](🐞) model config merging doesn't respect mro[/-] [+]model config in inheritance doesn't respect MRO[/+] on Aug 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    changeSuggested alteration to Pydantic, not a new feature nor a bugv3Under consideration for V3

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions