2024-08-28 –, Room 6
Many scientific Python packages are "stringly typed," i.e., using strings to select algorithms or methods and dictionaries for configuration. While easy for beginners and convenient for authors, these libraries miss out on static typing benefits like error detection before runtime and autocomplete. This talk shares insights from redesigning the optimagic library from the ground up with static typing in mind. Without compromising on simplicity, we achieve better static analysis, autocomplete, and fewer runtime errors. The insights are not specific to numerical optimization and apply to a wide range of scientific Python packages.
Most libraries in the scientific Python ecosystem are "stringly typed". For example, many packages allow users to switch between multiple algorithms or methods by passing a string with the method name; Advanced configuration often relies on dictionaries of keyword arguments. "Stringly typed" libraries are easy to use for beginners and convenient to implement for library authors. However, they miss out on several benefits of static typing.
- Push errors left: Instead of exceptions at runtime, many errors can be discovered by an IDE, type checker, or other automated tools.
- Autocomplete: An IDE shows all available arguments and options and their types. This serves as quick documentation and saves typing (and typos).
- Robustness: Static analysis provides an additional safety layer on top of unit tests.
To achieve these benefits, it is not enough to annotate existing code with type hints. Instead, the code has to be redesigned from the ground up with static typing in mind.
This talk shares insights from redesigning "optimagic" (a Python package for numerical optimization) to get the most out of type hints. While preserving backward compatibility, we establish new recommended ways of using the library. Users who adopt the new workflow benefit immediately from better autocomplete, better static analysis, and fewer runtime errors. Most importantly, we do not compromise on simplicity for beginner users. While the talk focuses on benefits for users, maintainers and library authors also benefit via improved maintainability and extensibility.
The lessons learned from the redesign are not specific to the case study. They apply to any package in the scientific ecosystem that lets users select between multiple algorithms or methods, all of which have different optional arguments. Prime examples are plotting libraries, ODE solvers, Numerical Integration packages, and many more. Moreover, they are relevant for anyone adopting type hints in large existing codebases.
none
Expected audience expertise: Python:some
Abstract as a tweet:Unlocking the full power of static typing requires more than type hints. Join @JanosGabler as he shares insights from redesigning a Python library for enhanced robustness, static analysis, and superior autocomplete.
Category [Community, Education, and Outreach]:Learning and Teaching Scientific Python
Author of optimagic / estimagic | Senior AI Researcher at appliedAI | Expert in numerical optimization | Building Bandsaws, Pizza Ovens and Furniture