scipy-2023-pydantic-tutorial

Summary and Conclusion

Summary

In this tutorial we have demonstrated how Pydantic can improve the user experience of scientific Python software. In this introductory tutorial we have covered:

This sets the foundation for using Pydantic in your own projects.

Some General Thoughts

Pydantic is not meant to introduce static typing to Python “through the backdoor”. So it may not make sense to use it for every small script, that you only use for yourself or share with a few colleagues. However, if you are writing a larger application, that is used by many people, or an application that is re-used with many different inout configurations, Pydantic will be a great tool to improve the safety of use and user experience of your application. Personally we think Pydantic should be used where the type conversion and validation brings the most value.

As we listed in the title of this tutorial, we consider the most important use cases of Pydantic to be:

Conclusion and Additional Thoughts

Editor Plugins

Pydantic introduces some specific functionality, which is not supported by all editors out of the box. This includes for example the validator decorator, which is used to define a class method, but not recognized as such by all editors. However, there are plugins available for most editors, which add support for Pydantic.

Let’s dive deeper into the above example of the validator decorator. If we use base PyCharm as our IDE, we will get a yellow squiggly line under the cls parameter as shown in the screen show below:

This is because the validator decorator converts the instance method to a class method, but PyCharm still thinks the method is an instance method whose first parameter should be self. You can see in this warning message in the screenshot as I have hovered over the yellow squiggly lines. If I then install the Pydantic plugin for PyCharm, you will see that the method is now correctly recognized as a class method and the yellow squiggly line goes away:

This is just one quality of life improvement from the Pydantic plugin, but the plugin contains many others.

A Note On Pydantic v2.0

On June 30th 2023 Pydantic v2.0 was released. It was a bit to short notice to include it in this tutorial. However, it is a major release and it is worth mentioning it here. Pydantiv v2.0 inlcudes a major refactoring of the actual validation code, which is now fully implemented in Rust and exposed to Python, which makes Pydantic much faster. This can be relevant e.g. for large data sets.

It is supposed to be mostly backwards compatible. However, there are some changes that might break existing code. The most important changes affecting things learned in this tutorial are:

API changes to the BaseModel class:

pydantic_v2.0.png

Some of the decorators have been renamed:

Under the following links you can find some more details on Pydantic v2.0:

We hope you enjoyed this tutorial and learned something new. If you have any questions or comments, please feel free to contact us.