MCPcopy Create free account
hub / github.com/psf/black / assert_format

Function assert_format

tests/util.py:64–111  ·  view source on GitHub ↗

Convenience function to check that Black formats as expected. You can pass @minimum_version if you're passing code with newer syntax to guard safety guards so they don't just crash with a SyntaxError. Please note this is separate from TargetVerson Mode configuration.

(
    source: str,
    expected: str,
    mode: black.Mode = DEFAULT_MODE,
    *,
    fast: bool = False,
    minimum_version: Optional[Tuple[int, int]] = None,
)

Source from the content-addressed store, hash-verified

62
63
64def assert_format(
65 source: str,
66 expected: str,
67 mode: black.Mode = DEFAULT_MODE,
68 *,
69 fast: bool = False,
70 minimum_version: Optional[Tuple[int, int]] = None,
71) -> None:
72 """Convenience function to check that Black formats as expected.
73
74 You can pass @minimum_version if you're passing code with newer syntax to guard
75 safety guards so they don't just crash with a SyntaxError. Please note this is
76 separate from TargetVerson Mode configuration.
77 """
78 _assert_format_inner(
79 source, expected, mode, fast=fast, minimum_version=minimum_version
80 )
81
82 # For both preview and non-preview tests, ensure that Black doesn't crash on
83 # this code, but don't pass "expected" because the precise output may differ.
84 try:
85 _assert_format_inner(
86 source,
87 None,
88 replace(mode, preview=not mode.preview),
89 fast=fast,
90 minimum_version=minimum_version,
91 )
92 except Exception as e:
93 text = "non-preview" if mode.preview else "preview"
94 raise FormatFailure(
95 f"Black crashed formatting this case in {text} mode."
96 ) from e
97 # Similarly, setting line length to 1 is a good way to catch
98 # stability bugs. But only in non-preview mode because preview mode
99 # currently has a lot of line length 1 bugs.
100 try:
101 _assert_format_inner(
102 source,
103 None,
104 replace(mode, preview=False, line_length=1),
105 fast=fast,
106 minimum_version=minimum_version,
107 )
108 except Exception as e:
109 raise FormatFailure(
110 "Black crashed formatting this case with line-length set to 1."
111 ) from e
112
113
114def _assert_format_inner(

Callers 15

check_fileFunction · 0.90
test_emptyFunction · 0.90
test_python_36Function · 0.90
test_python_37Function · 0.90
test_python_38Function · 0.90
test_python_39Function · 0.90
test_python_310Function · 0.90
test_patma_invalidFunction · 0.90

Calls 3

_assert_format_innerFunction · 0.85
replaceFunction · 0.85
FormatFailureClass · 0.85

Tested by 15

check_fileFunction · 0.72
test_emptyFunction · 0.72
test_python_36Function · 0.72
test_python_37Function · 0.72
test_python_38Function · 0.72
test_python_39Function · 0.72
test_python_310Function · 0.72
test_patma_invalidFunction · 0.72