Hypermodern Python Tooling

Book description

Keeping up with the Python ecosystem can be daunting. Its developer tooling doesn't provide the out-of-the-box experience native to languages like Rust and Go. When it comes to long-term project maintenance or collaborating with others, every Python project faces the same problem: how to build reliable workflows beyond local development while staying in sync with the evolving ecosystem.

With this hands-on guide, Python developers will learn how to forge the moving parts of a Python project into an easy-to-use toolchain, using state-of-the-art tools including Poetry, Nox, pytest, mypy, pre-commit, Black, Ruff, uv, Rye, Hatch, and more. Author Claudio Jolowicz shows you how to create robust Python project structures complete with unit tests, static analysis, code formatting, and type checking.

You'll learn how to:

  • Create open source projects with state-of-the-art infrastructure
  • Build a custom infrastructure for all Python projects in a company or team
  • Improve and modernize the infrastructure of an existing Python project
  • Evaluate modern Python tooling for adoption in existing projects
  • Use tools for packaging and dependency management
  • Automate common development tasks such as testing, dependency updates, and publishing releases

Publisher resources

View/Submit Errata

Table of contents

  1. Preface
    1. Who Should Read This Book?
    2. How This Book Is Organized
    3. References and Further Reading
    4. Conventions Used in This Book
    5. Using Code Examples
    6. O’Reilly Online Learning
    7. How to Contact Us
    8. Acknowledgments
  2. I. Working with Python
  3. 1. Installing Python
    1. Supporting Multiple Versions of Python
    2. Locating Python Interpreters
    3. Installing Python on Windows
    4. The Python Launcher for Windows
    5. Installing Python on macOS
      1. Homebrew Python
      2. The python.org Installers
    6. Installing Python on Linux
      1. Fedora Linux
      2. Ubuntu Linux
      3. Other Linux Distributions
    7. The Python Launcher for Unix
    8. Installing Python with pyenv
    9. Installing Python from Anaconda
    10. A Brave New World: Installing with Hatch and Rye
    11. An Overview of Installers
    12. Summary
  4. 2. Python Environments
    1. A Tour of Python Environments
      1. Python Installations
      2. The Per-User Environment
      3. Virtual Environments
    2. Installing Applications with pipx
      1. pipx in a Nutshell
      2. Installing pipx
      3. Managing Applications with pipx
      4. Running Applications with pipx
      5. Configuring pipx
    3. Managing Environments with uv
    4. Finding Python Modules
      1. Module Objects
      2. The Module Cache
      3. Module Specs
      4. Finders and Loaders
      5. The Module Path
      6. Site Packages
      7. Back to the Basics
    5. Summary
  5. II. Python Projects
  6. 3. Python Packages
    1. The Package Lifecycle
    2. An Example Application
    3. Why Packaging?
    4. The pyproject.toml File
    5. Building Packages with build
    6. Uploading Packages with Twine
    7. Installing Projects from Source
    8. Project Layout
    9. Managing Packages with Rye
    10. Wheels and Sdists
    11. Project Metadata
      1. Naming Projects
      2. Versioning Projects
      3. Dynamic Fields
      4. Entry-Point Scripts
      5. Entry Points
      6. Authors and Maintainers
      7. The Description and README
      8. Keywords and Classifiers
      9. The Project URLs
      10. The License
      11. The Required Python Version
      12. Dependencies and Optional Dependencies
    12. Summary
  7. 4. Dependency Management
    1. Adding Dependencies to the Example Application
      1. Consuming an API with HTTPX
      2. Console Output with Rich
    2. Specifying Dependencies for a Project
      1. Version Specifiers
      2. Extras
      3. Environment Markers
    3. Development Dependencies
      1. An Example: Testing with pytest
      2. Optional Dependencies
      3. Requirements Files
    4. Locking Dependencies
      1. Freezing Requirements with pip and uv
      2. Compiling Requirements with pip-tools and uv
    5. Summary
  8. 5. Managing Projects with Poetry
    1. Installing Poetry
    2. Creating a Project
      1. The Project Metadata
      2. The Package Contents
      3. The Source Code
    3. Managing Dependencies
      1. Caret Constraints
      2. Extras and Environment Markers
      3. The Lock File
      4. Updating Dependencies
    4. Managing Environments
    5. Dependency Groups
    6. Package Repositories
      1. Publishing Packages to Package Repositories
      2. Fetching Packages from Package Sources
    7. Extending Poetry with Plugins
      1. Generating Requirements Files with the Export Plugin
      2. Deploying Environments with the Bundle Plugin
      3. The Dynamic Versioning Plugin
    8. Summary
  9. III. Testing and Static Analysis
  10. 6. Testing with pytest
    1. Writing a Test
    2. Managing Test Dependencies
    3. Designing for Testability
    4. Fixtures and Parameterization
    5. Advanced Techniques for Fixtures
    6. Extending pytest with Plugins
      1. The pytest-httpserver Plugin
      2. The pytest-xdist Plugin
      3. The factory-boy and faker Libraries
      4. Other Plugins
    7. Summary
  11. 7. Measuring Coverage with Coverage.py
    1. Using Coverage.py
    2. Branch Coverage
    3. Testing in Multiple Environments
    4. Parallel Coverage
    5. Measuring in Subprocesses
    6. What Coverage to Aim For
    7. Summary
  12. 8. Automation with Nox
    1. First Steps with Nox
    2. Working with Sessions
    3. Working with Multiple Python Interpreters
    4. Session Arguments
    5. Automating Coverage
    6. Session Notification
    7. Automating Coverage in Subprocesses
    8. Parameterizing Sessions
    9. Session Dependencies
    10. Using Nox with Poetry Projects
    11. Locking Dependencies with nox-poetry
    12. Summary
  13. 9. Linting with Ruff and pre-commit
    1. Linting Basics
    2. The Ruff Linter
      1. Pyflakes and pycodestyle
      2. Fantastic Linters and Where to Find Them
      3. Disabling Rules and Warnings
      4. Automation with Nox
    3. The pre-commit Framework
      1. First Steps with pre-commit
      2. A Hook Up Close
      3. Automatic Fixes
      4. Running pre-commit from Nox
      5. Running pre-commit from Git
    4. The Ruff Formatter
      1. Approaches to Code Formatting: autopep8
      2. Approaches to Code Formatting: YAPF
      3. An Uncompromising Code Formatter
      4. The Black Code Style
      5. Formatting Code with Ruff
    5. Summary
  14. 10. Using Types for Safety and Inspection
    1. Benefits and Costs of Type Annotations
    2. A Brief Tour of Python’s Typing Language
      1. Variable Annotations
      2. The Subtype Relation
      3. Union Types
      4. Gradual Typing
      5. Function Annotations
      6. Annotating Classes
      7. Type Aliases
      8. Generics
      9. Protocols
      10. Compatibility with Older Python Versions
    3. Static Type Checking with mypy
      1. First Steps with mypy
      2. Revisiting the Wikipedia Example
      3. Strict Mode
      4. Automating mypy with Nox
      5. Distributing Types with Python Packages
      6. Type Checking the Tests
    4. Inspecting Type Annotations at Runtime
      1. Writing a @dataclass Decorator
      2. Runtime Type Checking
      3. Serialization and Deserialization with cattrs
    5. Runtime Type Checking with Typeguard
    6. Summary
  15. Index
  16. About the Author

Product information

  • Title: Hypermodern Python Tooling
  • Author(s): Claudio Jolowicz
  • Release date: June 2024
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781098139582