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
support a field delete operator #1968
Comments
I also kind of need the deletion of a field but I don't see that as working well with the inheritance model in python in general (and I'm pretty sure that mypy won't understand or like that in any case) so I'll start with the simple export exclusion. I currently can inherit the model and then just delete the field from fields which word functionally but is really hacky. Would a pull request for something like this be accepted? |
Sure, the field delete feature would confuse mypy. But I don't think it's a problem. Currently, I implemented the prefab function to express the difference. This is useful in the following cases:
|
What I'm about to go for: class ModelBaseClass(pydantic.BaseModel):
...
delete_this_field: str
class ModelClass(ModelBaseClass):
pass
ModelClass.__fields__.pop("delete_this_field") This seems to produce the desired result, but haven't tested extensively. The field disappears from schema and no validation behavior seems to take place. Edit In some cases it may be necessary to remove (or replace) validators on other fields, for instance if those validators were taking as input the value of the deleted field: ModelClass.__fields__['some_field'].pre_validators # type: list
ModelClass.__fields__['some_field'].post_validators # type: list
# ^-- remove concerned validators from the relevant list using custom logic |
@jeanmonet thx |
If you can add arbitrary fields by setting class Config:
extra = "allow" you need a way to delete these fields. Currently there is none. |
Pydantic could implement this sort of thing at runtime but we won't be doing it until Python's typing ecosystem gets partial types, etc. That is bound to happen at some point and we don't want to have two different ways to do it when that happens. I'm closing this until that time comes. |
Thank you for the great library.
I want you to be able to delete a attribute of the inherited class.
If this request is accepted, I will make a pull request.
Similar issues #660
The above behavior depends on the exclude option.
This is a strong exclusion that removes the definition itself.
The text was updated successfully, but these errors were encountered: