#1557: Fix mypy complaints, is now complete. The corresponding PR is
#1830.
This post summarizes what I learned.
mypy is a superb tool
mypy is much easier to use than I expected! I only skimmed documentation from time to time. After fixing a few mypy complaints I started to get a feel for how mypy works and what it expects.
mypy supports gradual type checking. The more type hints a program contains, the more mypy checks. At no time did I need to know how mypy checks types. Everything just works.
Running mypy on all of Leo's files is easy. From the leo-editor folder do:
mypy launchLeo.py
mypy is fast enough that there is no need to restrict type checking to a particular file.
After I figure out how to use mypy, it took only a few hours to
remove all the initial complaints. The `type: ignore` comment tells mypy
to suppress an error. Later, I discovered how to remove some `type:
ignore` comments.
mypy settings
mypy looks for a .mypy.ini file in the current directory and in other places. I wasn't able to get mypy to look for the config file in my home directory. Anyway, leo-editor is the best place for .mypy.ini.
mypy has a
huge number of settings, but Leo's .mypy.ini file is short and simple. It just tells mypy not to follow imports in leo.external.npyscreen and leo.core.leoQt.
mypy requires minimal annotations
mypy will complain about top-level dicts and lists without annotations. The required hints are useful! Many such dicts and lists already have comments.
Some annotations clear up difficult code. My favorite is in leoConfig.py:
# Keys are settings names, values are (type,value) tuples.
settingsDict: Dict[str, Tuple[str, Union[g.TypedDict, g.GeneralSetting]]] = {}
The hint says that values can be either a g.TypedDict or a g.GeneralSetting.
mypy finds cruft
mypy complaints highlighted several dubious methods and chunks of code, which I have revised or eliminated. See the diffs in the PR for details.
Summary
mypy is amazingly easy to learn and to use.
mypy is flexible. Initially, mypy requires type annotations only for a few top-level lists and dicts. mypy will require more annotations (and report more errors) as you add more annotations.
mypy's required annotations are valuable, and removing mypy complaints generally makes code easier to understand.
I'll merge the ekr-mypy branch into devel in a day or two. Please feel free to review the code changes.