--- a/eric7/Plugins/CheckerPlugins/CodeStyleChecker/Imports/ImportsChecker.py Wed Dec 01 18:03:11 2021 +0100 +++ b/eric7/Plugins/CheckerPlugins/CodeStyleChecker/Imports/ImportsChecker.py Wed Dec 01 20:09:57 2021 +0100 @@ -8,6 +8,7 @@ """ import copy +import sys class ImportsChecker: @@ -15,6 +16,8 @@ Class implementing a checker for import statements. """ Codes = [ + ## Local imports + "I101", "I102", "I103", ] def __init__(self, source, filename, tree, select, ignore, expected, @@ -36,11 +39,11 @@ @type list of str @param repeat flag indicating to report each occurrence of a code @type bool - @param args dictionary of arguments for the miscellaneous checks + @param args dictionary of arguments for the various checks @type dict """ self.__select = tuple(select) - self.__ignore = ('',) if select else tuple(ignore) + self.__ignore = ("",) if select else tuple(ignore) self.__expected = expected[:] self.__repeat = repeat self.__filename = filename @@ -55,6 +58,7 @@ self.errors = [] checkersWithCodes = [ + (self.__checkLocalImports, ("I101", "I102", "I103")), ] self.__checkers = [] @@ -127,3 +131,73 @@ for check in self.__checkers: check() + + def getStandardModules(self): + """ + Public method to get a list of modules of the standard library. + + @return set of builtin modules + @rtype set of str + """ + try: + return sys.stdlib_module_names + except AttributeError: + return { + "__future__", "__main__", "_dummy_thread", "_thread", "abc", + "aifc", "argparse", "array", "ast", "asynchat", "asyncio", + "asyncore", "atexit", "audioop", "base64", "bdb", "binascii", + "binhex", "bisect", "builtins", "bz2", "calendar", "cgi", + "cgitb", "chunk", "cmath", "cmd", "code", "codecs", "codeop", + "collections", "colorsys", "compileall", "concurrent", + "configparser", "contextlib", "contextvars", "copy", "copyreg", + "cProfile", "crypt", "csv", "ctypes", "curses", "dataclasses", + "datetime", "dbm", "decimal", "difflib", "dis", "distutils", + "doctest", "dummy_threading", "email", "encodings", + "ensurepip", "enum", "errno", "faulthandler", "fcntl", + "filecmp", "fileinput", "fnmatch", "formatter", "fractions", + "ftplib", "functools", "gc", "getopt", "getpass", "gettext", + "glob", "grp", "gzip", "hashlib", "heapq", "hmac", "html", + "http", "imaplib", "imghdr", "imp", "importlib", "inspect", + "io", "ipaddress", "itertools", "json", "keyword", "lib2to3", + "linecache", "locale", "logging", "lzma", "mailbox", "mailcap", + "marshal", "math", "mimetypes", "mmap", "modulefinder", + "msilib", "msvcrt", "multiprocessing", "netrc", "nis", + "nntplib", "numbers", "operator", "optparse", "os", + "ossaudiodev", "parser", "pathlib", "pdb", "pickle", + "pickletools", "pipes", "pkgutil", "platform", "plistlib", + "poplib", "posix", "pprint", "profile", "pstats", "pty", "pwd", + "py_compile", "pyclbr", "pydoc", "queue", "quopri", "random", + "re", "readline", "reprlib", "resource", "rlcompleter", + "runpy", "sched", "secrets", "select", "selectors", "shelve", + "shlex", "shutil", "signal", "site", "smtpd", "smtplib", + "sndhdr", "socket", "socketserver", "spwd", "sqlite3", "ssl", + "stat", "statistics", "string", "stringprep", "struct", + "subprocess", "sunau", "symbol", "symtable", "sys", + "sysconfig", "syslog", "tabnanny", "tarfile", "telnetlib", + "tempfile", "termios", "test", "textwrap", "threading", "time", + "timeit", "tkinter", "token", "tokenize", "trace", "traceback", + "tracemalloc", "tty", "turtle", "turtledemo", "types", + "typing", "unicodedata", "unittest", "urllib", "uu", "uuid", + "venv", "warnings", "wave", "weakref", "webbrowser", "winreg", + "winsound", "wsgiref", "xdrlib", "xml", "xmlrpc", "zipapp", + "zipfile", "zipimport", "zlib", "zoneinfo", + } + + ####################################################################### + ## Local imports + ## + ## adapted from: flake8-local-import v1.0.6 + ####################################################################### + + def __checkLocalImports(self): + """ + Private method to check local imports. + """ + from .LocalImportVisitor import LocalImportVisitor + + visitor = LocalImportVisitor(self.__args, self) + visitor.visit(copy.deepcopy(self.__tree)) + for violation in visitor.violations: + node = violation[0] + reason = violation[1] + self.__error(node.lineno - 1, node.col_offset, reason)