All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
1 from dataclasses import replace
2 from typing import Any, Iterator
3 from unittest.mock import patch
8 from tests.util import (
18 "beginning_backslash",
20 "class_blank_parentheses",
21 "class_methods_new_line",
29 "comments_non_breaking_space",
30 "comment_after_escaped_newline",
32 "composition_no_trailing_comma",
49 "function_trailing_comma",
54 "tricky_unicode_symbols",
58 EXPERIMENTAL_STRING_PROCESSING_CASES = [
62 "long_strings__edge_case",
63 "long_strings__regression",
68 "pattern_matching_simple",
69 "pattern_matching_complex",
70 "pattern_matching_extras",
71 "pattern_matching_style",
72 "pattern_matching_generic",
73 "parenthesized_context_managers",
77 "src/black/__init__.py",
78 "src/black/__main__.py",
79 "src/black/brackets.py",
81 "src/black/comments.py",
82 "src/black/concurrency.py",
86 "src/black/linegen.py",
90 "src/black/numerics.py",
91 "src/black/output.py",
92 "src/black/parsing.py",
93 "src/black/report.py",
95 "src/black/strings.py",
97 "src/blackd/__init__.py",
98 "src/black_primer/cli.py",
99 "src/black_primer/lib.py",
100 "src/blib2to3/pygram.py",
101 "src/blib2to3/pytree.py",
102 "src/blib2to3/pgen2/conv.py",
103 "src/blib2to3/pgen2/driver.py",
104 "src/blib2to3/pgen2/grammar.py",
105 "src/blib2to3/pgen2/literals.py",
106 "src/blib2to3/pgen2/parse.py",
107 "src/blib2to3/pgen2/pgen.py",
108 "src/blib2to3/pgen2/tokenize.py",
109 "src/blib2to3/pgen2/token.py",
111 "tests/test_black.py",
112 "tests/test_blackd.py",
113 "tests/test_format.py",
114 "tests/test_primer.py",
121 @pytest.fixture(autouse=True)
122 def patch_dump_to_file(request: Any) -> Iterator[None]:
123 with patch("black.dump_to_file", dump_to_stderr):
127 def check_file(filename: str, mode: black.Mode, *, data: bool = True) -> None:
128 source, expected = read_data(filename, data=data)
129 assert_format(source, expected, mode, fast=False)
132 @pytest.mark.parametrize("filename", SIMPLE_CASES)
133 def test_simple_format(filename: str) -> None:
134 check_file(filename, DEFAULT_MODE)
137 @pytest.mark.parametrize("filename", EXPERIMENTAL_STRING_PROCESSING_CASES)
138 def test_experimental_format(filename: str) -> None:
139 check_file(filename, black.Mode(experimental_string_processing=True))
142 @pytest.mark.parametrize("filename", SOURCES)
143 def test_source_is_formatted(filename: str) -> None:
144 path = THIS_DIR.parent / filename
145 check_file(str(path), DEFAULT_MODE, data=False)
153 def test_empty() -> None:
154 source = expected = ""
155 assert_format(source, expected)
158 def test_pep_572() -> None:
159 source, expected = read_data("pep_572")
160 assert_format(source, expected, minimum_version=(3, 8))
163 def test_pep_572_remove_parens() -> None:
164 source, expected = read_data("pep_572_remove_parens")
165 assert_format(source, expected, minimum_version=(3, 8))
168 def test_pep_572_do_not_remove_parens() -> None:
169 source, expected = read_data("pep_572_do_not_remove_parens")
170 # the AST safety checks will fail, but that's expected, just make sure no
171 # parentheses are touched
172 assert_format(source, expected, fast=True)
175 @pytest.mark.parametrize("major, minor", [(3, 9), (3, 10)])
176 def test_pep_572_newer_syntax(major: int, minor: int) -> None:
177 source, expected = read_data(f"pep_572_py{major}{minor}")
178 assert_format(source, expected, minimum_version=(major, minor))
181 def test_pep_570() -> None:
182 source, expected = read_data("pep_570")
183 assert_format(source, expected, minimum_version=(3, 8))
186 @pytest.mark.parametrize("filename", PY310_CASES)
187 def test_python_310(filename: str) -> None:
188 source, expected = read_data(filename)
189 mode = black.Mode(target_versions={black.TargetVersion.PY310})
190 assert_format(source, expected, mode, minimum_version=(3, 10))
193 def test_patma_invalid() -> None:
194 source, expected = read_data("pattern_matching_invalid")
195 mode = black.Mode(target_versions={black.TargetVersion.PY310})
196 with pytest.raises(black.parsing.InvalidInput) as exc_info:
197 assert_format(source, expected, mode, minimum_version=(3, 10))
199 exc_info.match("Cannot parse: 10:11")
202 def test_patma_hint() -> None:
203 source, expected = read_data("pattern_matching_simple")
204 mode = black.Mode(target_versions={black.TargetVersion.PY39})
205 with pytest.raises(black.parsing.InvalidInput) as exc_info:
206 assert_format(source, expected, mode, minimum_version=(3, 10))
208 exc_info.match(black.parsing.PY310_HINT)
211 def test_python_2_hint() -> None:
212 with pytest.raises(black.parsing.InvalidInput) as exc_info:
213 assert_format("print 'daylily'", "print 'daylily'")
214 exc_info.match(black.parsing.PY2_HINT)
217 def test_docstring_no_string_normalization() -> None:
218 """Like test_docstring but with string normalization off."""
219 source, expected = read_data("docstring_no_string_normalization")
220 mode = replace(DEFAULT_MODE, string_normalization=False)
221 assert_format(source, expected, mode)
224 def test_long_strings_flag_disabled() -> None:
225 """Tests for turning off the string processing logic."""
226 source, expected = read_data("long_strings_flag_disabled")
227 mode = replace(DEFAULT_MODE, experimental_string_processing=False)
228 assert_format(source, expected, mode)
231 def test_numeric_literals() -> None:
232 source, expected = read_data("numeric_literals")
233 mode = replace(DEFAULT_MODE, target_versions=PY36_VERSIONS)
234 assert_format(source, expected, mode)
237 def test_numeric_literals_ignoring_underscores() -> None:
238 source, expected = read_data("numeric_literals_skip_underscores")
239 mode = replace(DEFAULT_MODE, target_versions=PY36_VERSIONS)
240 assert_format(source, expected, mode)
243 def test_stub() -> None:
244 mode = replace(DEFAULT_MODE, is_pyi=True)
245 source, expected = read_data("stub.pyi")
246 assert_format(source, expected, mode)
249 def test_python38() -> None:
250 source, expected = read_data("python38")
251 assert_format(source, expected, minimum_version=(3, 8))
254 def test_python39() -> None:
255 source, expected = read_data("python39")
256 assert_format(source, expected, minimum_version=(3, 9))