Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_fstring(self) -> None:
source, expected = read_data("fstring")
actual = fs(source)
self.assertFormatEqual(expected, actual)
black.assert_equivalent(source, actual)
black.assert_stable(source, actual, black.FileMode())
def test_composition(self) -> None:
source, expected = read_data("composition")
actual = fs(source)
self.assertFormatEqual(expected, actual)
black.assert_equivalent(source, actual)
black.assert_stable(source, actual, black.FileMode())
def assert_stable(
dst: str,
line_length: int,
mode: black.FileMode = black.FileMode.AUTO_DETECT,
) -> None:
new_dst = format_str(dst, line_length=line_length, mode=mode)
if dst != new_dst:
raise AssertionError(
"INTERNAL ERROR: Black produced different code on the second pass "
"of the formatter."
) from None
def apply_black(code: str, python_version: PythonVersion) -> str:
root = black.find_project_root((Path().resolve(),))
path = root / "pyproject.toml"
if path.is_file():
value = str(path)
pyproject_toml = toml.load(value)
config = pyproject_toml.get("tool", {}).get("black", {})
else:
config = {}
return black.format_str(
code,
mode=black.FileMode(
target_versions={BLACK_PYTHON_VERSION[python_version]},
line_length=config.get("line-length", black.DEFAULT_LINE_LENGTH),
string_normalization=not config.get("skip-string-normalization", True),
),
def assert_stable(dst: str, mode: black.FileMode = black.FileMode(),) -> None:
new_dst = format_str(dst, mode=mode)
if dst != new_dst:
raise AssertionError(
"INTERNAL ERROR: Black produced different code on the second pass "
"of the formatter."
) from None
def to_python(self, *, indent=0, strict=True, pretty=False, black_mode=None):
self.columns_used() # for table consistency check/raise
if pretty:
strict = True
python_str = self.to_python_implementation(
indent=indent, strict=strict, print_sources=True
)
if pretty:
if _have_black:
try:
if black_mode is None:
black_mode = black.FileMode()
python_str = black.format_str(python_str, mode=black_mode)
except Exception:
pass
return python_str
with open(py_file) as f:
contents = f.read()
# Tell mypy to ignore this file
contents = "# type: ignore\n" + contents
for import_pkg in PACKAGES:
contents = contents.replace(
f"from {import_pkg} import",
f"from {IMPORT_PREFIX}.{import_pkg} import",
)
# Pre-format
try:
contents = black.format_file_contents(
contents, fast=False, mode=black.FileMode()
)
except black.NothingChanged:
pass
with open(py_file, "w") as f:
f.write(contents)
# Move the package into the correct directory in the source tree
package_path = os.path.join(TARGET_PACKAGE, package)
if os.path.exists(package_path):
shutil.rmtree(package_path)
shutil.move(os.path.join(tmp_dir, package), package_path)
def _get_op_str(op):
op_str = op.to_python_implementation(print_sources=False)
if have_black:
# noinspection PyBroadException
try:
black_mode = black.FileMode(line_length=60)
op_str = black.format_str(op_str, mode=black_mode)
except Exception:
pass
return op_str