Skip to content

Commit

Permalink
Merge branch 'main' into psfgh-4143
Browse files Browse the repository at this point in the history
  • Loading branch information
RedGuy12 committed Jan 24, 2024
2 parents 9ef0043 + 8fe602b commit 5e7ad45
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/diff_shades.yml
Expand Up @@ -72,7 +72,7 @@ jobs:
- name: Attempt to use cached baseline analysis
id: baseline-cache
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: ${{ matrix.baseline-analysis }}
key: ${{ matrix.baseline-cache-key }}
Expand Down
3 changes: 3 additions & 0 deletions CHANGES.md
Expand Up @@ -23,12 +23,15 @@
blocks, except immediately before a docstring (#4130)
- For stubs, fix logic to enforce empty line after nested classes with bodies (#4141)
- Fix crash when using a walrus in a dictionary (#4155)
- Fix unnecessary parentheses when wrapping long dicts (#4135)
- Stop normalizing spaces before `# fmt: skip` comments (#4146)

### Configuration

<!-- Changes to how Black can be configured -->

- Fix symlink handling, properly catch and ignore symlinks that point outside of root
(#4161)
- Fix cache mtime logic that resulted in false positive cache hits (#4128)

### Packaging
Expand Down
6 changes: 3 additions & 3 deletions docs/guides/using_black_with_other_tools.md
Expand Up @@ -145,7 +145,7 @@ There are a few deviations that cause incompatibilities with _Black_.

```
max-line-length = 88
extend-ignore = E203
extend-ignore = E203, E704
```

#### Why those options above?
Expand Down Expand Up @@ -184,7 +184,7 @@ extend-ignore = E203, E704
```ini
[flake8]
max-line-length = 88
extend-ignore = E203
extend-ignore = E203, E704
```

</details>
Expand All @@ -195,7 +195,7 @@ extend-ignore = E203
```ini
[flake8]
max-line-length = 88
extend-ignore = E203
extend-ignore = E203, E704
```

</details>
Expand Down
6 changes: 5 additions & 1 deletion src/black/__init__.py
Expand Up @@ -49,6 +49,7 @@
find_user_pyproject_toml,
gen_python_files,
get_gitignore,
get_root_relative_path,
normalize_path_maybe_ignore,
parse_pyproject_toml,
path_is_excluded,
Expand Down Expand Up @@ -700,7 +701,10 @@ def get_sources(

# Compare the logic here to the logic in `gen_python_files`.
if is_stdin or path.is_file():
root_relative_path = path.absolute().relative_to(root).as_posix()
root_relative_path = get_root_relative_path(path, root, report)

if root_relative_path is None:
continue

root_relative_path = "/" + root_relative_path

Expand Down
24 changes: 16 additions & 8 deletions src/black/files.py
Expand Up @@ -259,14 +259,7 @@ def normalize_path_maybe_ignore(
try:
abspath = path if path.is_absolute() else Path.cwd() / path
normalized_path = abspath.resolve()
try:
root_relative_path = normalized_path.relative_to(root).as_posix()
except ValueError:
if report:
report.path_ignored(
path, f"is a symbolic link that points outside {root}"
)
return None
root_relative_path = get_root_relative_path(normalized_path, root, report)

except OSError as e:
if report:
Expand All @@ -276,6 +269,21 @@ def normalize_path_maybe_ignore(
return root_relative_path


def get_root_relative_path(
path: Path,
root: Path,
report: Optional[Report] = None,
) -> Optional[str]:
"""Returns the file path relative to the 'root' directory"""
try:
root_relative_path = path.absolute().relative_to(root).as_posix()
except ValueError:
if report:
report.path_ignored(path, f"is a symbolic link that points outside {root}")
return None
return root_relative_path


def _path_is_ignored(
root_relative_path: str,
root: Path,
Expand Down
7 changes: 3 additions & 4 deletions src/black/linegen.py
Expand Up @@ -244,15 +244,14 @@ def visit_dictsetmaker(self, node: Node) -> Iterator[Line]:
if node.children[i - 1].type == token.COLON:
if (
child.type == syms.atom
and child.children[0].type == token.LPAR
and child.children[0].type in OPENING_BRACKETS
and not is_walrus_assignment(child)
):
if maybe_make_parens_invisible_in_atom(
maybe_make_parens_invisible_in_atom(
child,
parent=node,
remove_brackets_around_comma=False,
):
wrap_in_parentheses(node, child, visible=False)
)
else:
wrap_in_parentheses(node, child, visible=False)
yield from self.visit_default(node)
Expand Down
38 changes: 38 additions & 0 deletions tests/data/cases/preview_long_dict_values.py
Expand Up @@ -37,6 +37,26 @@
}


class Random:
def func():
random_service.status.active_states.inactive = (
make_new_top_level_state_from_dict(
{
"topLevelBase": {
"secondaryBase": {
"timestamp": 1234,
"latitude": 1,
"longitude": 2,
"actionTimestamp": Timestamp(
seconds=1530584000, nanos=0
).ToJsonString(),
}
},
}
)
)


# output


Expand Down Expand Up @@ -89,3 +109,21 @@
}
),
}


class Random:
def func():
random_service.status.active_states.inactive = (
make_new_top_level_state_from_dict({
"topLevelBase": {
"secondaryBase": {
"timestamp": 1234,
"latitude": 1,
"longitude": 2,
"actionTimestamp": (
Timestamp(seconds=1530584000, nanos=0).ToJsonString()
),
}
},
})
)
14 changes: 14 additions & 0 deletions tests/test_black.py
Expand Up @@ -2592,6 +2592,20 @@ def test_symlinks(self) -> None:
outside_root_symlink.resolve.assert_called_once()
ignored_symlink.resolve.assert_not_called()

def test_get_sources_with_stdin_symlink_outside_root(
self,
) -> None:
path = THIS_DIR / "data" / "include_exclude_tests"
stdin_filename = str(path / "b/exclude/a.py")
outside_root_symlink = Path("/target_directory/a.py")
with patch("pathlib.Path.resolve", return_value=outside_root_symlink):
assert_collected_sources(
root=Path("target_directory/"),
src=["-"],
expected=[],
stdin_filename=stdin_filename,
)

@patch("black.find_project_root", lambda *args: (THIS_DIR.resolve(), None))
def test_get_sources_with_stdin(self) -> None:
src = ["-"]
Expand Down

0 comments on commit 5e7ad45

Please sign in to comment.