From 1b43e4981ed4b6623c9f0553d9269bf07c36ed9e Mon Sep 17 00:00:00 2001 From: Simon Edwards Date: Tue, 17 Jan 2017 16:57:37 +0100 Subject: [PATCH] Fixes for all of the plugins. Added a script to invoke mypy. (I'm stiiiick of .bat files. They are just broken.) --- cura/CrashHandler.py | 12 ++++--- plugins/3MFReader/ThreeMFReader.py | 4 ++- plugins/3MFReader/__init__.py | 8 ++--- plugins/3MFWriter/ThreeMFWriter.py | 4 ++- .../WindowsRemovableDrivePlugin.py | 2 +- plugins/SliceInfoPlugin/SliceInfo.py | 5 +-- .../NetworkPrinterOutputDevicePlugin.py | 2 +- plugins/USBPrinting/USBPrinterOutputDevice.py | 2 +- .../USBPrinterOutputDeviceManager.py | 4 +-- plugins/USBPrinting/avr_isp/stk500v2.py | 4 +-- plugins/X3DReader/X3DReader.py | 4 ++- run_mypy.py | 31 +++++++++++++++++++ 12 files changed, 62 insertions(+), 20 deletions(-) create mode 100644 run_mypy.py diff --git a/cura/CrashHandler.py b/cura/CrashHandler.py index ba8499d4f26..b658f88824b 100644 --- a/cura/CrashHandler.py +++ b/cura/CrashHandler.py @@ -12,10 +12,14 @@ from UM.i18n import i18nCatalog catalog = i18nCatalog("cura") -try: - from cura.CuraVersion import CuraDebugMode -except ImportError: - CuraDebugMode = False # [CodeStyle: Reflecting imported value] +MYPY = False +if MYPY: + CuraDebugMode = False +else: + try: + from cura.CuraVersion import CuraDebugMode + except ImportError: + CuraDebugMode = False # [CodeStyle: Reflecting imported value] # List of exceptions that should be considered "fatal" and abort the program. # These are primarily some exception types that we simply cannot really recover from diff --git a/plugins/3MFReader/ThreeMFReader.py b/plugins/3MFReader/ThreeMFReader.py index 976f54ba252..5638ce551c1 100644 --- a/plugins/3MFReader/ThreeMFReader.py +++ b/plugins/3MFReader/ThreeMFReader.py @@ -17,8 +17,10 @@ from cura.QualityManager import QualityManager from UM.Scene.SceneNode import SceneNode +MYPY = False try: - import xml.etree.cElementTree as ET + if not MYPY: + import xml.etree.cElementTree as ET except ImportError: Logger.log("w", "Unable to load cElementTree, switching to slower version") import xml.etree.ElementTree as ET diff --git a/plugins/3MFReader/__init__.py b/plugins/3MFReader/__init__.py index 3e05cb8dc7d..cb4f9b97616 100644 --- a/plugins/3MFReader/__init__.py +++ b/plugins/3MFReader/__init__.py @@ -1,16 +1,16 @@ # Copyright (c) 2015 Ultimaker B.V. # Cura is released under the terms of the AGPLv3 or higher. +from typing import Dict from . import ThreeMFReader from . import ThreeMFWorkspaceReader from UM.i18n import i18nCatalog -import UM.Platform +from UM.Platform import Platform catalog = i18nCatalog("cura") - -def getMetaData(): +def getMetaData() -> Dict: # Workarround for osx not supporting double file extensions correclty. - if UM.Platform.isOSX(): + if Platform.isOSX(): workspace_extension = "3mf" else: workspace_extension = "curaproject.3mf" diff --git a/plugins/3MFWriter/ThreeMFWriter.py b/plugins/3MFWriter/ThreeMFWriter.py index 882740c4ed7..361cf796d01 100644 --- a/plugins/3MFWriter/ThreeMFWriter.py +++ b/plugins/3MFWriter/ThreeMFWriter.py @@ -7,8 +7,10 @@ from UM.Math.Matrix import Matrix from UM.Application import Application +MYPY = False try: - import xml.etree.cElementTree as ET + if not MYPY: + import xml.etree.cElementTree as ET except ImportError: Logger.log("w", "Unable to load cElementTree, switching to slower version") import xml.etree.ElementTree as ET diff --git a/plugins/RemovableDriveOutputDevice/WindowsRemovableDrivePlugin.py b/plugins/RemovableDriveOutputDevice/WindowsRemovableDrivePlugin.py index 14a4681bc37..42f3935f65b 100644 --- a/plugins/RemovableDriveOutputDevice/WindowsRemovableDrivePlugin.py +++ b/plugins/RemovableDriveOutputDevice/WindowsRemovableDrivePlugin.py @@ -8,7 +8,7 @@ from . import RemovableDrivePlugin import string -import ctypes +import ctypes # type: ignore from ctypes import wintypes # Using ctypes.wintypes in the code below does not seem to work from UM.i18n import i18nCatalog diff --git a/plugins/SliceInfoPlugin/SliceInfo.py b/plugins/SliceInfoPlugin/SliceInfo.py index 4f39fd4818d..05f7c0e6f5a 100644 --- a/plugins/SliceInfoPlugin/SliceInfo.py +++ b/plugins/SliceInfoPlugin/SliceInfo.py @@ -1,5 +1,6 @@ # Copyright (c) 2015 Ultimaker B.V. # Cura is released under the terms of the AGPLv3 or higher. +from typing import Any from cura.CuraApplication import CuraApplication @@ -26,8 +27,8 @@ catalog = i18nCatalog("cura") class SliceInfoJob(Job): - data = None - url = None + data = None # type: Any + url = None # type: str def __init__(self, url, data): super().__init__() diff --git a/plugins/UM3NetworkPrinting/NetworkPrinterOutputDevicePlugin.py b/plugins/UM3NetworkPrinting/NetworkPrinterOutputDevicePlugin.py index 2725fa8d17b..fe35b60de5c 100644 --- a/plugins/UM3NetworkPrinting/NetworkPrinterOutputDevicePlugin.py +++ b/plugins/UM3NetworkPrinting/NetworkPrinterOutputDevicePlugin.py @@ -1,7 +1,7 @@ from UM.OutputDevice.OutputDevicePlugin import OutputDevicePlugin from . import NetworkPrinterOutputDevice -from zeroconf import Zeroconf, ServiceBrowser, ServiceStateChange, ServiceInfo +from zeroconf import Zeroconf, ServiceBrowser, ServiceStateChange, ServiceInfo # type: ignore from UM.Logger import Logger from UM.Signal import Signal, signalemitter from UM.Application import Application diff --git a/plugins/USBPrinting/USBPrinterOutputDevice.py b/plugins/USBPrinting/USBPrinterOutputDevice.py index e344caee1df..7adb0b0d084 100644 --- a/plugins/USBPrinting/USBPrinterOutputDevice.py +++ b/plugins/USBPrinting/USBPrinterOutputDevice.py @@ -2,7 +2,7 @@ # Cura is released under the terms of the AGPLv3 or higher. from .avr_isp import stk500v2, ispBase, intelHex -import serial +import serial # type: ignore import threading import time import queue diff --git a/plugins/USBPrinting/USBPrinterOutputDeviceManager.py b/plugins/USBPrinting/USBPrinterOutputDeviceManager.py index 4dec2e3a06e..ed97076df65 100644 --- a/plugins/USBPrinting/USBPrinterOutputDeviceManager.py +++ b/plugins/USBPrinting/USBPrinterOutputDeviceManager.py @@ -258,7 +258,7 @@ def connectedPrinterList(self): def getSerialPortList(self, only_list_usb = False): base_list = [] if platform.system() == "Windows": - import winreg #@UnresolvedImport + import winreg # type: ignore @UnresolvedImport try: key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE,"HARDWARE\\DEVICEMAP\\SERIALCOMM") i = 0 @@ -277,4 +277,4 @@ def getSerialPortList(self, only_list_usb = False): base_list = base_list + glob.glob("/dev/ttyUSB*") + glob.glob("/dev/ttyACM*") + glob.glob("/dev/cu.*") + glob.glob("/dev/tty.usb*") + glob.glob("/dev/rfcomm*") + glob.glob("/dev/serial/by-id/*") return list(base_list) - _instance = None + _instance = None # type: "USBPrinterOutputDeviceManager" diff --git a/plugins/USBPrinting/avr_isp/stk500v2.py b/plugins/USBPrinting/avr_isp/stk500v2.py index 91bef538759..dbfc8dc7563 100644 --- a/plugins/USBPrinting/avr_isp/stk500v2.py +++ b/plugins/USBPrinting/avr_isp/stk500v2.py @@ -7,7 +7,7 @@ import sys import time -from serial import Serial +from serial import Serial # type: ignore from serial import SerialException from serial import SerialTimeoutException from UM.Logger import Logger @@ -184,7 +184,7 @@ def recvMessage(self): def portList(): ret = [] - import _winreg + import _winreg # type: ignore key=_winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,"HARDWARE\\DEVICEMAP\\SERIALCOMM") #@UndefinedVariable i=0 while True: diff --git a/plugins/X3DReader/X3DReader.py b/plugins/X3DReader/X3DReader.py index 0a81e98d0dd..f78023dfab2 100644 --- a/plugins/X3DReader/X3DReader.py +++ b/plugins/X3DReader/X3DReader.py @@ -13,8 +13,10 @@ from UM.Mesh.MeshReader import MeshReader from UM.Scene.SceneNode import SceneNode +MYPY = False try: - import xml.etree.cElementTree as ET + if not MYPY: + import xml.etree.cElementTree as ET except ImportError: import xml.etree.ElementTree as ET diff --git a/run_mypy.py b/run_mypy.py new file mode 100644 index 00000000000..7c203f87d99 --- /dev/null +++ b/run_mypy.py @@ -0,0 +1,31 @@ +#!env python +import os +import subprocess + +os.putenv("MYPYPATH", r".;.\plugins;..\Uranium_hint\;..\Uranium_hint\stubs\\" ) + +def findModules(path): + result = [] + for entry in os.scandir(path): + if entry.is_dir() and os.path.exists(os.path.join(path, entry.name, "__init__.py")): + result.append(entry.name) + return result + +plugins = findModules("plugins") +plugins.sort() + +mods = ["cura"] + plugins + +for mod in mods: + print("------------- Checking module {mod}".format(**locals())) + result = subprocess.run(["python", r"c:\python35\Scripts\mypy", "-p", mod]) + if result.returncode != 0: + print(""" +Module {mod} failed checking. :( +""".format(**locals())) + break +else: + print(""" + +Done checking. All is good. +""")