Open Viicos opened 8 months ago
Agreed. PRs welcome for parts or all of the above features. Ping me if you want to chat more about implementation details :).
@sydney-runkle @Viicos I had started to look into implementing point 4, and from my testing, the @deprecated
decorator on a model does emit a runtime warning when using the model.
My question is, should a warning also be emitted in the following scenario?
@deprecated
class DeprecatedModel(BaseModel):
pass
class NewModel(BaseModel):
field: DeprecatedModel
Do you think that in this case Pydantic should emit a warning along the lines of Usage of deprecated model `DeprecatedModel`
?
That's great to hear!
Do you think that in this case Pydantic should emit a warning along the lines of
Usage of deprecated model `DeprecatedModel`
?
Do you mean the warning should be emitted during class creation? (And thus whenever the model's module is imported). That might be too invasive, although I can't think of a use case where a user would be constrained to use a deprecated model as a field in the model he is defining. Let's maybe here what the maintainers think of it
Do you mean the warning should be emitted during class creation? (And thus whenever the model's module is imported). That might be too invasive, although I can't think of a use case where a user would be constrained to use a deprecated model as a field in the model he is defining. Let's maybe here what the maintainers think of it
I actually meant to emit a warning when a user defines a model with one of its fields being a deprecated model, not when the deprecated model is created/imported.
In my examples above, when creating NewModel
, we could check to see if a field is defined with the type of another subclass of BaseModel
and then check if has the __deprecated__
attribute.
Also, just a side note, I don't think we should support the config based deprecation above, but the deprecated decorator approach is great.
Has any progress been made on:
When instantiating a model with a deprecated field explicitly specified (Model(deprecated_field=...)), the runtime warning could be emitted.
I'm currently doing this with a validator, but this would be nice to have.
Nope, not yet @allenlawrence94. Feel free to open a PR with support this!
Initial Checks
Description
https://github.com/pydantic/pydantic/pull/8237 implemented support for deprecated fields. While doing so, we encountered/postponed some extra features that could be nice to have:
When using the
deprecated
decorator class, extra information (such as thecategory
andstacklevel
) can be passed. It would be great if Pydantic could support such attributes when emitting the runtime warning. For reference, this commit implements the feature: https://github.com/pydantic/pydantic/pull/8237/commits/cf06a7322d59aefc2e3ca607f0c5ee66d28f6757 (extra care should be taken with thetyping-extensions
version -- Only 4.9.0 and greater support the class implementation of thedeprecated
decorator).When instantiating a model with a deprecated field explicitly specified (
Model(deprecated_field=...)
), the runtime warning could be emitted.Support for Pydantic/stdlib dataclasses: not sure if there are any technical limitations for this one?
@deprecated
decorator:The decorator takes care of the runtime warning (tested locally and doesn't seem to cause any issues with the model metaclass). However, the JSON Schema should reflect the deprecation.
Field
:I wouldn't really recommend the second option, as type checkers won't be aware of the deprecation (same for
computed_field
) https://github.com/pydantic/pydantic/pull/9298These were the ideas I had while implementing support for deprecated fields. Some of them requires some discussion, as I'm not sure whether it should be supported or not. Happy to hear your opinions
Affected Components
.model_dump()
and.model_dump_json()
model_construct()
, pickling, private attributes, ORM mode