summaryrefslogtreecommitdiff
path: root/tests/unit
diff options
context:
space:
mode:
authorTimothy Crosley <timothy.crosley@gmail.com>2021-06-20 22:54:14 -0700
committerTimothy Crosley <timothy.crosley@gmail.com>2021-06-20 22:54:14 -0700
commitcd2e7636affca6bd7a3856cb6ed23b4f33eaac9f (patch)
tree6ed32f7516a7679b7f96b5b143ba7dbe0ad26807 /tests/unit
parent4cb72fa93fdcd78df00687de7cd91428ab0fdc93 (diff)
downloadisort-cd2e7636affca6bd7a3856cb6ed23b4f33eaac9f.tar.gz
Migrate mypy from setuf.cfg -> pyproject.toml. Starting running against tests in CI
Diffstat (limited to 'tests/unit')
-rw-r--r--tests/unit/profiles/test_black.py2
-rw-r--r--tests/unit/test_api.py2
-rw-r--r--tests/unit/test_deprecated_finders.py16
-rw-r--r--tests/unit/test_exceptions.py24
-rw-r--r--tests/unit/test_isort.py89
-rw-r--r--tests/unit/test_main.py22
-rw-r--r--tests/unit/test_regressions.py2
-rw-r--r--tests/unit/test_settings.py4
8 files changed, 86 insertions, 75 deletions
diff --git a/tests/unit/profiles/test_black.py b/tests/unit/profiles/test_black.py
index 0e54e706..7288d478 100644
--- a/tests/unit/profiles/test_black.py
+++ b/tests/unit/profiles/test_black.py
@@ -9,7 +9,7 @@ def black_format(code: str, is_pyi: bool = False, line_length: int = 88) -> str:
return black.format_file_contents(
code,
fast=True,
- mode=black.FileMode(
+ mode=black.FileMode( # type: ignore
is_pyi=is_pyi,
line_length=line_length,
),
diff --git a/tests/unit/test_api.py b/tests/unit/test_api.py
index bffa7fca..7fe73ab9 100644
--- a/tests/unit/test_api.py
+++ b/tests/unit/test_api.py
@@ -14,7 +14,7 @@ fixed_diff = "+import a\n import b\n-import a\n"
@pytest.fixture
-def imperfect(tmpdir) -> None:
+def imperfect(tmpdir):
imperfect_file = tmpdir.join("test_needs_changes.py")
imperfect_file.write_text(imperfect_content, "utf8")
return imperfect_file
diff --git a/tests/unit/test_deprecated_finders.py b/tests/unit/test_deprecated_finders.py
index bbd43596..3e3be56e 100644
--- a/tests/unit/test_deprecated_finders.py
+++ b/tests/unit/test_deprecated_finders.py
@@ -31,13 +31,13 @@ class TestFindersManager:
assert FindersManager(settings.DEFAULT_CONFIG)
class ExceptionOnInit(finders.BaseFinder):
- def __init__(*args, **kwargs):
+ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
raise ValueError("test")
with patch(
"isort.deprecated.finders.FindersManager._default_finders_classes",
- FindersManager._default_finders_classes + (ExceptionOnInit,),
+ FindersManager._default_finders_classes + (ExceptionOnInit,), # type: ignore
):
assert FindersManager(settings.Config(verbose=True))
@@ -59,14 +59,14 @@ class AbstractTestFinder:
@classmethod
def setup_class(cls):
- cls.instance = cls.kind(settings.DEFAULT_CONFIG)
+ cls.instance = cls.kind(settings.DEFAULT_CONFIG) # type: ignore
def test_create(self):
- assert self.kind(settings.DEFAULT_CONFIG)
+ assert self.kind(settings.DEFAULT_CONFIG) # type: ignore
def test_find(self):
- self.instance.find("isort")
- self.instance.find("")
+ self.instance.find("isort") # type: ignore
+ self.instance.find("") # type: ignore
class TestForcedSeparateFinder(AbstractTestFinder):
@@ -154,7 +154,7 @@ def test_requirements_finder(tmpdir) -> None:
assert finder.find("flask") is None # package not in reqs
assert finder.find("deal") == sections.THIRDPARTY # vcs
- assert len(finder.mapping) > 100
+ assert len(finder.mapping) > 100 # type: ignore
assert finder._normalize_name("deal") == "deal"
assert finder._normalize_name("Django") == "django" # lowercase
assert finder._normalize_name("django_haystack") == "haystack" # mapping
@@ -174,7 +174,7 @@ def test_pipfile_finder(tmpdir) -> None:
assert finder.find("flask") is None # package not in reqs
assert finder.find("deal") == sections.THIRDPARTY # vcs
- assert len(finder.mapping) > 100
+ assert len(finder.mapping) > 100 # type: ignore
assert finder._normalize_name("deal") == "deal"
assert finder._normalize_name("Django") == "django" # lowercase
assert finder._normalize_name("django_haystack") == "haystack" # mapping
diff --git a/tests/unit/test_exceptions.py b/tests/unit/test_exceptions.py
index 2cd17aa1..81610557 100644
--- a/tests/unit/test_exceptions.py
+++ b/tests/unit/test_exceptions.py
@@ -11,7 +11,7 @@ class TestISortError:
class TestExistingSyntaxErrors(TestISortError):
def setup_class(self):
- self.instance = exceptions.ExistingSyntaxErrors("file_path")
+ self.instance: exceptions.ExistingSyntaxErrors = exceptions.ExistingSyntaxErrors("file_path")
def test_variables(self):
assert self.instance.file_path == "file_path"
@@ -19,7 +19,7 @@ class TestExistingSyntaxErrors(TestISortError):
class TestIntroducedSyntaxErrors(TestISortError):
def setup_class(self):
- self.instance = exceptions.IntroducedSyntaxErrors("file_path")
+ self.instance: exceptions.IntroducedSyntaxErrors = exceptions.IntroducedSyntaxErrors("file_path")
def test_variables(self):
assert self.instance.file_path == "file_path"
@@ -27,7 +27,7 @@ class TestIntroducedSyntaxErrors(TestISortError):
class TestFileSkipped(TestISortError):
def setup_class(self):
- self.instance = exceptions.FileSkipped("message", "file_path")
+ self.instance: exceptions.FileSkipped = exceptions.FileSkipped("message", "file_path")
def test_variables(self):
assert self.instance.file_path == "file_path"
@@ -36,7 +36,7 @@ class TestFileSkipped(TestISortError):
class TestFileSkipComment(TestISortError):
def setup_class(self):
- self.instance = exceptions.FileSkipComment("file_path")
+ self.instance: exceptions.FileSkipComment = exceptions.FileSkipComment("file_path")
def test_variables(self):
assert self.instance.file_path == "file_path"
@@ -44,7 +44,7 @@ class TestFileSkipComment(TestISortError):
class TestFileSkipSetting(TestISortError):
def setup_class(self):
- self.instance = exceptions.FileSkipSetting("file_path")
+ self.instance: exceptions.FileSkipSetting = exceptions.FileSkipSetting("file_path")
def test_variables(self):
assert self.instance.file_path == "file_path"
@@ -52,7 +52,7 @@ class TestFileSkipSetting(TestISortError):
class TestProfileDoesNotExist(TestISortError):
def setup_class(self):
- self.instance = exceptions.ProfileDoesNotExist("profile")
+ self.instance: exceptions.ProfileDoesNotExist = exceptions.ProfileDoesNotExist("profile")
def test_variables(self):
assert self.instance.profile == "profile"
@@ -60,7 +60,7 @@ class TestProfileDoesNotExist(TestISortError):
class TestSortingFunctionDoesNotExist(TestISortError):
def setup_class(self):
- self.instance = exceptions.SortingFunctionDoesNotExist("round", ["square", "peg"])
+ self.instance: exceptions.SortingFunctionDoesNotExist = exceptions.SortingFunctionDoesNotExist("round", ["square", "peg"])
def test_variables(self):
assert self.instance.sort_order == "round"
@@ -69,7 +69,7 @@ class TestSortingFunctionDoesNotExist(TestISortError):
class TestLiteralParsingFailure(TestISortError):
def setup_class(self):
- self.instance = exceptions.LiteralParsingFailure("x = [", SyntaxError)
+ self.instance: exceptions.LiteralParsingFailure = exceptions.LiteralParsingFailure("x = [", SyntaxError)
def test_variables(self):
assert self.instance.code == "x = ["
@@ -78,7 +78,7 @@ class TestLiteralParsingFailure(TestISortError):
class TestLiteralSortTypeMismatch(TestISortError):
def setup_class(self):
- self.instance = exceptions.LiteralSortTypeMismatch(tuple, list)
+ self.instance: exceptions.LiteralSortTypeMismatch = exceptions.LiteralSortTypeMismatch(tuple, list)
def test_variables(self):
assert self.instance.kind == tuple
@@ -87,7 +87,7 @@ class TestLiteralSortTypeMismatch(TestISortError):
class TestAssignmentsFormatMismatch(TestISortError):
def setup_class(self):
- self.instance = exceptions.AssignmentsFormatMismatch("print x")
+ self.instance: exceptions.AssignmentsFormatMismatch = exceptions.AssignmentsFormatMismatch("print x")
def test_variables(self):
assert self.instance.code == "print x"
@@ -95,7 +95,7 @@ class TestAssignmentsFormatMismatch(TestISortError):
class TestUnsupportedSettings(TestISortError):
def setup_class(self):
- self.instance = 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": "/"}}
@@ -103,7 +103,7 @@ class TestUnsupportedSettings(TestISortError):
class TestUnsupportedEncoding(TestISortError):
def setup_class(self):
- self.instance = exceptions.UnsupportedEncoding("file.py")
+ self.instance: exceptions.UnsupportedEncoding = exceptions.UnsupportedEncoding("file.py")
def test_variables(self):
assert self.instance.filename == "file.py"
diff --git a/tests/unit/test_isort.py b/tests/unit/test_isort.py
index c90f40d2..63a59f9a 100644
--- a/tests/unit/test_isort.py
+++ b/tests/unit/test_isort.py
@@ -9,18 +9,24 @@ import subprocess
import sys
from io import StringIO
from tempfile import NamedTemporaryFile
-from typing import Any, Dict, Iterator, List, Set, Tuple
+from typing import Any, Dict, Iterator, List, Set, Tuple, TYPE_CHECKING
import py
import pytest
import toml
import isort
from isort import api, sections, files
-from isort.settings import WrapModes, Config
+from isort.settings import Config
+
from isort.utils import exists_case_sensitive
from isort.exceptions import FileSkipped, ExistingSyntaxErrors
from .utils import as_stream, UnreadableStream
+if TYPE_CHECKING:
+ WrapModes: Any
+else:
+ from isort.wrap_modes import WrapModes
+
TEST_DEFAULT_CONFIG = """
[*.{py,pyi}]
max_line_length = 120
@@ -225,7 +231,10 @@ def test_line_length() -> None:
)
with pytest.raises(ValueError):
test_output = isort.code(code=REALLY_LONG_IMPORT, line_length=80, wrap_length=99)
- test_output = isort.code(REALLY_LONG_IMPORT, line_length=100, wrap_length=99) == test_input
+ 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)
+""")
# Test Case described in issue #1015
test_output = isort.code(
@@ -1271,46 +1280,44 @@ import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
"""
- test_output = (
- 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"
- # )
- # test_output = isort.code(
- # 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"
- # )
- # test_second_run = isort.code(
- # code=test_output,
- # known_first_party=["myproject"],
- # import_heading_stdlib="Standard Library",
- # import_heading_firstparty="My Stuff",
- # )
- # assert test_second_run == test_output
+ test_input = (
+ "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",
+ )
+ 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"
+ )
+ test_second_run = isort.code(
+ code=test_output,
+ known_first_party=["myproject"],
+ import_heading_stdlib="Standard Library",
+ import_heading_firstparty="My Stuff",
+ )
+ assert test_second_run == test_output
test_input_lines_down = (
"# comment 1\n"
@@ -1420,9 +1427,9 @@ while True print 'Hello world'
)
# ensure atomic works with streams
- test_input = as_stream("from b import d, c\nfrom a import f, e\n")
+ test_stream_input = as_stream("from b import d, c\nfrom a import f, e\n")
test_output = UnreadableStream()
- isort.stream(test_input, test_output, atomic=True)
+ isort.stream(test_stream_input, test_output, atomic=True)
test_output.seek(0)
assert test_output.read() == "from a import e, f\nfrom b import c, d\n"
diff --git a/tests/unit/test_main.py b/tests/unit/test_main.py
index 207f19dc..e7697643 100644
--- a/tests/unit/test_main.py
+++ b/tests/unit/test_main.py
@@ -12,10 +12,14 @@ from isort import main
from isort._version import __version__
from isort.exceptions import InvalidSettingsPath
from isort.settings import DEFAULT_CONFIG, Config
-from isort.wrap_modes import WrapModes
from .utils import as_stream
from io import BytesIO, TextIOWrapper
+from typing import TYPE_CHECKING, Any
+if TYPE_CHECKING:
+ WrapModes: Any
+else:
+ from isort.wrap_modes import WrapModes
@given(
file_name=st.text(),
@@ -37,15 +41,15 @@ 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
+ assert main.sort_imports(str(tmp_file), DEFAULT_CONFIG, check=True).incorrectly_sorted # type: ignore
main.sort_imports(str(tmp_file), DEFAULT_CONFIG)
- assert not main.sort_imports(str(tmp_file), DEFAULT_CONFIG, check=True).incorrectly_sorted
+ assert not main.sort_imports(str(tmp_file), DEFAULT_CONFIG, check=True).incorrectly_sorted # type: ignore
skip_config = Config(skip=["file.py"])
- assert main.sort_imports(
+ assert main.sort_imports( # type: ignore
str(tmp_file), config=skip_config, check=True, disregard_skip=False
- ).skipped
- assert main.sort_imports(str(tmp_file), config=skip_config, disregard_skip=False).skipped
+ ).skippedg
+ assert main.sort_imports(str(tmp_file), config=skip_config, disregard_skip=False).skipped # type: ignore
def test_sort_imports_error_handling(tmpdir, mocker, capsys):
@@ -53,7 +57,7 @@ def test_sort_imports_error_handling(tmpdir, mocker, capsys):
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
+ main.sort_imports(str(tmp_file), DEFAULT_CONFIG, check=True).incorrectly_sorted # type: ignore
out, error = capsys.readouterr()
assert "Unrecoverable exception thrown when parsing" in error
@@ -345,7 +349,7 @@ import b
def test_isort_command():
"""Ensure ISortCommand got registered, otherwise setuptools error must have occurred"""
- assert main.ISortCommand
+ assert main.ISortCommand # type: ignore
def test_isort_filename_overrides(tmpdir, capsys):
@@ -1060,7 +1064,7 @@ def test_identify_imports_main(tmpdir, capsys):
len(out.split("\n")) == 2
-def test_gitignore(capsys: pytest.CaptureFixture, tmpdir: py.path.local):
+def test_gitignore(capsys, tmpdir: py.path.local):
import_content = """
import b
diff --git a/tests/unit/test_regressions.py b/tests/unit/test_regressions.py
index ccd3da22..a7933005 100644
--- a/tests/unit/test_regressions.py
+++ b/tests/unit/test_regressions.py
@@ -233,7 +233,7 @@ 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
+ assert isort.place_module("sre_parse") == isort.place_module("sre") == isort.settings.STDLIB # type: ignore
def test_add_imports_shouldnt_move_lower_comments_issue_1300():
diff --git a/tests/unit/test_settings.py b/tests/unit/test_settings.py
index 6ef8f70e..ad0f3ae4 100644
--- a/tests/unit/test_settings.py
+++ b/tests/unit/test_settings.py
@@ -93,11 +93,11 @@ class TestConfig:
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
+ assert Config(multi_line_output=6).multi_line_output == WrapModes.VERTICAL_GRID_GROUPED # type: ignore
def test_as_list():
- assert settings._as_list([" one "]) == ["one"]
+ assert settings._as_list([" one "]) == ["one"] # type: ignore
assert settings._as_list("one,two") == ["one", "two"]