Book Description

Does it seem like your Python projects are getting bigger and bigger? Are you feeling the pain as your codebase grows and gets tougher to debug and maintain? Python is an easy language to learn and use, but that also means systems can quickly grow beyond comprehension. Thankfully, Python has features to help developers overcome maintainability woes.

In this practical book, author Patrick Viafore shows you how to use Python's type system to the max. You'll look at user-defined types, such as classes and enums, and Python's type hinting system. You'll also learn how to make Python extensible and how to use a comprehensive testing strategy as a safety net. With these tips and techniques, you'll write clearer and more maintainable code.

  • Learn why types are essential in modern development ecosystems
  • Understand how type choices such as classes, dictionaries, and enumerations reflect specific intents
  • Make Python extensible for the future without adding bloat
  • Use the tools provided to increase the safety and robustness of your codebase
  • Evaluate current code to see common maintainability gotchas
  • Make your codebase more maintainable with off-the-shelf open source tools

Table of Contents

  1. 1. Introduction to Robust Python
    1. Robustness
      1. What Does “Robust” Mean?
      2. Why Does Robustness Matter?
    2. What’s Your Intent?
      1. Asynchronous Communication
    3. Examples of Intent In Python
      1. Collections
      2. Iteration
      3. Law of Least Surprise
    4. Wrap-up
  2. 2. Introduction to Python Types
    1. What’s In a Type?
      1. Mechanical Representation
      2. Semantic Representation
    2. Typing Systems
      1. Strong vs. Weak
      2. Dynamic vs. Static
      3. Duck Typing
    3. Wrap-up
  3. 3. Type Annotations
    1. Type Annotations
    2. Benefits
      1. Autocomplete
      2. Typecheckers
      3. Exercise: Spot the Bug
    3. When To Use
    4. Wrap-up
  4. 4. Constraining Types
    1. Optional Type
    2. Union Types
      1. Product and Sum Types
    3. Literal Types
    4. Annotated Types
    5. NewType
    6. Final Types
    7. Wrap-up
  5. 5. Collection Types
    1. Annotating Collections
    2. Homogeneous vs. Heterogeneous Collections
    3. TypedDict
    4. Creating New Collections
      1. Generics
      2. Modifying Existing Types
      3. As Easy as ABC
    5. Wrap-up