How to use the isort.isort.SortImports function in isort

To help you get started, we’ve selected a few isort examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github timothycrosley / isort / test_isort.py View on Github external
def test_long_single_line():
    """Test to ensure long single lines get handled correctly"""
    output = SortImports(file_contents="from ..views import ("
                                       " _a,"
                                       "_xxxxxx_xxxxxxx_xxxxxxxx_xxx_xxxxxxx as xxxxxx_xxxxxxx_xxxxxxxx_xxx_xxxxxxx)",
                         line_length=79).output
    for line in output.split('\n'):
        assert len(line) <= 79

    output = SortImports(file_contents="from ..views import ("
                                       " _a,"
                                       "_xxxxxx_xxxxxxx_xxxxxxxx_xxx_xxxxxxx as xxxxxx_xxxxxxx_xxxxxxxx_xxx_xxxxxxx)",
                         line_length=76, combine_as_imports=True).output
    for line in output.split('\n'):
        assert len(line) <= 79
github timothycrosley / isort / test_isort.py View on Github external
def test_correctly_placed_imports():
    """Test to ensure comments stay on correct placement after being sorted"""
    test_input = ("from a import b # comment for b\n"
                  "from a import c # comment for c\n")
    assert SortImports(file_contents=test_input, force_single_line=True).output == \
                      ("from a import b  # comment for b\n"
                       "from a import c  # comment for c\n")
    assert SortImports(file_contents=test_input).output == ("from a import b  # comment for b\n"
                                                            "from a import c  # comment for c\n")

    # Full example test from issue #143
    test_input = ("from itertools import chain\n"
                  "\n"
                  "from django.test import TestCase\n"
                  "from model_mommy import mommy\n"
                  "\n"
                  "from apps.clientman.commands.download_usage_rights import associate_right_for_item_product\n"
                  "from apps.clientman.commands.download_usage_rights import associate_right_for_item_product_d"
                  "efinition\n"
                  "from apps.clientman.commands.download_usage_rights import associate_right_for_item_product_d"
                  "efinition_platform\n"
github timothycrosley / isort / test_isort.py View on Github external
"    lib7, lib8, lib9, lib10, lib11,\n"
                                                 "    lib12, lib13, lib14, lib15, lib16,\n"
                                                 "    lib17, lib18, lib20, lib21, lib22\n"
                                                 ")\n")

    comment_output_vertical_grid_grouped = SortImports(file_contents=REALLY_LONG_IMPORT_WITH_COMMENT,
                                                       multi_line_output=WrapModes.VERTICAL_GRID_GROUPED,
                                                       line_length=40, indent="    ").output
    assert comment_output_vertical_grid_grouped == ("from third_party import (  # comment\n"
                                                    "    lib1, lib2, lib3, lib4, lib5, lib6,\n"
                                                    "    lib7, lib8, lib9, lib10, lib11,\n"
                                                    "    lib12, lib13, lib14, lib15, lib16,\n"
                                                    "    lib17, lib18, lib20, lib21, lib22\n"
                                                    ")\n")

    output_noqa = SortImports(file_contents=REALLY_LONG_IMPORT_WITH_COMMENT,
                              multi_line_output=WrapModes.NOQA).output
    assert output_noqa == "from third_party import lib1, lib2, lib3, lib4, lib5, lib6, lib7, lib8, lib9, lib10, lib11, lib12, lib13, lib14, lib15, lib16, lib17, lib18, lib20, lib21, lib22  # NOQA comment\n"  # NOQA
github timothycrosley / isort / test_isort.py View on Github external
def test_sort_within_section():
    '''Test to ensure its possible to force isort to sort within sections'''
    test_input = ('from Foob import ar\n'
                  'import foo\n'
                  'from foo import bar\n'
                  'from foo.bar import Quux, baz\n')
    test_output = SortImports(file_contents=test_input, force_sort_within_sections=True).output
    assert test_output == test_input

    test_input = ('import foo\n'
                  'from foo import bar\n'
                  'from foo.bar import baz\n'
                  'from foo.bar import Quux\n'
                  'from Foob import ar\n')
    test_output = SortImports(file_contents=test_input, force_sort_within_sections=True, order_by_type=False,
                              force_single_line=True).output
    assert test_output == test_input
github timothycrosley / isort / test_isort.py View on Github external
def test_happy_path():
    """Test the most basic use case, straight imports no code, simply not organized by category."""
    test_input = ("import sys\n"
                  "import os\n"
                  "import myproject.test\n"
                  "import django.settings")
    test_output = SortImports(file_contents=test_input, known_third_party=['django']).output
    assert test_output == ("import os\n"
                           "import sys\n"
github timothycrosley / isort / test_isort.py View on Github external
def test_custom_lines_after_import_section():
    """Test the case where the number of lines to output after imports has been explicitly set."""
    test_input = ("from a import b\n"
                  "foo = 'bar'\n")

    # default case is one space if not method or class after imports
    assert SortImports(file_contents=test_input).output == ("from a import b\n"
                                                            "\n"
                                                            "foo = 'bar'\n")

    # test again with a custom number of lines after the import section
    assert SortImports(file_contents=test_input, lines_after_imports=2).output == ("from a import b\n"
                                                                                   "\n"
github timothycrosley / isort / test_isort.py View on Github external
def test_force_grid_wrap_long():
    """Ensure that force grid wrap still happens with long line length"""
    test_input = (
      "from foo import lib6, lib7\n"
      "from bar import lib2\n"
      "from babar import something_that_is_kind_of_long"
    )
    test_output = SortImports(
      file_contents=test_input,
      force_grid_wrap=2,
      multi_line_output=WrapModes.VERTICAL_HANGING_INDENT,
      line_length=9999,
      ).output
    assert test_output == """from babar import something_that_is_kind_of_long
from bar import lib2
github timothycrosley / isort / test_isort.py View on Github external
def test_same_line_statements():
    """Ensure isort correctly handles the case where a single line contains multiple statements including an import"""
    test_input = ("import pdb; import nose\n")
    assert SortImports(file_contents=test_input).output == ("import pdb\n"
                                                            "\n"
                                                            "import nose\n")

    test_input = ("import pdb; pdb.set_trace()\n"
                  "import nose; nose.run()\n")
    assert SortImports(file_contents=test_input).output == test_input
github timothycrosley / isort / test_isort.py View on Github external
def test_other_file_encodings():
    """Test to ensure file encoding is respected"""
    try:
        tmp_dir = tempfile.mkdtemp()
        for encoding in ('latin1', 'utf8'):
            tmp_fname = os.path.join(tmp_dir, 'test_{0}.py'.format(encoding))
            with codecs.open(tmp_fname, mode='w', encoding=encoding) as f:
                file_contents = "# coding: {0}\n\ns = u'ã'\n".format(encoding)
                f.write(file_contents)
        assert SortImports(file_path=tmp_fname).output == file_contents
    finally:
        shutil.rmtree(tmp_dir, ignore_errors=True)
github timothycrosley / isort / test_isort.py View on Github external
def test_function_with_docstring():
    """Test to ensure isort can correctly sort imports when the first found content is a function with a docstring"""
    add_imports = ['from __future__ import unicode_literals']
    test_input = ('def foo():\n'
                  '    """ Single line triple quoted doctring """\n'
                  '    pass\n')
    expected_output = ('from __future__ import unicode_literals\n'
                       '\n'
                       '\n'
                       'def foo():\n'
                       '    """ Single line triple quoted doctring """\n'
                       '    pass\n')
    assert SortImports(file_contents=test_input, add_imports=add_imports).output == expected_output