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

support a field delete operator #1968

Closed
sasano8 opened this issue Oct 5, 2020 · 6 comments
Closed

support a field delete operator #1968

sasano8 opened this issue Oct 5, 2020 · 6 comments

Comments

@sasano8
Copy link

sasano8 commented Oct 5, 2020

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.

from pydantic import BaseModel, Delete

class Person(BaseModel):
  id: int
  name: str

class PersonCreate(Person):
  id: Delete

obj = PersonCreate(name='test')
print(hasattr(obj, 'id'))  # => False
@daviskirk
Copy link
Contributor

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.
Perhaps a less invasive way would be to "hide" the field so it won't show up in the schema and will through a validation error if it is set?

Would a pull request for something like this be accepted?

@sasano8
Copy link
Author

sasano8 commented Oct 20, 2020

Sure, the field delete feature would confuse mypy.

But I don't think it's a problem.
Because if you refer to it, you will get an "NoAttributeError".

Currently, I implemented the prefab function to express the difference.
Another way for difference expression.

This is useful in the following cases:

  • Remove the primary key for create.
  • All fields optionalize for patch.
@router.patch("/{id}/patch")
async def patch(self, id: int, data: User.prefab(suffix="Patch", exclude=["id"], requires=[], optionalize=[...])):
  ...

@jeanmonet
Copy link

jeanmonet commented Aug 1, 2022

I currently can inherit the model and then just delete the field from fields which word functionally but is really hacky. Perhaps a less invasive way would be to "hide" the field so it won't show up in the schema and will through a validation error if it is set?

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

@freenetwork
Copy link

@jeanmonet thx
I need solve

@agronick
Copy link

If you can add arbitrary fields by setting

class Config:
        extra = "allow"

you need a way to delete these fields. Currently there is none.

@adriangb
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants