Robust Python

I learned about the new book Robust Python from the Talk Python To Me Podcast. I really liked the book and recommend it to anyone who has been working with Python for some time and who already needs to maintain the written code, especially if you are not the only one who writes the code.

Book covers Python 3.9.

This is the new book (July 2021). The book discusses how to write code that is easy to change, maintain, and generally understand what is happening in the code.

This book is not a reference book. For all topics, there is introductory information about the topic, but the rest is about trying the technology in practice.

Because of this, it is better to have at least minimal familiarity with the topics of the book, otherwise it can be difficult to read when and how best to use the topic, when it is still not really clear what it is (Fluent Python is perfect for learning topics, especially the 2nd edition, which will be released at the end of the year).

Things I loved:

  • code examples with food :)
  • I learned new things (mutation testing, mypy alternatives, security analysis tools, code complexity tools)
  • I got a lot of ideas
  • the book was interesting to read
  • design patterns with the freedom not to use classes :)

Main topics:

  • Annotating Your Code with Types
  • Defining Your Own Types (Enums, Data Classes, Classes, Protocols, subtyping, pydantic)
  • Extensible Python
  • Building a Safety Net (static analysis and testing)

Almost a third of the book is devoted to type annotation, namely, how best to write annotation in specific cases and what it gives in terms of code maintenance, modification and error finding. Specific examples are covered of how best to write an annotation in a particular case. Plus, since the book is new, it discusses new functionality in type annotation that was added in Python 3.8, 3.9.

The big plus in the section on type annotation is that it covers not only mypy, but also other similar projects, discusses their differences and interesting features of each.

The second section covers creating your own types, here are topics such as Enum, Data Classess, classes, interfaces, protocols, subtyping and using pydantic. Everything with practical application and an explanation of why and when it is better to use one or another version of creating types.

The third section looks at code dependencies. This is not so much about the modules on which the code depends, but about the dependencies of one part of the code on another. Plus, several design patterns are covered and what I especially liked, the patterns are shown in practical examples, where it is necessary on functions, where it is necessary on classes, and not necessarily as they look classically in OOP.

The fourth section deals with static code analysis and testing. Static code analysis covers linters, code complexity checking, security analysis. The testing sections cover not only common unit tests, but acceptance, property-based and mutation testing.