Example #1
0
def _init_pylint():

    from pylint import lint, checkers
    import re

    class VimReporter(object):
        def __init__(self):
            self.errors = []

        def add_message(self, msg_id, location, msg):
            _, _, line, col = location[1:]
            self.errors.append(
                dict(lnum=line,
                     col=col,
                     text="%s %s" % (msg_id, msg),
                     type=msg_id[0]))

    PYLINT['lint'] = lint.PyLinter()
    PYLINT['re'] = re.compile(
        '^(?:.:)?[^:]+:(\d+): \[([EWRCI]+)[^\]]*\] (.*)$')

    checkers.initialize(PYLINT['lint'])
    PYLINT['lint'].load_file_configuration(vim.eval("g:pymode_lint_config"))
    PYLINT['lint'].set_option("output-format", "parseable")
    PYLINT['lint'].set_option("include-ids", 1)
    PYLINT['lint'].set_option("reports", 0)
    PYLINT['lint'].reporter = VimReporter()
Example #2
0
def test_disable_global_option_end_of_line():
    """
    Test for issue with disabling tokenizer messages
    that extend beyond the scope of the ast tokens
    """
    file_ = tempfile.NamedTemporaryFile("w", delete=False)
    with file_:
        file_.write(
            """
mylist = [
    None
        ]
    """
        )
    try:
        linter = lint.PyLinter()
        checker = FormatChecker(linter)
        linter.register_checker(checker)
        args = linter.load_command_line_configuration(
            [file_.name, "-d", "bad-continuation"]
        )
        myreporter = reporters.CollectingReporter()
        linter.set_reporter(myreporter)
        linter.check(args)
        assert not myreporter.messages
    finally:
        os.remove(file_.name)
Example #3
0
    def run_pylint(self, path):
        with warnings.catch_warnings():
            # suppress this warnings from output
            warnings.filterwarnings(
                "ignore",
                category=PendingDeprecationWarning,
            )
            warnings.filterwarnings(
                "ignore",
                category=DeprecationWarning,
            )
            warnings.filterwarnings(
                "ignore",
                category=ImportWarning,
            )

            linter = lint.PyLinter()
            # Register standard checkers.
            linter.load_default_plugins()
            linter.set_reporter(reporters.CollectingReporter())
            # we can simply use linter.error_mode(),
            # but I prefer to filter errors later.
            linter.global_set_option("ignored-modules", self.ignored_modules)
            linter.global_set_option("ignored-classes", self.ignored_classes)
            linter.global_set_option("disable", self.disable_ids)
            linter.check(path)

            return linter.reporter.messages
Example #4
0
def linter():
    test_reporter = testutils.TestReporter()
    linter = lint.PyLinter()
    linter.set_reporter(test_reporter)
    linter.disable('I')
    linter.config.persistent = 0
    checkers.initialize(linter)
    return linter
Example #5
0
def check(module_name):
    """Check a module for errors, printing a report.

    The name of the module should be passed in as a string,
    without a file extension (.py).
    """
    spec = importlib.util.find_spec(module_name)
    reporter = PyTAReporter()
    linter = lint.PyLinter(reporter=reporter)
    linter.load_default_plugins()
    linter.load_plugin_modules(['checkers'])
    linter.check([spec.origin])
    reporter.print_message_ids()
Example #6
0
 def __init__(self, test_file):
     test_reporter = FunctionalTestReporter()
     self._linter = lint.PyLinter()
     self._linter.set_reporter(test_reporter)
     self._linter.config.persistent = 0
     checkers.initialize(self._linter)
     self._linter.disable('I')
     try:
         self._linter.read_config_file(test_file.option_file)
         self._linter.load_config_file()
     except NoFileError:
         pass
     self._test_file = test_file
Example #7
0
 def __init__(self, test_file):
     super(LintModuleTest, self).__init__('_runTest')
     test_reporter = TestReporter()
     self._linter = lint.PyLinter()
     self._linter.set_reporter(test_reporter)
     self._linter.config.persistent = 0
     checkers.initialize(self._linter)
     self._linter.disable('I')
     try:
         self._linter.load_file_configuration(test_file.option_file)
     except NoFileError:
         pass
     self._test_file = test_file
def _init_linter():
    global _linter
    global _walker
    global _token_checkers
    global _raw_checkers
    global _reporter
    _linter = lint.PyLinter(reporter=_reporter)
    _linter.load_default_plugins()
    _walker = utils.PyLintASTWalker(_linter)
    checkers = _linter.prepare_checkers()
    _token_checkers = [
        c for c in checkers
        if implements(c, ITokenChecker) and c is not _linter
    ]
    _raw_checkers = [c for c in checkers if implements(c, IRawChecker)]
    for checker in checkers:
        checker.open()
        if implements(checker, IAstroidChecker):
            _walker.add_checker(checker)
Example #9
0
def main():
    arg_parser = ArgumentParser(
        description='Simple extension for pylint to check django projects for '
        'common mistakes.')
    arg_parser.add_argument('targets',
                            metavar='TARGET',
                            nargs='+',
                            help='python package or module')

    args = arg_parser.parse_args()

    linter = lint.PyLinter()
    reporters.initialize(linter)
    linter._load_reporter()
    register(linter)

    with lint.fix_import_path(args.targets):
        linter.check(args.targets)

    return linter.msg_status
Example #10
0
def test_disable_global_option_end_of_line() -> None:
    """Test for issue with disabling tokenizer messages
    that extend beyond the scope of the ast tokens
    """
    file_ = tempfile.NamedTemporaryFile("w", delete=False)
    with file_:
        file_.write("""
1
    """)
    try:
        linter = lint.PyLinter()
        checker = BasicChecker(linter)
        linter.register_checker(checker)
        args = linter._arguments_manager._parse_command_line_configuration(
            [file_.name, "-d", "pointless-statement"])
        myreporter = reporters.CollectingReporter()
        linter.set_reporter(myreporter)
        linter.check(args)
        assert not myreporter.messages
    finally:
        os.remove(file_.name)
Example #11
0
def run(argv, show_reports=False):
    linter = lint.PyLinter()
    checkers.initialize(linter)
    AstCheckers.register(linter)
    for warning in DISABLED_WARNINGS:
        linter.disable(warning)
    linter.set_option('ignore', ['local_settings.py', 'migrations'])
    linter.set_option('include-ids', True)
    if not show_reports:
        linter.set_option('reports', False)
    if not argv:
        # django lint uses deprecated style of pylint warning and we are not
        # interested in seeing warnings about this
        with warnings.catch_warnings():
            targets = [x for x in os.listdir('.')
               if (os.path.isdir(x) and
                   os.path.exists('%s/__init__.py' % (x,)))]
            warnings.simplefilter('ignore')
            linter.check(targets)
    else:
        files = linter.load_command_line_configuration(argv)
        linter.check(files)
Example #12
0
    def built_in_check(self, view, code, filename):
        linter = lint.PyLinter()
        checkers.initialize(linter)

        # Disable some errors.
        linter.load_command_line_configuration([
            '--module-rgx=.*',  # don't check the module name
            '--reports=n',  # remove tables
            '--persistent=n',  # don't save the old score (no sense for temp)
        ])

        temp = tempfile.NamedTemporaryFile(suffix='.py')
        temp.write(code)
        temp.flush()

        output_buffer = StringIO()
        linter.reporter.set_output(output_buffer)
        linter.check(temp.name)
        report = output_buffer.getvalue().replace(temp.name, 'line ')

        output_buffer.close()
        temp.close()

        return report
Example #13
0
        if module_name.endswith('.py'):
            module_name = module_name[:-3]

        spec = importlib.util.find_spec(module_name)

    if spec is None:
        print("The Module '{}' could not be found. ".format(module_name))
        return

    # Clear the astroid cache of this module (allows for on-the-fly changes
    # to be detected in consecutive runs in the interpreter).
    if spec.name in MANAGER.astroid_cache:
        del MANAGER.astroid_cache[spec.name]

    current_reporter = reporter(number_of_messages)
    linter = lint.PyLinter(reporter=current_reporter)
    linter.load_default_plugins()
    linter.load_plugin_modules(['python_ta/checkers/forbidden_import_checker',
                                'python_ta/checkers/global_variables_checker',
                                'python_ta/checkers/dynamic_execution_checker',
                                'python_ta/checkers/IO_Function_checker',
                                # TODO: Fix this test
                                #'python_ta/checkers/invalid_range_index_checker',
                                'python_ta/checkers/assigning_to_self_checker',
                                'python_ta/checkers/always_returning_checker'])

    if pep8:
=======
        # If available, use config file at directory of the file being linted.
        pylintrc_location = None
        if file_linted:
Example #14
0
 def test_register(self):
     pylinter_instance = lint.PyLinter()
     pylint_extensions.register(pylinter_instance)
Example #15
0
    __implements__ = interfaces.IAstroidChecker
    msgs = {
        'R9991': (
            "Consider Using %s.extend(%s)",
            "consider-using-extend",
            "Consider using list.extend instead of '+=' "
            "will allow you to use",
        ),
    }

    def visit_augassign(self, node):
        try:
            for inferred in node.target.infer():
                if inferred.qname() == 'builtins.list':
                    args = (node.target.name, node.value.as_string())
                    self.add_message('consider-using-extend',
                                     node=node,
                                     args=args)
        except astroid.InferenceError:
            pass


linter = lint.PyLinter()
linter.register_checker(MyChecker(linter))
args = linter.load_command_line_configuration()
linter.set_reporter(text.TextReporter())
linter.disable('bad-option-value')
with lint.fix_import_path(args):
    linter.check(args)
linter.generate_reports()
Example #16
0
def main():
    usage = """ %prog [options] target

    Django Lint is a tool that statically analyses Django projects and
    applications, checking for programming errors and bad code smells. For
    example, it reports nullable "CharField" fields, as well as reporting for
    unspecified options in settings.py.

    The `target` argument is mandatory and can specify either a directory
    containing a Django project, a single application or a single file.
    """.rstrip()

    parser = OptionParser(usage=usage)
    parser.add_option(
        '-r',
        '--reports',
        dest='report',
        action='store_true',
        default=False,
        help='generate report',
    )
    parser.add_option(
        '-p',
        '--pylint',
        dest='pylint',
        action='store_true',
        default=False,
        help='run normal PyLint checks',
    )
    parser.add_option(
        '-e',
        '--errors',
        dest='errors',
        action='store_true',
        default=False,
        help='only show errors',
    )
    parser.add_option(
        '-f',
        '--format',
        dest='outputformat',
        metavar='OUTPUT',
        default='text',
        help='Set the output format. Available formats are text,'
        'parseable, colorized, msvs (visual studio) and html',
    )

    options, args = parser.parse_args()

    try:
        args[0]
    except IndexError:
        args = ['.']

    targets = [os.path.abspath(arg) for arg in args]

    for target in targets:
        if not os.path.exists(target):
            try:
                # Is target a module?
                x = __import__(args[0], locals(), globals(), [], -1)
                target = sys.modules[args[0]].__path__[0]
            except:
                pass

        if not os.path.exists(target):
            raise parser.error(
                "The specified target (%r) does not exist" \
                    % target
            )

        path = target
        while True:
            flag = False
            for django_file in ('manage.py', 'models.py', 'urls.py'):
                if os.path.exists(os.path.join(path, django_file)):
                    sys.path.insert(0, os.path.dirname(path))
                    flag = True
                    break
            if flag:
                break

            path = os.path.dirname(path)

            if path == '/':
                raise parser.error(
                    "The specified target (%r) does not appear to be part of a " \
                    "Django application" % target
                )

    try:
        import django
    except ImportError:
        print >>sys.stderr, "E: Cannot import `django' module, exiting.."
        return 1

    linter = lint.PyLinter()
    linter.set_option('reports', options.report)
    linter.set_option('output-format', options.outputformat)

    if options.pylint:
        checkers.initialize(linter)
        for msg in ('C0111', 'C0301'):
            linter.disable(msg)

    AstCheckers.register(linter)

    if options.errors:
        linter.set_option('disable-msg-cat', 'WCRI')
        linter.set_option('reports', False)
        linter.set_option('persistent', False)

    linter.check(targets)

    return linter.msg_status
Example #17
0
def _check(module_name='',
           reporter=ColorReporter,
           number_of_messages=5,
           level='all',
           local_config_file='',
           pep8=False):
    """Check a module for problems, printing a report.

    <level> is used to specify which checks should be made.

    The name of the module should be the name of a module,
    or the path to a Python file.
    """
    if module_name == '':
        m = sys.modules['__main__']
        spec = importlib.util.spec_from_file_location(m.__name__, m.__file__)
    else:
        # Check if `module_name` is not the type str, raise error.
        if not isinstance(module_name, str):
            print("The Module '{}' has an invalid name. Module name must be "
                  "type str.".format(module_name))
            return
        module_name = module_name.replace(os.path.sep, '.')

        # Detect if the extension .py is added, and if it is, remove it.
        if module_name.endswith('.py'):
            module_name = module_name[:-3]

        spec = importlib.util.find_spec(module_name)

    if spec is None:
        print("The Module '{}' could not be found. ".format(module_name))
        return

    # Clear the astroid cache of this module (allows for on-the-fly changes
    # to be detected in consecutive runs in the interpreter).
    if spec.name in MANAGER.astroid_cache:
        del MANAGER.astroid_cache[spec.name]

    current_reporter = reporter(number_of_messages)
    linter = lint.PyLinter(reporter=current_reporter)
    linter.load_default_plugins()
    linter.load_plugin_modules([
        'python_ta/checkers/forbidden_import_checker',
        'python_ta/checkers/global_variables_checker',
        'python_ta/checkers/dynamic_execution_checker',
        'python_ta/checkers/IO_Function_checker',
        # TODO: Fix this test
        #'python_ta/checkers/invalid_range_index_checker',
        'python_ta/checkers/assigning_to_self_checker',
        'python_ta/checkers/always_returning_checker'
    ])

    if pep8:
        linter.load_plugin_modules(['python_ta/checkers/pycodestyle_checker'])

    if local_config_file != '':
        linter.read_config_file(local_config_file)
    else:
        linter.read_config_file(
            os.path.join(os.path.dirname(__file__), '.pylintrc'))
    linter.load_config_file()

    # Monkeypatch pylint
    patch_all()

    # Make sure the program doesn't crash for students.
    # Could use some improvement for better logging and error reporting.
    try:
        # Check for inline "pylint:" comment, which may indicate a student
        # trying to disable a check.
        # TODO: Put this into a helper function.
        with tokenize.open(spec.origin) as f:
            for (tok_type, content, _, _,
                 _) in tokenize.generate_tokens(f.readline):
                if tok_type != tokenize.COMMENT:
                    continue
                match = pylint.utils.OPTION_RGX.search(content)
                if match is None:
                    continue
                else:
                    print(
                        'ERROR: string "pylint:" found in comment. No checks will be run.'
                    )
                    return
    except IndentationError as e:
        print('ERROR: python_ta could not check your code due to an ' +
              'indentation error at line {}'.format(e.lineno))
        return
    except tokenize.TokenError as e:
        print('ERROR: python_ta could not check your code due to a ' +
              'syntax error in your file')
        return

    try:
        linter.check([spec.origin])
        current_reporter.print_messages(level)

    except Exception as e:
        print(
            'Unexpected error encountered - please report this to [email protected]!'
        )
        print(e)