Visitar URL original
Fully type check `tests` and modernize code · PyCQA/isort@0482160 · GitHub
Skip to content

Commit 0482160

Browse files
committed
Fully type check tests and modernize code
1 parent bbda474 commit 0482160

39 files changed

Lines changed: 869 additions & 841 deletions

‎pyproject.toml‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -158,16 +158,6 @@ follow_imports = "silent"
158158
packages = ["isort", "tests"]
159159
exclude = "isort/_vendored|tests/unit/example_projects|tests/unit/example_crlf_file.py"
160160

161-
[[tool.mypy.overrides]]
162-
module = "tests.*"
163-
allow_untyped_defs = true
164-
allow_incomplete_defs = true
165-
allow_untyped_calls = true
166-
167-
[[tool.mypy.overrides]]
168-
module = "importlib_metadata.*"
169-
ignore_missing_imports = true
170-
171161
[tool.ruff]
172162
target-version = "py310"
173163
line-length = 100

‎tests/benchmark/test_api.py‎

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,38 @@
1-
from typing import Any
1+
from collections.abc import Callable
2+
from pathlib import Path
3+
from typing import Protocol
24

35
import pytest
46

57
from isort import api
68

9+
10+
class BenchmarkFixture(Protocol):
11+
def pedantic(self, target: Callable[[], object], *, iterations: int, rounds: int) -> None: ...
12+
13+
714
imperfect_content = "import b\nimport a\n"
815
fixed_content = "import a\nimport b\n"
916

1017

1118
@pytest.fixture
12-
def imperfect(tmpdir) -> Any:
13-
imperfect_file = tmpdir.join("test_needs_changes.py")
19+
def imperfect(tmp_path: Path) -> Path:
20+
imperfect_file = tmp_path / "test_needs_changes.py"
1421
imperfect_file.write_text(imperfect_content, "utf8")
1522
return imperfect_file
1623

1724

18-
def test_sort_file(benchmark, imperfect) -> None:
19-
def sort_file():
25+
def test_sort_file(benchmark: BenchmarkFixture, imperfect: Path) -> None:
26+
def sort_file() -> None:
2027
api.sort_file(imperfect)
2128

2229
benchmark.pedantic(sort_file, iterations=10, rounds=100)
23-
assert imperfect.read() == fixed_content
30+
assert imperfect.read_text() == fixed_content
2431

2532

26-
def test_sort_file_in_place(benchmark, imperfect) -> None:
27-
def sort_file():
33+
def test_sort_file_in_place(benchmark: BenchmarkFixture, imperfect: Path) -> None:
34+
def sort_file() -> None:
2835
api.sort_file(imperfect, overwrite_in_place=True)
2936

3037
benchmark.pedantic(sort_file, iterations=10, rounds=100)
31-
assert imperfect.read() == fixed_content
38+
assert imperfect.read_text() == fixed_content

‎tests/integration/test_hypothesmith.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import ast
2-
from typing import get_type_hints
2+
from typing import Any, get_type_hints
33

44
import hypothesis
55
import libcst
@@ -9,7 +9,7 @@
99
import isort
1010

1111

12-
def _as_config(kw) -> isort.Config:
12+
def _as_config(kw: dict[str, Any]) -> isort.Config:
1313
if "wrap_length" in kw and "line_length" in kw:
1414
kw["wrap_length"], kw["line_length"] = sorted([kw["wrap_length"], kw["line_length"]])
1515
try:

‎tests/integration/test_projects_using_isort.py‎

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,51 +18,52 @@
1818
from isort.main import main
1919

2020

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

2525

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

3030

31-
def test_django(tmpdir):
32-
git_clone("https://github.com/django/django.git", tmpdir)
31+
def test_django(tmp_path: Path) -> None:
32+
git_clone("https://github.com/django/django.git", tmp_path)
3333
run_isort(
34-
str(target_dir) for target_dir in (tmpdir / "django", tmpdir / "tests", tmpdir / "scripts")
34+
str(target_dir)
35+
for target_dir in (tmp_path / "django", tmp_path / "tests", tmp_path / "scripts")
3536
)
3637

3738

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

4243

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

5051

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

5556

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

6061

61-
def test_hypothesis(tmpdir):
62-
git_clone("https://github.com/HypothesisWorks/hypothesis.git", tmpdir)
62+
def test_hypothesis(tmp_path: Path) -> None:
63+
git_clone("https://github.com/HypothesisWorks/hypothesis.git", tmp_path)
6364
run_isort(
6465
(
65-
str(tmpdir),
66+
str(tmp_path),
6667
"--skip",
6768
"tests",
6869
"--profile",
@@ -76,24 +77,24 @@ def test_hypothesis(tmpdir):
7677
)
7778

7879

79-
def test_pyramid(tmpdir):
80-
git_clone("https://github.com/Pylons/pyramid.git", tmpdir)
80+
def test_pyramid(tmp_path: Path) -> None:
81+
git_clone("https://github.com/Pylons/pyramid.git", tmp_path)
8182
run_isort(
8283
str(target_dir)
83-
for target_dir in (tmpdir / "src" / "pyramid", tmpdir / "tests", tmpdir / "setup.py")
84+
for target_dir in (tmp_path / "src" / "pyramid", tmp_path / "tests", tmp_path / "setup.py")
8485
)
8586

8687

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

9192

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

9697

97-
def test_zope(tmpdir):
98-
git_clone("https://github.com/zopefoundation/Zope.git", tmpdir)
99-
run_isort([str(tmpdir), "--skip", "util.py"])
98+
def test_zope(tmp_path: Path) -> None:
99+
git_clone("https://github.com/zopefoundation/Zope.git", tmp_path)
100+
run_isort([str(tmp_path), "--skip", "util.py"])

‎tests/integration/test_setting_combinations.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
from typing import get_type_hints
1+
from typing import Any, get_type_hints
22

33
import hypothesis
44
from hypothesis import strategies as st
55

66
import isort
77

88

9-
def _as_config(kw) -> isort.Config:
9+
def _as_config(kw: dict[str, Any]) -> isort.Config:
1010
kw["atomic"] = False
1111
if "wrap_length" in kw and "line_length" in kw:
1212
kw["wrap_length"], kw["line_length"] = sorted([kw["wrap_length"], kw["line_length"]])

‎tests/unit/conftest.py‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,25 @@
1010

1111

1212
@pytest.fixture
13-
def test_dir():
13+
def test_dir() -> str:
1414
return TEST_DIR
1515

1616

1717
@pytest.fixture
18-
def src_dir():
18+
def src_dir() -> str:
1919
return SRC_DIR
2020

2121

2222
@pytest.fixture
23-
def test_path():
23+
def test_path() -> Path:
2424
return Path(TEST_DIR).resolve()
2525

2626

2727
@pytest.fixture
28-
def src_path():
28+
def src_path() -> Path:
2929
return Path(SRC_DIR).resolve()
3030

3131

3232
@pytest.fixture
33-
def examples_path():
33+
def examples_path() -> Path:
3434
return Path(TEST_DIR).resolve() / "example_projects"

‎tests/unit/profiles/test_attrs.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
attrs_isort_test = partial(isort_test, profile="attrs")
66

77

8-
def test_attrs_code_snippet_one():
8+
def test_attrs_code_snippet_one() -> None:
99
attrs_isort_test(
1010
"""from __future__ import absolute_import, division, print_function
1111
@@ -35,7 +35,7 @@ def test_attrs_code_snippet_one():
3535
)
3636

3737

38-
def test_attrs_code_snippet_two():
38+
def test_attrs_code_snippet_two() -> None:
3939
attrs_isort_test(
4040
"""from __future__ import absolute_import, division, print_function
4141
@@ -72,7 +72,7 @@ def test_attrs_code_snippet_two():
7272
)
7373

7474

75-
def test_attrs_code_snippet_three():
75+
def test_attrs_code_snippet_three() -> None:
7676
attrs_isort_test(
7777
'''
7878
"""

‎tests/unit/profiles/test_black.py‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Any
2+
13
import black
24
from black.report import NothingChanged
35

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

2123

22-
def black_test(code: str, expected_output: str = "", *, is_pyi: bool = False, **config_kwargs):
24+
def black_test(
25+
code: str, expected_output: str = "", *, is_pyi: bool = False, **config_kwargs: Any
26+
) -> None:
2327
"""Tests that the given code:
2428
- Behaves the same when formatted multiple times with isort.
2529
- Agrees with black formatting.
@@ -44,7 +48,7 @@ def black_test(code: str, expected_output: str = "", *, is_pyi: bool = False, **
4448
assert output == expected_output
4549

4650

47-
def test_black_snippet_one():
51+
def test_black_snippet_one() -> None:
4852
"""Test consistent code formatting between isort and black for code snippet from black repository.
4953
See: https://github.com/psf/black/blob/master/tests/test_black.py
5054
"""
@@ -158,7 +162,7 @@ def test_black_snippet_one():
158162
)
159163

160164

161-
def test_black_snippet_two():
165+
def test_black_snippet_two() -> None:
162166
"""Test consistent code formatting between isort and black for code snippet from black repository.
163167
See: https://github.com/psf/black/blob/master/tests/test_primer.py
164168
"""
@@ -233,7 +237,7 @@ def test_black_snippet_two():
233237
)
234238

235239

236-
def test_black_snippet_three():
240+
def test_black_snippet_three() -> None:
237241
"""Test consistent code formatting between isort and black for code snippet from black repository.
238242
See: https://github.com/psf/black/blob/master/src/black/__init__.py
239243
"""
@@ -376,7 +380,7 @@ def test_black_snippet_three():
376380
)
377381

378382

379-
def test_black_pyi_file():
383+
def test_black_pyi_file() -> None:
380384
"""Test consistent code formatting between isort and black for `.pyi` files.
381385
382386
black only allows no more than two consecutive blank lines in a `.pyi` file.
@@ -445,7 +449,7 @@ def sub(a: np.ndarray, b: np.ndarray) -> np.ndarray: ...
445449
)
446450

447451

448-
def test_black_trailing_comma():
452+
def test_black_trailing_comma() -> None:
449453
black_test(
450454
"from x import (a, b, c,)\n",
451455
"""\

‎tests/unit/profiles/test_django.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
django_isort_test = partial(isort_test, profile="django", known_first_party=["django"])
66

77

8-
def test_django_snippet_one():
8+
def test_django_snippet_one() -> None:
99
django_isort_test(
1010
"""import copy
1111
import inspect
@@ -57,7 +57,7 @@ def __str__(self):
5757
)
5858

5959

60-
def test_django_snippet_two():
60+
def test_django_snippet_two() -> None:
6161
django_isort_test(
6262
'''from django.utils.version import get_version
6363
@@ -86,7 +86,7 @@ def setup(set_prefix=True):
8686
)
8787

8888

89-
def test_django_snippet_three():
89+
def test_django_snippet_three() -> None:
9090
django_isort_test(
9191
"""import cgi
9292
import codecs

‎tests/unit/profiles/test_google.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
google_isort_test = partial(isort_test, profile="google")
66

77

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

2424

25-
def test_google_code_snippet_one():
25+
def test_google_code_snippet_one() -> None:
2626
google_isort_test(
2727
'''# coding=utf-8
2828
# Copyright 2018 Google LLC
@@ -232,7 +232,7 @@ def test_google_code_snippet_one():
232232
)
233233

234234

235-
def test_google_code_snippet_two():
235+
def test_google_code_snippet_two() -> None:
236236
google_isort_test(
237237
"""#!/usr/bin/env python
238238
# In[ ]:
@@ -316,7 +316,7 @@ def test_google_code_snippet_two():
316316
)
317317

318318

319-
def test_code_snippet_three():
319+
def test_code_snippet_three() -> None:
320320
google_isort_test(
321321
'''# Copyright 2019 Google LLC
322322
#

0 commit comments

Comments
 (0)