Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
cooperlees committed Jan 16, 2023
2 parents 46f7e55 + d4ff985 commit cbcee44
Show file tree
Hide file tree
Showing 14 changed files with 312 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/diff_shades_comment.yml
Expand Up @@ -33,7 +33,7 @@ jobs:
- name: Try to find pre-existing PR comment
if: steps.metadata.outputs.needs-comment == 'true'
id: find-comment
uses: peter-evans/find-comment@f4499a714d59013c74a08789b48abe4b704364a0
uses: peter-evans/find-comment@81e2da3af01c92f83cb927cf3ace0e085617c556
with:
issue-number: ${{ steps.metadata.outputs.pr-number }}
comment-author: "github-actions[bot]"
Expand Down
7 changes: 7 additions & 0 deletions .github/workflows/lint.yml
Expand Up @@ -16,6 +16,13 @@ jobs:
steps:
- uses: actions/checkout@v3

- name: Assert PR target is main
if: github.event_name == 'pull_request' && github.repository == 'psf/black'
run: |
if [ "$GITHUB_BASE_REF" != "main" ]; then
echo "::error::PR targeting '$GITHUB_BASE_REF', please refile targeting 'main'." && exit 1
fi
- name: Set up latest Python
uses: actions/setup-python@v4
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pypi_upload.yml
Expand Up @@ -59,7 +59,7 @@ jobs:
- uses: actions/checkout@v3

- name: Build wheels via cibuildwheel
uses: pypa/cibuildwheel@v2.11.3
uses: pypa/cibuildwheel@v2.11.4
env:
CIBW_ARCHS_MACOS: "${{ matrix.macos_arch }}"

Expand Down
2 changes: 1 addition & 1 deletion AUTHORS.md
Expand Up @@ -10,7 +10,7 @@ Maintained with:
- [Mika Naylor](mailto:mail@autophagy.io)
- [Zsolt Dollenstein](mailto:zsol.zsol@gmail.com)
- [Cooper Lees](mailto:me@cooperlees.com)
- Richard Si
- [Richard Si](mailto:sichard26@gmail.com)
- [Felix Hildén](mailto:felix.hilden@gmail.com)
- [Batuhan Taskaya](mailto:batuhan@python.org)

Expand Down
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -16,6 +16,7 @@

<!-- Changes that affect Black's preview style -->

- Add parentheses around `if`-`else` expressions (#2278)
- Improve the performance on large expressions that contain many strings (#3467)
- Fix a crash in preview style with assert + parenthesized string (#3415)
- Fix crashes in preview style with walrus operators used in function return annotations
Expand Down
2 changes: 1 addition & 1 deletion docs/contributing/issue_triage.md
Expand Up @@ -42,7 +42,7 @@ The lifecycle of a bug report or user support issue typically goes something lik
1. _the issue is waiting for triage_
2. **identified** - has been marked with a type label and other relevant labels, more
details or a functional reproduction may be still needed (and therefore should be
marked with `S: needs repro` or `S: awaiting reponse`)
marked with `S: needs repro` or `S: awaiting response`)
3. **confirmed** - the issue can reproduced and necessary details have been provided
4. **discussion** - initial triage has been done and now the general details on how the
issue should be best resolved are being hashed out
Expand Down
106 changes: 106 additions & 0 deletions docs/integrations/editors.md
Expand Up @@ -10,6 +10,19 @@ Options include the following:

## PyCharm/IntelliJ IDEA

There are three different ways you can use _Black_ from PyCharm:

1. As local server using the BlackConnect plugin
1. As external tool
1. As file watcher

The first option is the simplest to set up and formats the fastest (by spinning up
{doc}`Black's HTTP server </usage_and_configuration/black_as_a_server>`, avoiding the
startup cost on subsequent formats), but if you would prefer to not install a
third-party plugin or blackd's extra dependencies, the other two are also great options.

### As local server

1. Install _Black_ with the `d` extra.

```console
Expand Down Expand Up @@ -46,6 +59,99 @@ Options include the following:
- In `Trigger Settings` section of plugin configuration check
`Trigger when saving changed files`.

### As external tool

1. Install `black`.

```console
$ pip install black
```

1. Locate your `black` installation folder.

On macOS / Linux / BSD:

```console
$ which black
/usr/local/bin/black # possible location
```

On Windows:

```console
$ where black
%LocalAppData%\Programs\Python\Python36-32\Scripts\black.exe # possible location
```

Note that if you are using a virtual environment detected by PyCharm, this is an
unneeded step. In this case the path to `black` is `$PyInterpreterDirectory$/black`.

1. Open External tools in PyCharm/IntelliJ IDEA

On macOS:

`PyCharm -> Preferences -> Tools -> External Tools`

On Windows / Linux / BSD:

`File -> Settings -> Tools -> External Tools`

1. Click the + icon to add a new external tool with the following values:

- Name: Black
- Description: Black is the uncompromising Python code formatter.
- Program: \<install_location_from_step_2>
- Arguments: `"$FilePath$"`

1. Format the currently opened file by selecting `Tools -> External Tools -> black`.

- Alternatively, you can set a keyboard shortcut by navigating to
`Preferences or Settings -> Keymap -> External Tools -> External Tools - Black`.

### As file watcher

1. Install `black`.

```console
$ pip install black
```

1. Locate your `black` installation folder.

On macOS / Linux / BSD:

```console
$ which black
/usr/local/bin/black # possible location
```

On Windows:

```console
$ where black
%LocalAppData%\Programs\Python\Python36-32\Scripts\black.exe # possible location
```

Note that if you are using a virtual environment detected by PyCharm, this is an
unneeded step. In this case the path to `black` is `$PyInterpreterDirectory$/black`.

1. Make sure you have the
[File Watchers](https://plugins.jetbrains.com/plugin/7177-file-watchers) plugin
installed.
1. Go to `Preferences or Settings -> Tools -> File Watchers` and click `+` to add a new
watcher:
- Name: Black
- File type: Python
- Scope: Project Files
- Program: \<install_location_from_step_2>
- Arguments: `$FilePath$`
- Output paths to refresh: `$FilePath$`
- Working directory: `$ProjectFileDir$`

- In Advanced Options
- Uncheck "Auto-save edited files to trigger the watcher"
- Uncheck "Trigger the watcher on external changes"

## Wing IDE

Wing IDE supports `black` via **Preference Settings** for system wide settings and
Expand Down
6 changes: 3 additions & 3 deletions docs/the_black_code_style/future_style.md
Expand Up @@ -19,7 +19,7 @@ with make_context_manager1() as cm1, make_context_manager2() as cm2, make_contex
... # nothing to split on - line too long
```

So _Black_ will eventually format it like this:
So _Black_ will, when we implement this, format it like this:

```py3
with \
Expand All @@ -31,8 +31,8 @@ with \
... # backslashes and an ugly stranded colon
```

Although when the target version is Python 3.9 or higher, _Black_ will use parentheses
instead since they're allowed in Python 3.9 and higher.
Although when the target version is Python 3.9 or higher, _Black_ will, when we
implement this, use parentheses instead since they're allowed in Python 3.9 and higher.

An alternative to consider if the backslashes in the above formatting are undesirable is
to use {external:py:obj}`contextlib.ExitStack` to combine context managers in the
Expand Down
6 changes: 3 additions & 3 deletions docs/usage_and_configuration/the_basics.md
Expand Up @@ -40,6 +40,9 @@ so style options are deliberately limited and rarely added.

</details>

Note that all command-line options listed above can also be configured using a
`pyproject.toml` file (more on that below).

### Code input alternatives

#### Standard Input
Expand Down Expand Up @@ -287,9 +290,6 @@ file hierarchy.

## Next steps

You've probably noted that not all of the options you can pass to _Black_ have been
covered. Don't worry, the rest will be covered in a later section.

A good next step would be configuring auto-discovery so `black .` is all you need
instead of laborously listing every file or directory. You can get started by heading
over to [File collection and discovery](./file_collection_and_discovery.md).
Expand Down
16 changes: 10 additions & 6 deletions src/black/__init__.py
Expand Up @@ -478,16 +478,20 @@ def main( # noqa: C901
)

normalized = [
(source, source)
if source == "-"
else (normalize_path_maybe_ignore(Path(source), root), source)
(
(source, source)
if source == "-"
else (normalize_path_maybe_ignore(Path(source), root), source)
)
for source in src
]
srcs_string = ", ".join(
[
f'"{_norm}"'
if _norm
else f'\033[31m"{source} (skipping - invalid)"\033[34m'
(
f'"{_norm}"'
if _norm
else f'\033[31m"{source} (skipping - invalid)"\033[34m'
)
for _norm, source in normalized
]
)
Expand Down
16 changes: 16 additions & 0 deletions src/black/linegen.py
Expand Up @@ -141,6 +141,22 @@ def visit_default(self, node: LN) -> Iterator[Line]:
self.current_line.append(node)
yield from super().visit_default(node)

def visit_test(self, node: Node) -> Iterator[Line]:
"""Visit an `x if y else z` test"""

if Preview.parenthesize_conditional_expressions in self.mode:
already_parenthesized = (
node.prev_sibling and node.prev_sibling.type == token.LPAR
)

if not already_parenthesized:
lpar = Leaf(token.LPAR, "")
rpar = Leaf(token.RPAR, "")
node.insert_child(0, lpar)
node.append_child(rpar)

yield from self.visit_default(node)

def visit_INDENT(self, node: Leaf) -> Iterator[Line]:
"""Increase indentation level, maybe yield a line."""
# In blib2to3 INDENT never holds comments.
Expand Down
1 change: 1 addition & 0 deletions src/black/mode.py
Expand Up @@ -161,6 +161,7 @@ class Preview(Enum):
# NOTE: string_processing requires wrap_long_dict_values_in_parens
# for https://github.com/psf/black/issues/3117 to be fixed.
string_processing = auto()
parenthesize_conditional_expressions = auto()
skip_magic_trailing_comma_in_subscript = auto()
wrap_long_dict_values_in_parens = auto()

Expand Down

0 comments on commit cbcee44

Please sign in to comment.