Description
It turns out dynamic typing is a bad idea for writing serious software, as it’s trivial to introduce bugs that would make code simply fail to compile in statically typed languages (e.g. #3737). The JS community has TypeScript, and Python has mypy and the typing
library in the stdlib. Nikola was started before mypy and attrs/dataclasses became a thing. The coding style is more laissez-faire, with ad-hoc tuples and other things that aren’t neatly structured. There are a few places that are newer that use typing
or dataclasses
.
What should be done?
- Add type annotations to all functions, methods, and class attributes; variable annotations may be added where useful
- Leverage the
typing
library — say what a structure is (typing.List[str]
instead oflist
), remembering we need to support Python 3.8 or newer (which means sometyping
features are not available, and which means we can’t use the newerlist[str]
syntax) - Consider getting rid of untyped, ad-hoc structures:
- replace large or highly-used tuples with dataclasses
- replace dictionaries that have a constant set of attributes and are passed around with dataclasses
- replace namedtuples with dataclasses
- where dictionaries make sense, consider TypedDict
- Consider converting some data-heavy classes to
@dataclasses.dataclass
- Consider getting rid of dynamicness/magic where it makes things harder to reason about without any clear benefits
This is a good first issue for people who never touched Nikola, including people who aren’t its users (yet). It is also a great opportunity to learn typing
in Python. We’re not expecting one person or one PR to handle everything :)