Thanks for the link. Your post is timely.
In the last few days, I have gone from a mypy newbie to a mypy journeyman. Just today, in the ekr-annotations branch, mypy passes the fully annotated version of leoNodes.py.
The following statement is almost completely wrong:
"evaluating these annotations requires
computation, so all programs pay the price of the annotations, even if they
are never needed."
Annotations are pretty much the same as comments. They have no significant cost.
The following is a real problem:
"
forward
references to types that have not yet been defined requires using string
literals, instead of type name".
The problem is dealing with the forward reference itself. Imo, the ugliness of the string itself isn't significant.
The typing module defines the
TYPE_CHECKING constant, which is
always False at runtime. mypy, and other checkers, set this constant to True while doing analysis.
At present, the following appears at the start of leoGlobals.py:
# Do the following imports *only* when running mypy.
if TYPE_CHECKING: # Always False at runtime.
import leo.core.leoCommands as leoCommands
import leo.core.leoNodes as leoNodes
assert leoCommands
assert leoNodes
Yes, this is ugly, but it allows string annotations like this:
def findTabWidthDirectives(
c:"leoCommands.Commands", p:"leoNodes.Position"):
Imo, annotations will become an important part of Leo. mypy isn't perfect, but it is flexible and easy to use. I plan to fully annotate only Leo's crown jewels (the modules corresponding to c, g, and p), namely the leoCommands, leoGlobals, and leoNodes modules. mypy has already found about a half dozen bugs that pylint and Leo's unit tests have missed.
Leo will fail to start if leoGlobals.py imports any other Leo module. leoGlobals.py can deal with forward references, as shown above.
leoNodes.py also uses forward references to the Position and Vnode class, but those references don't require
the
TYPE_CHECKING constant the referenced classes appear in leoNodes.py.
I care little or nothing what Python 3.10 may bring. Imo, mypy and python's typing module seem sufficient just as they are.