Do not inherit config in model fields that are themselves of type model #612
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I think the current mechanism of attempting to merge configs when handling fields of model types is the source of some problems.
I'll describe the problems below, but given how we want to get the v2 release out ASAP, I really think we should drop this functionality in pydantic v2 (or at least find a way to make it possible to add in a future minor release) so that we don't find ourselves committed to dealing with bugs related to it.
I'll also note that this didn't seem to be a thing in v1 (a point which I'll make in the companion PR for this on the
pydantic
side), so it's not a breaking change to remove this behavior. (And in fact, improves backwards-compatibility.)First, this is likely to be a source of bugs due to the fact that core refs don't reflect config settings, and if the schema gets simplified to use definitions, config settings will be lost. For example, consider:
If the schema is simplified in such a way that the
C
becomes a definition reference instead of duplicated, the behavior of C inside of B would be the same as inside of A. Right now we don't have a way to bake the config settings into the refs to prevent this, and I think it might be unreasonably difficult to try.Even if we were smart about not out-of-lining C, I think you'll run into more challenging issues with recursive models. For example:
In this case, A has to be a definition reference, and to behave correctly where the string gets lowercased only for the cases where it is a subfield of B, you'd need to make sure to have different refs for the outer A and the one that has the config settings. One can also imagine more complex scenarios where the config takes different values at different levels of nesting; basically, if config influences the behavior of validation/serialization, it must be the same when the ref is the same. By eliminating config inheritance for fields, we eliminate the most common (and perhaps only?) way to get differing config with the same ref today.
Second, even if we did want to keep this functionality, it is currently causing problems because some config fields almost certainly should not get inherited by fields. The main example being
extra
, which is causing issue pydantic/pydantic#5784, which incidentally does get fixed by this PR.In general, I would argue that if you want config inheritance, it's probably better to find a way to achieve the desired result by way of a shared base class, rather than trying to modify the config by way of inclusion in a different parent. I realize this is not equally capable, but I (currently) think its better to remove this capability and not try to support it for users in v2 (especially considering it wasn't even a thing in v1), than to deal with the various edge-case bugs it may cause.
The one exception to this would be if we made some kind of "global" config thing, so that it affected the entire core schema the same way, rather than being able to be overridden in fields. But I would argue that the current mechanism is too different to be a step toward that, and would prefer that we find a way to add that later.