_____________________________ test_black_pyi_file ______________________________
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
import numpy as np
def add(a: np.ndarray, b: np.ndarray) -> np.ndarray:
...
def sub(a: np.ndarray, b: np.ndarray) -> np.ndarray:
...
""",
"""# comment
import math
from typing import Sequence
import numpy as np
def add(a: np.ndarray, b: np.ndarray) -> np.ndarray:
...
def sub(a: np.ndarray, b: np.ndarray) -> np.ndarray:
...
""",
is_pyi=False,
lines_before_imports=2,
lines_after_imports=2,
)
tests/unit/profiles/test_black.py:385:
code = '# comment\n\n\nimport math\n\nfrom typing import Sequence\nimport numpy as np\n\n\ndef add(a: np.ndarray, b: np.ndarray) -> np.ndarray:\n ...\n\n\ndef sub(a: np.ndarray, b: np.ndarray) -> np.ndarray:\n ...\n'
expected_output = '# comment\n\n\nimport math\nfrom typing import Sequence\n\nimport numpy as np\n\n\ndef add(a: np.ndarray, b: np.ndarray) -> np.ndarray:\n ...\n\n\ndef sub(a: np.ndarray, b: np.ndarray) -> np.ndarray:\n ...\n'
is_pyi = False
config_kwargs = {'extension': None, 'lines_after_imports': 2, 'lines_before_imports': 2, 'profile': 'black'}
output = '# comment\n\n\nimport math\nfrom typing import Sequence\n\nimport numpy as np\n\n\ndef add(a: np.ndarray, b: np.ndarray) -> np.ndarray:\n ...\n\n\ndef sub(a: np.ndarray, b: np.ndarray) -> np.ndarray:\n ...\n'
@py_assert3 = None, @py_assert7 = None, @py_assert1 = False
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, **config_kwargs)
assert output == isort.code(code, **config_kwargs)
# output should agree with black
black_output = black_format(output, is_pyi=is_pyi)
> assert output == black_output
E AssertionError: assert '# comment\n\n\nimport math\nfrom typing import Sequence\n\nimport numpy as np\n\n\ndef add(a: np.ndarray, b: np.ndarray) -> np.ndarray:\n ...\n\n\ndef sub(a: np.ndarray, b: np.ndarray) -> np.ndarray:\n ...\n' == '# comment\n\n\nimport math\nfrom typing import Sequence\n\nimport numpy as np\n\n\ndef add(a: np.ndarray, b: np.ndarray) -> np.ndarray: ...\n\n\ndef sub(a: np.ndarray, b: np.ndarray) -> np.ndarray: ...\n'
E
E # comment
E
E
E import math
E from typing import Sequence
E
E import numpy as np
E
E
E - def add(a: np.ndarray, b: np.ndarray) -> np.ndarray: ...
E ? ----
E + def add(a: np.ndarray, b: np.ndarray) -> np.ndarray:
E + ...
E
E
E - def sub(a: np.ndarray, b: np.ndarray) -> np.ndarray: ...
E ? ----
E + def sub(a: np.ndarray, b: np.ndarray) -> np.ndarray:
E + ...
tests/unit/profiles/test_black.py:41: AssertionError
https://github.com/psf/black/blob/main/CHANGES.md#2410
Dummy class and function implementations consisting only of ... are formatted more compactly (#3796)
psf/black#3796
psf/black#1797 (comment)
https://github.com/psf/black/blob/main/CHANGES.md#2410
psf/black#3796
psf/black#1797 (comment)