Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -158,16 +158,6 @@ follow_imports = "silent"
packages = ["isort", "tests"]
exclude = "isort/_vendored|tests/unit/example_projects|tests/unit/example_crlf_file.py"

[[tool.mypy.overrides]]
module = "tests.*"
allow_untyped_defs = true
allow_incomplete_defs = true
allow_untyped_calls = true

[[tool.mypy.overrides]]
module = "importlib_metadata.*"
ignore_missing_imports = true

[tool.ruff]
target-version = "py310"
line-length = 100
Expand Down
25 changes: 16 additions & 9 deletions tests/benchmark/test_api.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,38 @@
from typing import Any
from collections.abc import Callable
from pathlib import Path
from typing import Protocol

import pytest

from isort import api


class BenchmarkFixture(Protocol):
def pedantic(self, target: Callable[[], object], *, iterations: int, rounds: int) -> None: ...


imperfect_content = "import b\nimport a\n"
fixed_content = "import a\nimport b\n"


@pytest.fixture
def imperfect(tmpdir) -> Any:
imperfect_file = tmpdir.join("test_needs_changes.py")
def imperfect(tmp_path: Path) -> Path:
imperfect_file = tmp_path / "test_needs_changes.py"
imperfect_file.write_text(imperfect_content, "utf8")
return imperfect_file


def test_sort_file(benchmark, imperfect) -> None:
def sort_file():
def test_sort_file(benchmark: BenchmarkFixture, imperfect: Path) -> None:
def sort_file() -> None:
api.sort_file(imperfect)

benchmark.pedantic(sort_file, iterations=10, rounds=100)
assert imperfect.read() == fixed_content
assert imperfect.read_text() == fixed_content


def test_sort_file_in_place(benchmark, imperfect) -> None:
def sort_file():
def test_sort_file_in_place(benchmark: BenchmarkFixture, imperfect: Path) -> None:
def sort_file() -> None:
api.sort_file(imperfect, overwrite_in_place=True)

benchmark.pedantic(sort_file, iterations=10, rounds=100)
assert imperfect.read() == fixed_content
assert imperfect.read_text() == fixed_content
4 changes: 2 additions & 2 deletions tests/integration/test_hypothesmith.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ast
from typing import get_type_hints
from typing import Any, get_type_hints

import hypothesis
import libcst
Expand All @@ -9,7 +9,7 @@
import isort


def _as_config(kw) -> isort.Config:
def _as_config(kw: dict[str, Any]) -> isort.Config:
if "wrap_length" in kw and "line_length" in kw:
kw["wrap_length"], kw["line_length"] = sorted([kw["wrap_length"], kw["line_length"]])
try:
Expand Down
65 changes: 33 additions & 32 deletions tests/integration/test_projects_using_isort.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,51 +18,52 @@
from isort.main import main


def git_clone(repository_url: str, directory: Path):
def git_clone(repository_url: str, directory: Path) -> None:
"""Clones the given repository into the given directory path"""
check_call(["git", "clone", "--depth", "1", repository_url, str(directory)])


def run_isort(arguments: Generator[str, None, None] | Sequence[str]):
def run_isort(arguments: Generator[str, None, None] | Sequence[str]) -> None:
"""Runs isort in diff and check mode with the given arguments"""
main(["--check-only", "--diff", *arguments])


def test_django(tmpdir):
git_clone("https://github.com/django/django.git", tmpdir)
def test_django(tmp_path: Path) -> None:
git_clone("https://github.com/django/django.git", tmp_path)
run_isort(
str(target_dir) for target_dir in (tmpdir / "django", tmpdir / "tests", tmpdir / "scripts")
str(target_dir)
for target_dir in (tmp_path / "django", tmp_path / "tests", tmp_path / "scripts")
)


def test_plone(tmpdir):
git_clone("https://github.com/plone/plone.app.multilingualindexes.git", tmpdir)
run_isort([str(tmpdir / "src"), "--skip", "languagefallback.py"])
def test_plone(tmp_path: Path) -> None:
git_clone("https://github.com/plone/plone.app.multilingualindexes.git", tmp_path)
run_isort([str(tmp_path / "src"), "--skip", "languagefallback.py"])


@pytest.mark.skip(
"Skip for now as #2295 introduce a breaking change. Can be re-enabled after pandas has updated."
)
def test_pandas(tmpdir):
git_clone("https://github.com/pandas-dev/pandas.git", tmpdir)
run_isort((str(tmpdir / "pandas"), "--skip", "__init__.py"))
def test_pandas(tmp_path: Path) -> None:
git_clone("https://github.com/pandas-dev/pandas.git", tmp_path)
run_isort((str(tmp_path / "pandas"), "--skip", "__init__.py"))


def test_habitat_lab(tmpdir):
git_clone("https://github.com/facebookresearch/habitat-lab.git", tmpdir)
run_isort([str(tmpdir)])
def test_habitat_lab(tmp_path: Path) -> None:
git_clone("https://github.com/facebookresearch/habitat-lab.git", tmp_path)
run_isort([str(tmp_path)])


def test_pylint(tmpdir):
git_clone("https://github.com/PyCQA/pylint.git", tmpdir)
run_isort([str(tmpdir), "--skip", "bad.py"])
def test_pylint(tmp_path: Path) -> None:
git_clone("https://github.com/PyCQA/pylint.git", tmp_path)
run_isort([str(tmp_path), "--skip", "bad.py"])


def test_hypothesis(tmpdir):
git_clone("https://github.com/HypothesisWorks/hypothesis.git", tmpdir)
def test_hypothesis(tmp_path: Path) -> None:
git_clone("https://github.com/HypothesisWorks/hypothesis.git", tmp_path)
run_isort(
(
str(tmpdir),
str(tmp_path),
"--skip",
"tests",
"--profile",
Expand All @@ -76,24 +77,24 @@ def test_hypothesis(tmpdir):
)


def test_pyramid(tmpdir):
git_clone("https://github.com/Pylons/pyramid.git", tmpdir)
def test_pyramid(tmp_path: Path) -> None:
git_clone("https://github.com/Pylons/pyramid.git", tmp_path)
run_isort(
str(target_dir)
for target_dir in (tmpdir / "src" / "pyramid", tmpdir / "tests", tmpdir / "setup.py")
for target_dir in (tmp_path / "src" / "pyramid", tmp_path / "tests", tmp_path / "setup.py")
)


def test_products_zopetree(tmpdir):
git_clone("https://github.com/jugmac00/Products.ZopeTree.git", tmpdir)
run_isort([str(tmpdir)])
def test_products_zopetree(tmp_path: Path) -> None:
git_clone("https://github.com/jugmac00/Products.ZopeTree.git", tmp_path)
run_isort([str(tmp_path)])


def test_dobby(tmpdir):
git_clone("https://github.com/rocketDuck/dobby.git", tmpdir)
run_isort([str(tmpdir / "tests"), str(tmpdir / "src")])
def test_dobby(tmp_path: Path) -> None:
git_clone("https://github.com/rocketDuck/dobby.git", tmp_path)
run_isort([str(tmp_path / "tests"), str(tmp_path / "src")])


def test_zope(tmpdir):
git_clone("https://github.com/zopefoundation/Zope.git", tmpdir)
run_isort([str(tmpdir), "--skip", "util.py"])
def test_zope(tmp_path: Path) -> None:
git_clone("https://github.com/zopefoundation/Zope.git", tmp_path)
run_isort([str(tmp_path), "--skip", "util.py"])
4 changes: 2 additions & 2 deletions tests/integration/test_setting_combinations.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from typing import get_type_hints
from typing import Any, get_type_hints

import hypothesis
from hypothesis import strategies as st

import isort


def _as_config(kw) -> isort.Config:
def _as_config(kw: dict[str, Any]) -> isort.Config:
kw["atomic"] = False
if "wrap_length" in kw and "line_length" in kw:
kw["wrap_length"], kw["line_length"] = sorted([kw["wrap_length"], kw["line_length"]])
Expand Down
10 changes: 5 additions & 5 deletions tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,25 @@


@pytest.fixture
def test_dir():
def test_dir() -> str:
return TEST_DIR


@pytest.fixture
def src_dir():
def src_dir() -> str:
return SRC_DIR


@pytest.fixture
def test_path():
def test_path() -> Path:
return Path(TEST_DIR).resolve()


@pytest.fixture
def src_path():
def src_path() -> Path:
return Path(SRC_DIR).resolve()


@pytest.fixture
def examples_path():
def examples_path() -> Path:
return Path(TEST_DIR).resolve() / "example_projects"
6 changes: 3 additions & 3 deletions tests/unit/profiles/test_attrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
attrs_isort_test = partial(isort_test, profile="attrs")


def test_attrs_code_snippet_one():
def test_attrs_code_snippet_one() -> None:
attrs_isort_test(
"""from __future__ import absolute_import, division, print_function

Expand Down Expand Up @@ -35,7 +35,7 @@ def test_attrs_code_snippet_one():
)


def test_attrs_code_snippet_two():
def test_attrs_code_snippet_two() -> None:
attrs_isort_test(
"""from __future__ import absolute_import, division, print_function

Expand Down Expand Up @@ -72,7 +72,7 @@ def test_attrs_code_snippet_two():
)


def test_attrs_code_snippet_three():
def test_attrs_code_snippet_three() -> None:
attrs_isort_test(
'''
"""
Expand Down
16 changes: 10 additions & 6 deletions tests/unit/profiles/test_black.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Any

import black
from black.report import NothingChanged

Expand All @@ -19,7 +21,9 @@ def black_format(code: str, is_pyi: bool = False, line_length: int = 88) -> str:
return code


def black_test(code: str, expected_output: str = "", *, is_pyi: bool = False, **config_kwargs):
def black_test(
code: str, expected_output: str = "", *, is_pyi: bool = False, **config_kwargs: Any
) -> None:
"""Tests that the given code:
- Behaves the same when formatted multiple times with isort.
- Agrees with black formatting.
Expand All @@ -44,7 +48,7 @@ def black_test(code: str, expected_output: str = "", *, is_pyi: bool = False, **
assert output == expected_output


def test_black_snippet_one():
def test_black_snippet_one() -> None:
"""Test consistent code formatting between isort and black for code snippet from black repository.
See: https://github.com/psf/black/blob/master/tests/test_black.py
"""
Expand Down Expand Up @@ -158,7 +162,7 @@ def test_black_snippet_one():
)


def test_black_snippet_two():
def test_black_snippet_two() -> None:
"""Test consistent code formatting between isort and black for code snippet from black repository.
See: https://github.com/psf/black/blob/master/tests/test_primer.py
"""
Expand Down Expand Up @@ -233,7 +237,7 @@ def test_black_snippet_two():
)


def test_black_snippet_three():
def test_black_snippet_three() -> None:
"""Test consistent code formatting between isort and black for code snippet from black repository.
See: https://github.com/psf/black/blob/master/src/black/__init__.py
"""
Expand Down Expand Up @@ -376,7 +380,7 @@ def test_black_snippet_three():
)


def test_black_pyi_file():
def test_black_pyi_file() -> None:
"""Test consistent code formatting between isort and black for `.pyi` files.

black only allows no more than two consecutive blank lines in a `.pyi` file.
Expand Down Expand Up @@ -445,7 +449,7 @@ def sub(a: np.ndarray, b: np.ndarray) -> np.ndarray: ...
)


def test_black_trailing_comma():
def test_black_trailing_comma() -> None:
black_test(
"from x import (a, b, c,)\n",
"""\
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/profiles/test_django.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
django_isort_test = partial(isort_test, profile="django", known_first_party=["django"])


def test_django_snippet_one():
def test_django_snippet_one() -> None:
django_isort_test(
"""import copy
import inspect
Expand Down Expand Up @@ -57,7 +57,7 @@ def __str__(self):
)


def test_django_snippet_two():
def test_django_snippet_two() -> None:
django_isort_test(
'''from django.utils.version import get_version

Expand Down Expand Up @@ -86,7 +86,7 @@ def setup(set_prefix=True):
)


def test_django_snippet_three():
def test_django_snippet_three() -> None:
django_isort_test(
"""import cgi
import codecs
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/profiles/test_google.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
google_isort_test = partial(isort_test, profile="google")


def test_google_code_snippet_shared_example():
def test_google_code_snippet_shared_example() -> None:
"""Tests snippet examples directly shared with the isort project.
See: https://github.com/PyCQA/isort/issues/1486.
"""
Expand All @@ -22,7 +22,7 @@ def test_google_code_snippet_shared_example():
)


def test_google_code_snippet_one():
def test_google_code_snippet_one() -> None:
google_isort_test(
'''# coding=utf-8
# Copyright 2018 Google LLC
Expand Down Expand Up @@ -232,7 +232,7 @@ def test_google_code_snippet_one():
)


def test_google_code_snippet_two():
def test_google_code_snippet_two() -> None:
google_isort_test(
"""#!/usr/bin/env python
# In[ ]:
Expand Down Expand Up @@ -316,7 +316,7 @@ def test_google_code_snippet_two():
)


def test_code_snippet_three():
def test_code_snippet_three() -> None:
google_isort_test(
'''# Copyright 2019 Google LLC
#
Expand Down
Loading
Loading