mypy now passes ekr-mypy branch

59 views
Skip to first unread message

Edward K. Ream

unread,
Mar 2, 2021, 6:17:43 AM3/2/21
to leo-editor
#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.

Edward

Edward K. Ream

unread,
Mar 2, 2021, 6:19:42 AM3/2/21
to leo-editor
On Tuesday, March 2, 2021 at 5:17:43 AM UTC-6 Edward K. Ream wrote:

#1557: Fix mypy complaints, is now complete. The corresponding PR is #1830.

I forgot to mention that I have added full type annotations to leoNodes.py. I have no plans to add more annotations at this time.

Edward

Félix

unread,
Mar 4, 2021, 7:47:20 PM3/4/21
to leo-editor
Everybody's into type checking now! Thats great! 

Edward K. Ream

unread,
Mar 5, 2021, 6:22:21 AM3/5/21
to leo-editor
On Thu, Mar 4, 2021 at 6:47 PM Félix <felix...@gmail.com> wrote:
Everybody's into type checking now! Thats great! 

:-)

Edward

tbp1...@gmail.com

unread,
Mar 5, 2021, 9:05:59 AM3/5/21
to leo-editor
I didn't get any output from the mypy command.  This code is in a node, and it's not an @file or @anything node:

@language python
def f1(x:int) -> int:
    y:int = 3*x
    return 'this is a test'
    #return y

print(f1(5))

When I ran the mypy command on this node, using <ALT-x>, I got no output in the Log pane or anywhere else.  When I ran mypy outside Leo on a file with the same code, I got the expected error message.

Leo 6.4-devel, devel branch, build 66a1c59a48
2021-03-04 06:50:33 -0600
Python 3.8.6, PyQt version 5.15.1
Windows 10 AMD64 (build 10.0.19041) SP0

Guidance, please!

Edward K. Ream

unread,
Mar 5, 2021, 9:09:35 AM3/5/21
to leo-editor
On Fri, Mar 5, 2021 at 8:06 AM tbp1...@gmail.com <tbp1...@gmail.com> wrote:

I didn't get any output from the mypy command.  This code is in a node, and it's not an @file or @anything node:

Leo's mypy command runs the mypy command on zero or more external files. In your case, zero :-)

Edward

tbp1...@gmail.com

unread,
Mar 5, 2021, 9:28:57 AM3/5/21
to leo-editor
From the F11 Help:

Run mypy on all nodes of the selected tree, or the first @<file> node in an ancestor.

I personally would like to be able to run mypy on a subtree that isn't part of an @file tree. I have a lot of code I use VR3 on, and I want to run mypy on that.  But at least I'd like the help message to not mislead me :).

Edward K. Ream

unread,
Mar 5, 2021, 11:25:44 AM3/5/21
to leo-editor
On Fri, Mar 5, 2021 at 8:28 AM tbp1...@gmail.com <tbp1...@gmail.com> wrote:
From the F11 Help:

Run mypy on all nodes of the selected tree, or the first @<file> node in an ancestor.

Rev 889bf7 in devel corrects the docstring. It now starts with:

    Run mypy on all @<file> nodes of the selected tree.
Edward
Reply all
Reply to author
Forward
0 new messages