Пример #1
0
def test_types_redefined(linter):
    elif_test = osp.join(osp.dirname(osp.abspath(__file__)), "data", "bad_builtin.py")
    with fix_import_path([elif_test]):
        linter.check([elif_test])
    msgs = sorted(linter.reporter.messages, key=lambda item: item.line)
    assert len(msgs) == 2
    for msg, expected in zip(msgs, EXPECTED):
        assert msg.symbol == "bad-builtin"
        assert msg.msg == expected
Пример #2
0
def test_two_similar_args(fake_path, case):
    with tempdir() as chroot:
        create_files(['a/b/__init__.py', 'a/c/__init__.py'])
        expected = [join(chroot, 'a')] + ["."] + fake_path

        assert sys.path == fake_path
        with lint.fix_import_path(case):
            assert sys.path == expected
        assert sys.path == fake_path
Пример #3
0
def test_one_arg(fake_path, case):
    with tempdir() as chroot:
        create_files(["a/b/__init__.py"])
        expected = [join(chroot, "a")] + ["."] + fake_path

        assert sys.path == fake_path
        with lint.fix_import_path(case):
            assert sys.path == expected
        assert sys.path == fake_path
Пример #4
0
    def _get_project(module: str, name: Optional[str] = "No Name") -> Project:
        """Return an astroid project representation."""

        def _astroid_wrapper(func: Callable, modname: str) -> Module:
            return func(modname)

        with fix_import_path([module]):
            project = project_from_files([module], _astroid_wrapper, project_name=name)
        return project
Пример #5
0
def test_types_redefined(linter):
    elif_test = osp.join(osp.dirname(osp.abspath(__file__)), 'data',
                         'redefined.py')
    with fix_import_path([elif_test]):
        linter.check([elif_test])
    msgs = sorted(linter.reporter.messages, key=lambda item: item.line)
    assert len(msgs) == 9
    for msg, expected in zip(msgs, EXPECTED):
        assert msg.symbol == 'redefined-variable-type'
        assert msg.msg == expected
Пример #6
0
def test_types_redefined(linter):
    elif_test = osp.join(osp.dirname(osp.abspath(__file__)), 'data',
                         'bad_builtin.py')
    with fix_import_path([elif_test]):
        linter.check([elif_test])
    msgs = sorted(linter.reporter.messages, key=lambda item: item.line)
    assert len(msgs) == 2
    for msg, expected in zip(msgs, EXPECTED):
        assert msg.symbol == 'bad-builtin'
        assert msg.msg == expected
Пример #7
0
 def test_types_redefined(self):
     elif_test = osp.join(osp.dirname(osp.abspath(__file__)), 'data',
                          'bad_builtin.py')
     with fix_import_path([elif_test]):
         self._linter.check([elif_test])
     msgs = sorted(self._linter.reporter.messages, key=lambda item: item.line)
     self.assertEqual(len(msgs), 2)
     for msg, expected in zip(msgs, self.expected):
         self.assertEqual(msg.symbol, 'bad-builtin')
         self.assertEqual(msg.msg, expected)
Пример #8
0
 def test_types_redefined(self):
     elif_test = osp.join(osp.dirname(osp.abspath(__file__)), 'data',
                          'bad_builtin.py')
     with fix_import_path([elif_test]):
         self._linter.check([elif_test])
     msgs = sorted(self._linter.reporter.messages, key=lambda item: item.line)
     self.assertEqual(len(msgs), 2)
     for msg, expected in zip(msgs, self.expected):
         self.assertEqual(msg.symbol, 'bad-builtin')
         self.assertEqual(msg.msg, expected)
Пример #9
0
def py3(check):
    """Verify if a custom check or integration can run on python 3. CHECK
    can be an integration name or a valid path to a Python module or package folder.
    """

    root = get_root()
    if check == 'datadog_checks_base':
        path_to_module = os.path.join(root, check, 'datadog_checks', 'base')
    elif check in get_valid_checks() and check not in NOT_CHECKS:
        path_to_module = os.path.join(root, check, 'datadog_checks', check)
    else:
        path_to_module = check

    if not os.path.exists(path_to_module):
        abort("{} does not exist.".format(path_to_module))

    echo_info("Validating python3 compatibility of {}...".format(check))
    with closing(StringIO()) as out:
        linter = PyLinter(reporter=JSONReporter(output=out))
        linter.load_default_plugins()
        linter.python3_porting_mode()
        # Disable `no-absolute-import`, which checks for a behaviour that's already part of python 2.7
        # cf https://www.python.org/dev/peps/pep-0328/
        linter.disable("no-absolute-import")
        with fix_import_path([path_to_module]):
            linter.check(path_to_module)
            linter.generate_reports()
        results = json.loads(out.getvalue() or "{}")

    if results:
        echo_failure("Incompatibilities were found for {}:".format(check))
        current_path = None
        for problem in sorted(results, key=itemgetter("path")):
            # An issue found by pylint is a dict like
            # {
            #     "message": "Calling a dict.iter*() method",
            #     "obj": "OpenFilesCheck.check",
            #     "column": 27,
            #     "path": "/path/to/file.py",
            #     "line": 235,
            #     "message-id": "W1620",
            #     "type": "warning",
            #     "symbol": "dict-iter-method",
            #     "module": "file"
            # }
            path = problem["path"]
            if current_path is None or path != current_path:
                echo_info("File {}:".format(path))
            echo_failure("  Line {}, column {}: {}".format(
                problem["line"], problem["column"], problem["message"]))
            current_path = path
        abort()
    else:
        echo_success("{} is compatible with python3".format(check))
Пример #10
0
def test_more_args(fake_path, case):
    with tempdir() as chroot:
        create_files(['a/b/c/__init__.py', 'a/d/__init__.py', 'a/e/f.py'])
        expected = [
            join(chroot, suffix)
            for suffix in [sep.join(('a', 'b')), 'a', sep.join(('a', 'e'))]
        ] + ["."] + fake_path

        assert sys.path == fake_path
        with lint.fix_import_path(case):
            assert sys.path == expected
        assert sys.path == fake_path
Пример #11
0
    def test_two_similar_args(self):
        with tempdir() as chroot:
            create_files(["a/b/__init__.py", "a/c/__init__.py"])
            expected = [join(chroot, "a")] + self.fake

            cases = (["a/b", "a/c"], ["a/c/", "a/b/"], ["a/b/__init__.py", "a/c/__init__.py"], ["a", "a/c/__init__.py"])

            self.assertEqual(sys.path, self.fake)
            for case in cases:
                with lint.fix_import_path(case):
                    self.assertEqual(sys.path, expected)
                self.assertEqual(sys.path, self.fake)
Пример #12
0
    def check_pylint(self):
        """
        Check using pylint.
        """
        options = self.options.get('pylint', self.file_path).copy()
        if not options['enabled']:
            return
        del options['enabled']

        if not self._compiled_tree:
            # We failed to compile the tree.
            return

        from pylint.lint import PyLinter, fix_import_path
        from pylint.reporters import CollectingReporter

        linter = PyLinter()
        linter.load_default_plugins()
        linter.set_reporter(CollectingReporter())

        if options['py3k']:
            linter.python3_porting_mode()
        del options['py3k']

        rcfile = options.get('rcfile', None)
        del options['rcfile']

        if rcfile:
            linter.read_config_file(config_file=rcfile)
            linter.load_config_file()
        else:
            linter.load_configuration_from_config(options)

        # PyLint does its own import and parsing, so we only pass the file
        # name and the precompiled tree.
        with fix_import_path(self.file_path):
            linter.check(self.file_path)

        for message in linter.reporter.messages:
            self.message(
                message.line,
                '%s:%s %s' % (
                    message.msg_id,
                    message.symbol,
                    message.msg,
                    ),
                code=message.msg_id,
                icon='info',
                category='pylint',
                )
Пример #13
0
def test_more_args(fake_path, case):
    with tempdir() as chroot:
        create_files(["a/b/c/__init__.py", "a/d/__init__.py", "a/e/f.py"])
        expected = (
            [
                join(chroot, suffix)
                for suffix in [sep.join(("a", "b")), "a", sep.join(("a", "e"))]
            ]
            + ["."]
            + fake_path
        )

        assert sys.path == fake_path
        with lint.fix_import_path(case):
            assert sys.path == expected
        assert sys.path == fake_path
Пример #14
0
def validate_py3(path_to_module):
    """
    Run pylint python3 validation on the python module/package provided
    """
    with closing(StringIO()) as out:
        linter = PyLinter(reporter=JSONReporter(output=out))
        linter.load_default_plugins()
        linter.python3_porting_mode()
        # Disable `no-absolute-import`, which checks for a behaviour that's already part of python 2.7
        # cf https://www.python.org/dev/peps/pep-0328/
        linter.disable("no-absolute-import")
        with fix_import_path([path_to_module]):
            syspath = sys.path[:]
            try:
                # Remove site-packages from imports. It keeps the standard
                # library, but it prevents pylint from parsing potentially big
                # third party modules.
                sys.path = [p for p in syspath if 'site-packages' not in p]
                linter.check(path_to_module)
            finally:
                sys.path = syspath
            linter.generate_reports()
        raw_results = json.loads(out.getvalue() or "{}")

    results = []
    for problem in raw_results:
        # An issue found by pylint is a dict like
        # {
        #     "message": "Calling a dict.iter*() method",
        #     "obj": "OpenFilesCheck.check",
        #     "column": 27,
        #     "path": "/path/to/file.py",
        #     "line": 235,
        #     "message-id": "W1620",
        #     "type": "warning",
        #     "symbol": "dict-iter-method",co
        #     "module": "file"
        # }
        results.append({
            "message":
            "Line {}, column {}: {}".format(problem["line"], problem["column"],
                                            problem["message"]),
            "path":
            problem["path"],
        })

    return results
Пример #15
0
    def test_two_similar_args(self):
        with tempdir() as chroot:
            create_files(['a/b/__init__.py', 'a/c/__init__.py'])
            expected = [join(chroot, 'a')] + ["."] + self.fake

            cases = (
                ['a/b', 'a/c'],
                ['a/c/', 'a/b/'],
                ['a/b/__init__.py', 'a/c/__init__.py'],
                ['a', 'a/c/__init__.py'],
            )

            self.assertEqual(sys.path, self.fake)
            for case in cases:
                with lint.fix_import_path(case):
                    self.assertEqual(sys.path, expected)
                self.assertEqual(sys.path, self.fake)
Пример #16
0
    def test_more_args(self):
        with tempdir() as chroot:
            create_files(["a/b/c/__init__.py", "a/d/__init__.py", "a/e/f.py"])
            expected = [
                join(chroot, suffix) for suffix in [sep.join(("a", "b")), "a", sep.join(("a", "e"))]
            ] + self.fake

            cases = (
                ["a/b/c/__init__.py", "a/d/__init__.py", "a/e/f.py"],
                ["a/b/c", "a", "a/e"],
                ["a/b/c", "a", "a/b/c", "a/e", "a"],
            )

            self.assertEqual(sys.path, self.fake)
            for case in cases:
                with lint.fix_import_path(case):
                    self.assertEqual(sys.path, expected)
                self.assertEqual(sys.path, self.fake)
Пример #17
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()
    checkers.register(linter)

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

    return linter.msg_status
Пример #18
0
    def test_more_args(self):
        with tempdir() as chroot:
            create_files(['a/b/c/__init__.py', 'a/d/__init__.py', 'a/e/f.py'])
            expected = [
                join(chroot, suffix)
                for suffix in [sep.join(('a', 'b')), 'a', sep.join(('a', 'e'))]
            ] + ["."] + self.fake

            cases = (
                ['a/b/c/__init__.py', 'a/d/__init__.py', 'a/e/f.py'],
                ['a/b/c', 'a', 'a/e'],
                ['a/b/c', 'a', 'a/b/c', 'a/e', 'a'],
            )

            self.assertEqual(sys.path, self.fake)
            for case in cases:
                with lint.fix_import_path(case):
                    self.assertEqual(sys.path, expected)
                self.assertEqual(sys.path, self.fake)
Пример #19
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
Пример #20
0
def test_no_args(fake_path):
    with lint.fix_import_path([]):
        assert sys.path == fake_path
    assert sys.path == fake_path
Пример #21
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()
Пример #22
0
 def test_no_args(self):
     with lint.fix_import_path([]):
         self.assertEqual(sys.path, ["."] + self.fake)
     self.assertEqual(sys.path, self.fake)
Пример #23
0
def test_project_root_in_sys_path():
    """Test the context manager adds the project root directory to sys.path.
    This should happen when pyreverse is run from any directory
    """
    with fix_import_path([TEST_DATA_DIR]):
        assert sys.path == [PROJECT_ROOT_DIR]
Пример #24
0
def test_no_args(fake_path):
    with lint.fix_import_path([]):
        assert sys.path == ["."] + fake_path
    assert sys.path == fake_path
Пример #25
0
def test_no_args(fake_path: list[int]) -> None:
    with lint.fix_import_path([]):
        assert sys.path == fake_path
    assert sys.path == fake_path