Rectify nearly all type errors revealed by mypy on current repo
#3267
+303
−140
mypy on current repo
#3267