Skip to content

Commit

Permalink
Test black compatibility for .pyi type stub files
Browse files Browse the repository at this point in the history
  • Loading branch information
XuehaiPan committed Dec 10, 2022
1 parent eed5d05 commit 6c7801c
Showing 1 changed file with 42 additions and 5 deletions.
47 changes: 42 additions & 5 deletions tests/unit/profiles/test_black.py
Expand Up @@ -19,21 +19,26 @@ def black_format(code: str, is_pyi: bool = False, line_length: int = 88) -> str:
return code


def black_test(code: str, expected_output: str = ""):
def black_test(code: str, expected_output: str = "", *, is_pyi: bool = False, **config_kwargs):
"""Tests that the given code:
- Behaves the same when formatted multiple times with isort.
- Agrees with black formatting.
- Matches the desired output or itself if none is provided.
"""
expected_output = expected_output or code
config_kwargs = {
"extension": "pyi" if is_pyi else None,
"profile": "black",
**config_kwargs,
}

# output should stay consistent over multiple runs
output = isort.code(code, profile="black")
assert output == isort.code(code, profile="black")
output = isort.code(code, **config_kwargs)
assert output == isort.code(code, **config_kwargs)

# output should agree with black
black_output = black_format(output)
assert output == black_output
black_output = black_format(output, is_pyi=is_pyi)
assert output == black_output, output

# output should match expected output
assert output == expected_output
Expand Down Expand Up @@ -369,3 +374,35 @@ def test_black_snippet_three():
DEFAULT_LINE_LENGTH = 88
""",
)


def test_black_pyi_file():
"""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.
"""

black_test(
"""# comment
import math
from typing import Sequence
def add(a: int, b: int) -> int: ...
def sub(a: int, b: int) -> int: ...
""",
"""# comment
import math
from typing import Sequence
def add(a: int, b: int) -> int: ...
def sub(a: int, b: int) -> int: ...
""",
is_pyi=True,
lines_before_imports=2,
lines_after_imports=2,
)

0 comments on commit 6c7801c

Please sign in to comment.