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

Double validation in submit pipeline #21

Open
te25son opened this issue Aug 7, 2022 · 1 comment
Open

Double validation in submit pipeline #21

te25son opened this issue Aug 7, 2022 · 1 comment
Labels
performance This issue indicates a problem or improvement in performance

Comments

@te25son
Copy link
Owner

te25son commented Aug 7, 2022

There is a performance downgrade introduced in #19 .

Due to the inheritance of JokeSingleSubmit + the workflow of using JokeBase to match the joke type, "type" and "category" are validated twice in the submit pipeline.

Screen Shot 2022-08-07 at 4 10 21 PM

@te25son te25son added the performance This issue indicates a problem or improvement in performance label Aug 7, 2022
@te25son
Copy link
Owner Author

te25son commented Aug 8, 2022

After investigating the pydantic docs, it doesn't seem there's a possibility to skip only certain fields. Pydantic does offer a construct method on all models that inherit from BaseModel but simply creates the model without any validation.

It is possible to delay the validation until the JokeSingleSubmit is called by using the construct method to build the joke base. So something like

joke = JokeBase.construct(**data)

However, this would make the use of match_type less safe since the type hasn't been validated at this point. If the type is not valid, the method can raise a key error when converting the string to an enum in Type[self.type].

Of course, the type should never be invalid since it's coming directly from click which also checks that the input is valid 🤷‍♂️ Also the method is wrapped inside a @safe decorator, so even it if fails, it won't display anything to the user unless the debug flag is activated.

All in all, if we want to improve the performance slightly, while also having plenty of safety checks in place, we could simply use the construct method as mentioned above and delay the validation until the JokeSingleSubmit or JokeTwopartSubmit models are created.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance This issue indicates a problem or improvement in performance
Projects
None yet
Development

No branches or pull requests

1 participant