Skip to content

Commit

Permalink
Address linting errors
Browse files Browse the repository at this point in the history
  • Loading branch information
timothycrosley committed Jun 21, 2021
1 parent 1705835 commit 9bf7b35
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 48 deletions.
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ toml = "^0.10.2"
pytest-benchmark = "^3.4.1"
types-pkg-resources = "^0.1.2"
pre-commit = "^2.13.0"
types-colorama = "^0.4.2"
types-toml = "^0.1.3"

[tool.poetry.scripts]
isort = "isort.main:main"
Expand Down
28 changes: 21 additions & 7 deletions tests/unit/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,19 @@ def test_init(self):

class TestExistingSyntaxErrors(TestISortError):
def setup_class(self):
self.instance: exceptions.ExistingSyntaxErrors = exceptions.ExistingSyntaxErrors("file_path")
self.instance: exceptions.ExistingSyntaxErrors = exceptions.ExistingSyntaxErrors(
"file_path"
)

def test_variables(self):
assert self.instance.file_path == "file_path"


class TestIntroducedSyntaxErrors(TestISortError):
def setup_class(self):
self.instance: exceptions.IntroducedSyntaxErrors = exceptions.IntroducedSyntaxErrors("file_path")
self.instance: exceptions.IntroducedSyntaxErrors = exceptions.IntroducedSyntaxErrors(
"file_path"
)

def test_variables(self):
assert self.instance.file_path == "file_path"
Expand Down Expand Up @@ -60,7 +64,9 @@ def test_variables(self):

class TestSortingFunctionDoesNotExist(TestISortError):
def setup_class(self):
self.instance: exceptions.SortingFunctionDoesNotExist = exceptions.SortingFunctionDoesNotExist("round", ["square", "peg"])
self.instance: exceptions.SortingFunctionDoesNotExist = (
exceptions.SortingFunctionDoesNotExist("round", ["square", "peg"])
)

def test_variables(self):
assert self.instance.sort_order == "round"
Expand All @@ -69,7 +75,9 @@ def test_variables(self):

class TestLiteralParsingFailure(TestISortError):
def setup_class(self):
self.instance: exceptions.LiteralParsingFailure = exceptions.LiteralParsingFailure("x = [", SyntaxError)
self.instance: exceptions.LiteralParsingFailure = exceptions.LiteralParsingFailure(
"x = [", SyntaxError
)

def test_variables(self):
assert self.instance.code == "x = ["
Expand All @@ -78,7 +86,9 @@ def test_variables(self):

class TestLiteralSortTypeMismatch(TestISortError):
def setup_class(self):
self.instance: exceptions.LiteralSortTypeMismatch = exceptions.LiteralSortTypeMismatch(tuple, list)
self.instance: exceptions.LiteralSortTypeMismatch = exceptions.LiteralSortTypeMismatch(
tuple, list
)

def test_variables(self):
assert self.instance.kind == tuple
Expand All @@ -87,15 +97,19 @@ def test_variables(self):

class TestAssignmentsFormatMismatch(TestISortError):
def setup_class(self):
self.instance: exceptions.AssignmentsFormatMismatch = exceptions.AssignmentsFormatMismatch("print x")
self.instance: exceptions.AssignmentsFormatMismatch = exceptions.AssignmentsFormatMismatch(
"print x"
)

def test_variables(self):
assert self.instance.code == "print x"


class TestUnsupportedSettings(TestISortError):
def setup_class(self):
self.instance: exceptions.UnsupportedSettings = exceptions.UnsupportedSettings({"apply": {"value": "true", "source": "/"}})
self.instance: exceptions.UnsupportedSettings = exceptions.UnsupportedSettings(
{"apply": {"value": "true", "source": "/"}}
)

def test_variables(self):
assert self.instance.unsupported_settings == {"apply": {"value": "true", "source": "/"}}
Expand Down
63 changes: 34 additions & 29 deletions tests/unit/test_isort.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import sys
from io import StringIO
from tempfile import NamedTemporaryFile
from typing import Any, Dict, Iterator, List, Set, Tuple, TYPE_CHECKING
from typing import Any, Dict, Iterator, List, Set, Tuple, TYPE_CHECKING

import py
import pytest
Expand Down Expand Up @@ -231,10 +231,13 @@ def test_line_length() -> None:
)
with pytest.raises(ValueError):
test_output = isort.code(code=REALLY_LONG_IMPORT, line_length=80, wrap_length=99)
assert isort.code(REALLY_LONG_IMPORT, line_length=100, wrap_length=99) == (
"""from third_party import (lib1, lib2, lib3, lib4, lib5, lib6, lib7, lib8, lib9, lib10, lib11, lib12,
assert (
isort.code(REALLY_LONG_IMPORT, line_length=100, wrap_length=99)
== """
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)
""")
""".lstrip()
)

# Test Case described in issue #1015
test_output = isort.code(
Expand Down Expand Up @@ -1280,42 +1283,44 @@ def test_force_single_line_imports_and_sort_within_sections() -> None:
import pandas as pd
from matplotlib import pyplot as plt
"""
assert isort.code(code=test_input, force_sort_within_sections=True, length_sort=True) == test_input
assert (
isort.code(code=test_input, force_sort_within_sections=True, length_sort=True) == test_input
)


def test_titled_imports() -> None:
"""Tests setting custom titled/commented import sections."""
test_input = (
"import sys\n"
"import unicodedata\n"
"import statistics\n"
"import os\n"
"import myproject.test\n"
"import django.settings"
"import sys\n"
"import unicodedata\n"
"import statistics\n"
"import os\n"
"import myproject.test\n"
"import django.settings"
)
test_output = isort.code(
code=test_input,
known_first_party=["myproject"],
import_heading_stdlib="Standard Library",
import_heading_firstparty="My Stuff",
code=test_input,
known_first_party=["myproject"],
import_heading_stdlib="Standard Library",
import_heading_firstparty="My Stuff",
)
assert test_output == (
"# Standard Library\n"
"import os\n"
"import statistics\n"
"import sys\n"
"import unicodedata\n"
"\n"
"import django.settings\n"
"\n"
"# My Stuff\n"
"import myproject.test\n"
"# Standard Library\n"
"import os\n"
"import statistics\n"
"import sys\n"
"import unicodedata\n"
"\n"
"import django.settings\n"
"\n"
"# My Stuff\n"
"import myproject.test\n"
)
test_second_run = isort.code(
code=test_output,
known_first_party=["myproject"],
import_heading_stdlib="Standard Library",
import_heading_firstparty="My Stuff",
code=test_output,
known_first_party=["myproject"],
import_heading_stdlib="Standard Library",
import_heading_firstparty="My Stuff",
)
assert test_second_run == test_output

Expand Down
16 changes: 6 additions & 10 deletions tests/unit/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
else:
from isort.wrap_modes import WrapModes


@given(
file_name=st.text(),
config=st.builds(Config),
Expand All @@ -41,23 +42,23 @@ def test_fuzz_sort_imports(file_name, config, check, ask_to_apply, write_to_stdo
def test_sort_imports(tmpdir):
tmp_file = tmpdir.join("file.py")
tmp_file.write("import os, sys\n")
assert main.sort_imports(str(tmp_file), DEFAULT_CONFIG, check=True).incorrectly_sorted # type: ignore
assert main.sort_imports(str(tmp_file), DEFAULT_CONFIG, check=True).incorrectly_sorted # type: ignore # noqa
main.sort_imports(str(tmp_file), DEFAULT_CONFIG)
assert not main.sort_imports(str(tmp_file), DEFAULT_CONFIG, check=True).incorrectly_sorted # type: ignore
assert not main.sort_imports(str(tmp_file), DEFAULT_CONFIG, check=True).incorrectly_sorted # type: ignore # noqa

skip_config = Config(skip=["file.py"])
assert main.sort_imports( # type: ignore
str(tmp_file), config=skip_config, check=True, disregard_skip=False
).skippedg
assert main.sort_imports(str(tmp_file), config=skip_config, disregard_skip=False).skipped # type: ignore
).skipped
assert main.sort_imports(str(tmp_file), config=skip_config, disregard_skip=False).skipped # type: ignore # noqa


def test_sort_imports_error_handling(tmpdir, mocker, capsys):
tmp_file = tmpdir.join("file.py")
tmp_file.write("import os, sys\n")
mocker.patch("isort.core.process").side_effect = IndexError("Example unhandled exception")
with pytest.raises(IndexError):
main.sort_imports(str(tmp_file), DEFAULT_CONFIG, check=True).incorrectly_sorted # type: ignore
main.sort_imports(str(tmp_file), DEFAULT_CONFIG, check=True).incorrectly_sorted # type: ignore # noqa

out, error = capsys.readouterr()
assert "Unrecoverable exception thrown when parsing" in error
Expand Down Expand Up @@ -347,11 +348,6 @@ def test_main(capsys, tmpdir):
main.main(["-sp", str(config_file), "-"], stdin=input_content)


def test_isort_command():
"""Ensure ISortCommand got registered, otherwise setuptools error must have occurred"""
assert main.ISortCommand # type: ignore


def test_isort_filename_overrides(tmpdir, capsys):
"""Tests isorts available approaches for overriding filename and extension based behavior"""
input_text = """
Expand Down
4 changes: 3 additions & 1 deletion tests/unit/test_regressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,9 @@ def test_ensure_sre_parse_is_identified_as_stdlib_issue_1304():
"""Ensure sre_parse is idenified as STDLIB.
See: https://github.com/pycqa/isort/issues/1304.
"""
assert isort.place_module("sre_parse") == isort.place_module("sre") == isort.settings.STDLIB # type: ignore
assert (
isort.place_module("sre_parse") == isort.place_module("sre") == isort.settings.STDLIB # type: ignore # noqa
)


def test_add_imports_shouldnt_move_lower_comments_issue_1300():
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def test_src_paths_are_combined_and_deduplicated(self):
assert Config(src_paths=src_paths * 2).src_paths == tuple(src_full_paths)

def test_deprecated_multi_line_output(self):
assert Config(multi_line_output=6).multi_line_output == WrapModes.VERTICAL_GRID_GROUPED # type: ignore
assert Config(multi_line_output=6).multi_line_output == WrapModes.VERTICAL_GRID_GROUPED # type: ignore # noqa


def test_as_list():
Expand Down

0 comments on commit 9bf7b35

Please sign in to comment.