Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: spacing in consecutive braces in f-string #223

Merged
merged 2 commits into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
716 changes: 359 additions & 357 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ snakefmt = 'snakefmt.snakefmt:main'
[tool.poetry.dependencies]
python = "^3.8.1"
click = "^8.0.0"
black = "^24.1.1"
black = "^24.3.0"
toml = "^0.10.2"
importlib_metadata = {version = ">=1.7.0,<5.0", python = "<3.8"}

Expand Down
56 changes: 54 additions & 2 deletions snakefmt/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,71 @@
"""

from dataclasses import fields
from functools import lru_cache
from pathlib import Path
from typing import Dict, Optional, Sequence, Union
from typing import Dict, Optional, Sequence, Tuple, Union

import click
import toml
from black import Mode, find_project_root
from black import Mode

from snakefmt import DEFAULT_LINE_LENGTH, DEFAULT_TARGET_VERSIONS
from snakefmt.exceptions import MalformattedToml

PathLike = Union[Path, str]


@lru_cache
def find_project_root(
srcs: Sequence[str], stdin_filename: Optional[str] = None
) -> Tuple[Path, str]:
"""Return a directory containing .git, .hg, or pyproject.toml.

That directory will be a common parent of all files and directories
passed in `srcs`.

If no directory in the tree contains a marker that would specify it's the
project root, the root of the file system is returned.

Returns a two-tuple with the first element as the project root path and
the second element as a string describing the method by which the
project root was discovered.

Note: taken directly from black v24.1.0 as they changed the behaviour of this
function in v24.2.0 to only find the root if the pyproject.toml file contained the
[tool.black] section. This is not the desired behaviour for snakefmt
"""
if stdin_filename is not None:
srcs = tuple(stdin_filename if s == "-" else s for s in srcs)
if not srcs:
srcs = [str(Path.cwd().resolve())]

path_srcs = [Path(Path.cwd(), src).resolve() for src in srcs]

# A list of lists of parents for each 'src'. 'src' is included as a
# "parent" of itself if it is a directory
src_parents = [
list(path.parents) + ([path] if path.is_dir() else []) for path in path_srcs
]

common_base = max(
set.intersection(*(set(parents) for parents in src_parents)),
key=lambda path: path.parts,
)

for directory in (common_base, *common_base.parents):
if (directory / ".git").exists():
return directory, ".git directory"

if (directory / ".hg").is_dir():
return directory, ".hg directory"

if (directory / "pyproject.toml").is_file():
return directory, "pyproject.toml"

return directory, "file system root"


def find_pyproject_toml(start_path: Sequence[str]) -> Optional[str]:
root, _ = find_project_root(start_path)
config_file = root / "pyproject.toml"
Expand Down
2 changes: 2 additions & 0 deletions snakefmt/parser/syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ def operator_skip_spacing(prev_token: Token, token: Token) -> bool:
return True
elif prev_token.type == tokenize.STRING and token.string == ",":
return True
elif prev_token.string == "}" and token.string == "{": # issue #220
return True
else:
return False

Expand Down
3 changes: 2 additions & 1 deletion tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ def test_black_and_snakefmt_default_line_lengths_aligned():
class TestFindPyprojectToml:
def test_find_pyproject_toml_nested_directory(self, tmp_path):
config_file = (tmp_path / "pyproject.toml").resolve()
config_file.touch()
# add [tool.black] to TOML to ensure black finds it (new in black v24.2.0)
config_file.write_text("[tool.black]\n")
dir1 = Path(tmp_path / "dir1").resolve()
dir1.mkdir()
snakefile = dir1 / "Snakefile"
Expand Down
9 changes: 6 additions & 3 deletions tests/test_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -861,9 +861,6 @@ def test_f_string_with_double_braces_in_input(self):

def test_f_string_with_double_braces_in_python_code(self):
"""https://github.com/snakemake/snakefmt/issues/215"""
"""def get_test_regions(wildcards):
benchmark = config["variant-calls"][wildcards.callset]["benchmark"]
return f"resources/regions/{benchmark}/test-regions.cov-{{cov}}.bed"""
snakecode = (
"def get_test_regions(wildcards):\n"
f'{TAB * 1}benchmark = config["variant-calls"][wildcards.callset]["benchmark"]\n' # noqa: E501
Expand All @@ -872,6 +869,12 @@ def test_f_string_with_double_braces_in_python_code(self):
formatter = setup_formatter(snakecode)
assert formatter.get_formatted() == snakecode

def test_f_string_spacing_of_consecutive_braces(self):
"""https://github.com/snakemake/snakefmt/issues/222"""
snakecode = 'f"{var1}{var2}"\n'
formatter = setup_formatter(snakecode)
assert formatter.get_formatted() == snakecode


class TestReformatting_SMK_BREAK:
"""
Expand Down
Loading