Exemplo n.º 1
0
def _register_pyflakes_check():
    """Register the pyFlakes checker into PEP8 set of checks."""
    from flake8_isort import Flake8Isort
    from flake8_blind_except import check_blind_except

    # Resolving conflicts between pep8 and pyflakes.
    codes = {
        "UnusedImport": "F401",
        "ImportShadowedByLoopVar": "F402",
        "ImportStarUsed": "F403",
        "LateFutureImport": "F404",
        "Redefined": "F801",
        "RedefinedInListComp": "F812",
        "UndefinedName": "F821",
        "UndefinedExport": "F822",
        "UndefinedLocal": "F823",
        "DuplicateArgument": "F831",
        "UnusedVariable": "F841",
    }

    for name, obj in vars(pyflakes.messages).items():
        if name[0].isupper() and obj.message:
            obj.tpl = "{0} {1}".format(codes.get(name, "F999"), obj.message)

    pep8.register_check(_PyFlakesChecker, codes=['F'])
    # FIXME parser hack
    parser = pep8.get_parser('', '')
    Flake8Isort.add_options(parser)
    options, args = parser.parse_args([])
    # end of hack
    pep8.register_check(Flake8Isort, codes=['I'])
    pep8.register_check(check_blind_except, codes=['B90'])
Exemplo n.º 2
0
def _register_pyflakes_check():
    """Register the pyFlakes checker into PEP8 set of checks."""
    from flake8_isort import Flake8Isort
    from flake8_blind_except import check_blind_except

    # Resolving conflicts between pep8 and pyflakes.
    codes = {
        "UnusedImport": "F401",
        "ImportShadowedByLoopVar": "F402",
        "ImportStarUsed": "F403",
        "LateFutureImport": "F404",
        "Redefined": "F801",
        "RedefinedInListComp": "F812",
        "UndefinedName": "F821",
        "UndefinedExport": "F822",
        "UndefinedLocal": "F823",
        "DuplicateArgument": "F831",
        "UnusedVariable": "F841",
    }

    for name, obj in vars(pyflakes.messages).items():
        if name[0].isupper() and obj.message:
            obj.tpl = "{0} {1}".format(codes.get(name, "F999"), obj.message)

    pep8.register_check(_PyFlakesChecker, codes=['F'])
    # FIXME parser hack
    parser = pep8.get_parser('', '')
    Flake8Isort.add_options(parser)
    options, args = parser.parse_args([])
    # end of hack
    pep8.register_check(Flake8Isort, codes=['I'])
    pep8.register_check(check_blind_except, codes=['B90'])
Exemplo n.º 3
0
def test_flake8_isort(tmpdir, testcase, mode):
    """Test the code examples in files and directly from string"""
    with tmpdir.as_cwd():
        if 'config' in testcase:
            write_isort_cfg(tmpdir, testcase['config'])
        if mode == "file":
            (file_path, lines) = write_python_file(tmpdir, testcase['code'])
            checker = Flake8Isort(None, file_path, lines)
        elif mode == "code_string":
            checker = Flake8Isort(None, None, testcase['code'])
        else:
            raise RuntimeError("invalid mode")
        ret = list(checker.run())
        check_isort_ret(ret, testcase['ref'])
Exemplo n.º 4
0
 def test_isortcfg_skip_file(self):
     (file_path, lines) = self.write_python_file('skipped_file', )
     self.write_isort_cfg('skip=test.py')
     with OutputCapture():
         checker = Flake8Isort(None, file_path, lines)
         ret = list(checker.run())
         self.assertEqual(ret, [])
Exemplo n.º 5
0
 def test_sorted_incorrectly_multiple(self):
     file_path = self._given_a_file_in_test_dir(
         'from __future__ import division\n'
         'import os\n'
         'from sys import pid\n'
         'import threading\n'
         '\n'
         'import isort\n'
         '\n\n\n'
         'def func()\n',
         isort_config=''
     )
     with OutputCapture():
         checker = Flake8Isort(None, file_path)
         ret = list(checker.run())
         self.assertEqual(len(ret), 3)
         self.assertEqual(ret[0][0], 2)
         self.assertEqual(ret[0][1], 0)
         self.assertTrue(ret[0][2].startswith('I003 '))
         self.assertEqual(ret[1][0], 4)
         self.assertEqual(ret[1][1], 0)
         self.assertTrue(ret[1][2].startswith('I001 '))
         self.assertEqual(ret[2][0], 9)
         self.assertEqual(ret[2][1], 0)
         self.assertTrue(ret[2][2].startswith('I004 '))
Exemplo n.º 6
0
 def test_isortcfg_skip_file(self):
     file_path = self._given_a_file_in_test_dir('skipped_file',
                                                isort_config='skip=test.py')
     with OutputCapture():
         checker = Flake8Isort(None, file_path)
         ret = list(checker.run())
         self.assertEqual(ret, [])
Exemplo n.º 7
0
 def test_empty_file(self):
     (file_path, lines) = self._given_a_file_in_test_dir('\n\n',
                                                         isort_config='')
     with OutputCapture():
         checker = Flake8Isort(None, file_path, lines)
         ret = list(checker.run())
         self.assertEqual(ret, [])
Exemplo n.º 8
0
def test_isort_formatted_output(tmpdir):
    options = collections.namedtuple(
        'Options', [
            'no_isort_config',
            'isort_show_traceback',
            'stdin_display_name'
        ]
    )

    (file_path, lines) = write_python_file(
        tmpdir,
        'from __future__ import division\n'
        'import os\n'
        'from sys import pid\n',
    )

    diff = ' from __future__ import division\n+\n import os'

    checker = Flake8Isort(None, file_path, lines)
    checker.parse_options(options(None, True, 'stdin'))
    ret = list(checker.run())
    assert len(ret) == 1
    assert ret[0][0] == 2
    assert ret[0][1] == 0
    assert diff in ret[0][2]
Exemplo n.º 9
0
 def test_sorted_correctly_default(self):
     (file_path, lines) = self._given_a_file_in_test_dir(
         'import os\n'
         'from sys import path\n', isort_config='')
     with OutputCapture():
         checker = Flake8Isort(None, file_path, lines)
         ret = list(checker.run())
         self.assertEqual(ret, [])
Exemplo n.º 10
0
 def test_sorted_correctly_default(self):
     (file_path, lines) = self.write_python_file(
         'import os\n'
         'from sys import path\n', )
     with OutputCapture():
         checker = Flake8Isort(None, file_path, lines)
         ret = list(checker.run())
         self.assertEqual(ret, [])
Exemplo n.º 11
0
 def test_file_skipped_with_comment(self):
     # Note: files skipped in this way are not marked as
     # "skipped" by isort <= 4.2.15, so we handle them in a
     # different code path and test to ensure they also work.
     (file_path, lines) = self.write_python_file('# isort:skip_file', )
     with OutputCapture():
         checker = Flake8Isort(None, file_path, lines)
         ret = list(checker.run())
         self.assertEqual(ret, [])
Exemplo n.º 12
0
 def test_force_single_line_imports(self):
     (file_path, lines) = self._given_a_file_in_test_dir(
         'from plone.app.testing import applyProfile\n'
         'from plone.app.testing import FunctionalTesting\n',
         isort_config='force_alphabetical_sort=True\nforce_single_line=True'
     )
     with OutputCapture():
         checker = Flake8Isort(None, file_path, lines)
         ret = list(checker.run())
         self.assertEqual(ret, [])
Exemplo n.º 13
0
 def test_file_skipped_with_comment(self):
     # Note: files skipped in this way are not marked as
     # "skipped" by isort <= 4.2.15, so we handle them in a
     # different code path and test to ensure they also work.
     file_path = self._given_a_file_in_test_dir('# isort:skip_file',
                                                isort_config='')
     with OutputCapture():
         checker = Flake8Isort(None, file_path)
         ret = list(checker.run())
         self.assertEqual(ret, [])
Exemplo n.º 14
0
def test_isortcfg_not_found(tmpdir):
    (file_path, lines) = write_python_file(
        tmpdir,
        'from sys import pid, path'
    )
    checker = Flake8Isort(None, file_path, lines)
    checker.search_current = False
    checker.config_file = True
    ret = list(checker.run())
    check_isort_ret(ret, [(1, 0, 'I001 ')])
Exemplo n.º 15
0
 def test_wrapped_imports(self):
     (file_path, lines) = self._given_a_file_in_test_dir(
         'from deluge.common import (fdate, fpcnt, fpeer, fsize, fspeed,\n'
         '                           ftime, get_path_size, is_infohash,\n'
         '                           is_ip, is_magnet, is_url)\n',
         isort_config='wrap_length=65')
     with OutputCapture():
         checker = Flake8Isort(None, file_path, lines)
         ret = list(checker.run())
         self.assertEqual(ret, [])
Exemplo n.º 16
0
 def test_sorted_correctly_alpha(self):
     (file_path, lines) = self._given_a_file_in_test_dir(
         'from sys import path\n'
         '\n'
         'import os\n',
         isort_config='force_single_line=True\nforce_alphabetical_sort=True'
     )
     with OutputCapture():
         checker = Flake8Isort(None, file_path, lines)
         ret = list(checker.run())
         self.assertEqual(ret, [])
Exemplo n.º 17
0
def test_isortcfg_found(tmpdir):
    (file_path, lines) = write_python_file(
        tmpdir,
        'from sys import pid\n'
        'import threading',
    )
    write_isort_cfg(tmpdir, 'force_single_line=True')
    checker = Flake8Isort(None, file_path, lines)
    checker.config_file = True
    ret = list(checker.run())
    check_isort_ret(ret, [(2, 0, 'I001 ')])
Exemplo n.º 18
0
def test_if_config_file_is_used(tmpdir, method_to_write_config):
    (file_path, lines) = write_python_file(
        tmpdir,
        'import os\n'
        'from sys import path\n',
    )
    method_to_write_config(tmpdir, 'lines_between_types=1')

    checker = Flake8Isort(None, file_path, lines)
    ret = list(checker.run())
    check_isort_ret(ret, [(2, 0, 'I003 ')])
Exemplo n.º 19
0
 def test_sorted_incorrectly(self):
     (file_path, lines) = self._given_a_file_in_test_dir(
         'from sys import pid\n'
         'import threading',
         isort_config='force_single_line=True')
     with OutputCapture():
         checker = Flake8Isort(None, file_path, lines)
         ret = list(checker.run())
         self.assertEqual(len(ret), 1)
         self.assertEqual(ret[0][0], 2)
         self.assertEqual(ret[0][1], 0)
         self.assertTrue(ret[0][2].startswith('I001 '))
Exemplo n.º 20
0
 def test_imports_requires_blank_line(self):
     (file_path, lines) = self.write_python_file(
         'from __future__ import division\n'
         'import threading\n'
         'from sys import pid\n', )
     with OutputCapture():
         checker = Flake8Isort(None, file_path, lines)
         ret = list(checker.run())
         self.assertEqual(len(ret), 1)
         self.assertEqual(ret[0][0], 2)
         self.assertEqual(ret[0][1], 0)
         self.assertTrue(ret[0][2].startswith('I003 '))
Exemplo n.º 21
0
 def test_with_eof_blank_lines(self):
     """Pass with eof blank line as flake8 will flag them"""
     (file_path, lines) = self.write_python_file(
         'import os\n'
         'from sys import path\n'
         '\n'
         '\n'
         '   \n', )
     with OutputCapture():
         checker = Flake8Isort(None, file_path, lines)
         ret = list(checker.run())
         self.assertEqual(ret, [])
Exemplo n.º 22
0
 def test_isortcfg_not_found(self):
     (file_path,
      lines) = self.write_python_file('from sys import pid, path')
     with OutputCapture():
         checker = Flake8Isort(None, file_path, lines)
         checker.search_current = False
         checker.config_file = True
         ret = list(checker.run())
         self.assertEqual(len(ret), 1)
         self.assertEqual(ret[0][0], 1)
         self.assertEqual(ret[0][1], 0)
         self.assertTrue(ret[0][2].startswith('I001 '))
Exemplo n.º 23
0
 def test_imports_requires_blank_line(self):
     file_path = self._given_a_file_in_test_dir(
         'from __future__ import division\n'
         'import threading\n'
         'from sys import pid\n',
         isort_config='')
     with OutputCapture():
         checker = Flake8Isort(None, file_path)
         ret = list(checker.run())
         self.assertEqual(len(ret), 1)
         self.assertEqual(ret[0][0], 2)
         self.assertEqual(ret[0][1], 0)
         self.assertTrue(ret[0][2].startswith('I003 '))
Exemplo n.º 24
0
 def test_isortcfg_found(self):
     (file_path, lines) = self.write_python_file(
         'from sys import pid\n'
         'import threading', )
     self.write_isort_cfg('force_single_line=True')
     with OutputCapture():
         checker = Flake8Isort(None, file_path, lines)
         checker.config_file = True
         ret = list(checker.run())
         self.assertEqual(len(ret), 1)
         self.assertEqual(ret[0][0], 2)
         self.assertEqual(ret[0][1], 0)
         self.assertTrue(ret[0][2].startswith('I001 '))
Exemplo n.º 25
0
    def check_if_config_file_is_used(self, method_to_write_config):
        (file_path, lines) = self.write_python_file(
            'import os\n'
            'from sys import path\n', )
        method_to_write_config('lines_between_types=1')

        with OutputCapture():
            checker = Flake8Isort(None, file_path, lines)
            ret = list(checker.run())
            self.assertEqual(len(ret), 1)
            self.assertEqual(ret[0][0], 2)
            self.assertEqual(ret[0][1], 0)
            self.assertTrue(ret[0][2].startswith('I003 '))
Exemplo n.º 26
0
 def test_with_eof_blank_lines(self):
     """Pass with eof blank line as flake8 will flag them"""
     (file_path, lines) = self._given_a_file_in_test_dir(
         'import os\n'
         'from sys import path\n'
         '\n'
         '\n'
         '   \n',
         isort_config='')
     with OutputCapture():
         checker = Flake8Isort(None, file_path, lines)
         ret = list(checker.run())
         self.assertEqual(ret, [])
Exemplo n.º 27
0
 def test_missing_add_imports(self):
     (file_path, lines) = self.write_python_file('import os')
     self.write_isort_cfg(
         'add_imports=from __future__ import unicode_literals')
     with OutputCapture():
         checker = Flake8Isort(None, file_path, lines)
         ret = list(checker.run())
         self.assertEqual(len(ret), 2)
         self.assertEqual(ret[0][0], 1)
         self.assertEqual(ret[0][1], 0)
         self.assertTrue(ret[0][2].startswith('I005 '))
         self.assertEqual(ret[1][0], 1)
         self.assertEqual(ret[1][1], 0)
         self.assertTrue(ret[1][2].startswith('I003 '))
Exemplo n.º 28
0
    def test_isortcfg_found(self):
        # _given_a_file_in_test_dir already creates an .isort.cfg file
        (file_path, lines) = self._given_a_file_in_test_dir(
            'from sys import pid\n'
            'import threading',
            isort_config='force_single_line=True')

        with OutputCapture():
            checker = Flake8Isort(None, file_path, lines)
            checker.config_file = True
            ret = list(checker.run())
            self.assertEqual(len(ret), 1)
            self.assertEqual(ret[0][0], 2)
            self.assertEqual(ret[0][1], 0)
            self.assertTrue(ret[0][2].startswith('I001 '))
Exemplo n.º 29
0
    def test_isortcfg_not_found(self):
        (file_path, lines) = self._given_a_file_in_test_dir(
            'from sys import pid\n'
            'import threading',
            isort_config='force_single_line=True')
        # remove the .isort.cfg file
        isortcfg_path = file_path.split('/')[:-1]
        isortcfg_path = '{0}/.isort.cfg'.format('/'.join(isortcfg_path))
        os.remove(isortcfg_path)

        with OutputCapture():
            checker = Flake8Isort(None, file_path, lines, search_current=False)
            checker.config_file = True
            ret = list(checker.run())
            self.assertEqual(len(ret), 1)
            self.assertEqual(ret[0][0], 0)
            self.assertEqual(ret[0][1], 0)
            self.assertTrue(ret[0][2].startswith('I002 '))
Exemplo n.º 30
0
    def test_isort_uses_pyproject_toml_if_available(self):
        (file_path, lines) = self._given_a_file_in_test_dir(
            'import os\n'
            'from sys import path\n', isort_config='')

        pyproject_toml_path = os.path.join(
            os.path.dirname(file_path),
            'pyproject.toml',
        )
        with open(pyproject_toml_path, 'w') as f:
            f.write('[tool.isort]\nlines_between_types=1')

        with OutputCapture():
            checker = Flake8Isort(None, file_path, lines)
            ret = list(checker.run())
            self.assertEqual(len(ret), 1)
            self.assertEqual(ret[0][0], 2)
            self.assertEqual(ret[0][1], 0)
            self.assertTrue(ret[0][2].startswith('I003 '))
Exemplo n.º 31
0
    def test_isort_formatted_output(self):
        options = collections.namedtuple(
            'Options',
            ['no_isort_config', 'isort_show_traceback', 'stdin_display_name'])

        (file_path, lines) = self.write_python_file(
            'from __future__ import division\n'
            'import os\n'
            'from sys import pid\n', )

        diff = ' from __future__ import division\n+\n import os'

        with OutputCapture():
            checker = Flake8Isort(None, file_path, lines)
            checker.parse_options(options(None, True, 'stdin'))
            ret = list(checker.run())
            self.assertEqual(len(ret), 1)
            self.assertEqual(ret[0][0], 2)
            self.assertEqual(ret[0][1], 0)
            self.assertIn(diff, ret[0][2])