Skip to content

Commit

Permalink
Merge branch 'main' into issue-3315
Browse files Browse the repository at this point in the history
  • Loading branch information
aaossa committed Nov 6, 2022
2 parents f97cccd + 0e9d29a commit 320f582
Show file tree
Hide file tree
Showing 30 changed files with 658 additions and 167 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/diff_shades_comment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ jobs:
- name: Try to find pre-existing PR comment
if: steps.metadata.outputs.needs-comment == 'true'
id: find-comment
uses: peter-evans/find-comment@1769778a0c5bd330272d749d12c036d65e70d39d
uses: peter-evans/find-comment@b657a70ff16d17651703a84bee1cb9ad9d2be2ea
with:
issue-number: ${{ steps.metadata.outputs.pr-number }}
comment-author: "github-actions[bot]"
body-includes: "diff-shades"

- name: Create or update PR comment
if: steps.metadata.outputs.needs-comment == 'true'
uses: peter-evans/create-or-update-comment@c9fcb64660bc90ec1cc535646af190c992007c32
uses: peter-evans/create-or-update-comment@5adcb0bb0f9fb3f95ef05400558bdb3f329ee808
with:
comment-id: ${{ steps.find-comment.outputs.comment-id }}
issue-number: ${{ steps.metadata.outputs.pr-number }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pypi_upload.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ jobs:
- uses: actions/checkout@v3

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

Expand Down
9 changes: 9 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,18 @@

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

- Enforce empty lines before classes and functions with sticky leading comments (#3302)
- Implicitly concatenated strings used as function args are now wrapped inside
parentheses (#3307)

### Configuration

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

- Fix incorrectly applied .gitignore rules by considering the .gitignore location and
the relative path to the target file (#3338)
- Fix incorrectly ignoring .gitignore presence when more than one source directory is
specified (#3336)

### Packaging

Expand Down Expand Up @@ -49,6 +55,9 @@

<!-- For example, Docker, GitHub Actions, pre-commit, editors -->

- Vim plugin: Optionally allow using the system installation of Black via
`let g:black_use_virtualenv = 0`(#3309)

### Documentation

<!-- Major changes to documentation and policies. Small docs changes
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Further information can be found in our docs:
_Black_ is already [successfully used](https://github.com/psf/black#used-by) by many
projects, small and big. _Black_ has a comprehensive test suite, with efficient parallel
tests, and our own auto formatting and parallel Continuous Integration runner. Now that
we have become stable, you should not expect large formatting to changes in the future.
we have become stable, you should not expect large formatting changes in the future.
Stylistic changes will mostly be responses to bug reports and support for new Python
syntax. For more information please refer to the
[The Black Code Style](https://black.readthedocs.io/en/stable/the_black_code_style/index.html).
Expand Down Expand Up @@ -138,7 +138,7 @@ pandas, Pillow, Twisted, LocalStack, every Datadog Agent Integration, Home Assis
Zulip, Kedro, OpenOA, FLORIS, ORBIT, WOMBAT, and many more.

The following organizations use _Black_: Facebook, Dropbox, KeepTruckin, Mozilla, Quora,
Duolingo, QuantumBlack, Tesla.
Duolingo, QuantumBlack, Tesla, Archer Aviation.

Are we missing anyone? Let us know.

Expand Down
10 changes: 10 additions & 0 deletions autoload/black.vim
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ def _get_virtualenv_site_packages(venv_path, pyver):
return venv_path / 'lib' / f'python{pyver[0]}.{pyver[1]}' / 'site-packages'

def _initialize_black_env(upgrade=False):
if vim.eval("g:black_use_virtualenv ? 'true' : 'false'") == "false":
if upgrade:
print("Upgrade disabled due to g:black_use_virtualenv being disabled.")
print("Either use your system package manager (or pip) to upgrade black separately,")
print("or modify your vimrc to have 'let g:black_use_virtualenv = 1'.")
return False
else:
# Nothing needed to be done.
return True

pyver = sys.version_info[:3]
if pyver < (3, 7):
print("Sorry, Black requires Python 3.7+ to run.")
Expand Down
22 changes: 14 additions & 8 deletions docs/contributing/reference/reference_classes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,29 @@
.. autoclass:: black.brackets.BracketTracker
:members:

:class:`EmptyLineTracker`
:class:`Line`
-------------

.. autoclass:: black.lines.Line
:members:
:special-members: __str__, __bool__

:class:`LinesBlock`
-------------------------

.. autoclass:: black.EmptyLineTracker
.. autoclass:: black.lines.LinesBlock
:members:

:class:`Line`
-------------
:class:`EmptyLineTracker`
-------------------------

.. autoclass:: black.Line
.. autoclass:: black.lines.EmptyLineTracker
:members:
:special-members: __str__, __bool__

:class:`LineGenerator`
----------------------

.. autoclass:: black.LineGenerator
.. autoclass:: black.linegen.LineGenerator
:show-inheritance:
:members:

Expand All @@ -40,7 +46,7 @@
:class:`Report`
---------------

.. autoclass:: black.Report
.. autoclass:: black.report.Report
:members:
:special-members: __str__

Expand Down
32 changes: 32 additions & 0 deletions docs/guides/using_black_with_other_tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,35 @@ max-line-length = "88"
```

</details>

### pycodestyle

[pycodestyle](https://pycodestyle.pycqa.org/) is also a code linter like Flake8.

#### Configuration

```
max_line_length = 88
ignore = E203
```

#### Why those options above?

pycodestyle should be configured to only complain about lines that surpass `88`
characters via `max_line_length = 88`.

See
[Why are Flake8’s E203 and W503 violated?](https://black.readthedocs.io/en/stable/faq.html#why-are-flake8-s-e203-and-w503-violated)

#### Formats

<details>
<summary>setup.cfg</summary>

```cfg
[pycodestyle]
ignore = E203
max_line_length = 88
```

</details>
14 changes: 13 additions & 1 deletion docs/integrations/editors.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ Commands and shortcuts:
- you can optionally pass `target_version=<version>` with the same values as in the
command line.
- `:BlackUpgrade` to upgrade _Black_ inside the virtualenv;
- `:BlackVersion` to get the current version of _Black_ inside the virtualenv.
- `:BlackVersion` to get the current version of _Black_ in use.

Configuration:

Expand Down Expand Up @@ -160,6 +160,18 @@ If you need to do anything special to make your virtualenv work and install _Bla
example you want to run a version from main), create a virtualenv manually and point
`g:black_virtualenv` to it. The plugin will use it.

If you would prefer to use the system installation of _Black_ rather than a virtualenv,
then add this to your vimrc:

```
let g:black_use_virtualenv = 0
```

Note that the `:BlackUpgrade` command is only usable and useful with a virtualenv, so
when the virtualenv is not in use, `:BlackUpgrade` is disabled. If you need to upgrade
the system installation of _Black_, then use your system package manager or pip--
whatever tool you used to install _Black_ originally.

To run _Black_ on save, add the following lines to `.vimrc` or `init.vim`:

```
Expand Down
2 changes: 1 addition & 1 deletion docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Used by ReadTheDocs; pinned requirements for stability.

myst-parser==0.18.1
Sphinx==5.2.3
Sphinx==5.3.0
# Older versions break Sphinx even though they're declared to be supported.
docutils==0.19
sphinxcontrib-programoutput==0.17
Expand Down
49 changes: 35 additions & 14 deletions docs/the_black_code_style/future_style.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,26 +63,47 @@ limit. Line continuation backslashes are converted into parenthesized strings.
Unnecessary parentheses are stripped. The stability and status of this feature is
tracked in [this issue](https://github.com/psf/black/issues/2188).

### Removing newlines in the beginning of code blocks
### Improved empty line management

_Black_ will remove newlines in the beginning of new code blocks, i.e. when the
indentation level is increased. For example:
1. _Black_ will remove newlines in the beginning of new code blocks, i.e. when the
indentation level is increased. For example:

```python
def my_func():
```python
def my_func():

print("The line above me will be deleted!")
```
print("The line above me will be deleted!")
```

will be changed to:
will be changed to:

```python
def my_func():
print("The line above me will be deleted!")
```

This new feature will be applied to **all code blocks**: `def`, `class`, `if`,
`for`, `while`, `with`, `case` and `match`.

2. _Black_ will enforce empty lines before classes and functions with leading comments.
For example:

```python
some_var = 1
# Leading sticky comment
def my_func():
...
```

will be changed to:

```python
some_var = 1

```python
def my_func():
print("The line above me will be deleted!")
```

This new feature will be applied to **all code blocks**: `def`, `class`, `if`, `for`,
`while`, `with`, `case` and `match`.
# Leading sticky comment
def my_func():
...
```

### Improved parentheses management

Expand Down
3 changes: 3 additions & 0 deletions plugin/black.vim
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ endif
if !exists("g:black_target_version")
let g:black_target_version = ""
endif
if !exists("g:black_use_virtualenv")
let g:black_use_virtualenv = 1
endif
if !exists("g:black_preview")
let g:black_preview = 0
endif
Expand Down
37 changes: 22 additions & 15 deletions src/black/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import click
from click.core import ParameterSource
from mypy_extensions import mypyc_attr
from pathspec import PathSpec
from pathspec.patterns.gitwildmatch import GitWildMatchPatternError

from _black_version import version as __version__
Expand Down Expand Up @@ -61,7 +62,7 @@
unmask_cell,
)
from black.linegen import LN, LineGenerator, transform_line
from black.lines import EmptyLineTracker, Line
from black.lines import EmptyLineTracker, LinesBlock
from black.mode import (
FUTURE_FLAG_TO_FEATURE,
VERSION_TO_FEATURES,
Expand Down Expand Up @@ -497,8 +498,10 @@ def main( # noqa: C901
user_level_config = str(find_user_pyproject_toml())
if config == user_level_config:
out(
"Using configuration from user-level config at "
f"'{user_level_config}'.",
(
"Using configuration from user-level config at "
f"'{user_level_config}'."
),
fg="blue",
)
elif config_source in (
Expand Down Expand Up @@ -625,7 +628,10 @@ def get_sources(
sources: Set[Path] = set()
root = ctx.obj["root"]

gitignore = None
exclude_is_None = exclude is None
exclude = re_compile_maybe_verbose(DEFAULT_EXCLUDES) if exclude is None else exclude
gitignore = None # type: Optional[PathSpec]
root_gitignore = get_gitignore(root)

for s in src:
if s == "-" and stdin_filename:
Expand Down Expand Up @@ -660,9 +666,7 @@ def get_sources(

sources.add(p)
elif p.is_dir():
if exclude is None:
exclude = re_compile_maybe_verbose(DEFAULT_EXCLUDES)
root_gitignore = get_gitignore(root)
if exclude_is_None:
p_gitignore = get_gitignore(p)
# No need to use p's gitignore if it is identical to root's gitignore
# (i.e. root and p point to the same directory).
Expand Down Expand Up @@ -1077,7 +1081,7 @@ def f(

def _format_str_once(src_contents: str, *, mode: Mode) -> str:
src_node = lib2to3_parse(src_contents.lstrip(), mode.target_versions)
dst_contents = []
dst_blocks: List[LinesBlock] = []
if mode.target_versions:
versions = mode.target_versions
else:
Expand All @@ -1086,22 +1090,25 @@ def _format_str_once(src_contents: str, *, mode: Mode) -> str:

normalize_fmt_off(src_node, preview=mode.preview)
lines = LineGenerator(mode=mode)
elt = EmptyLineTracker(is_pyi=mode.is_pyi)
empty_line = Line(mode=mode)
after = 0
elt = EmptyLineTracker(mode=mode)
split_line_features = {
feature
for feature in {Feature.TRAILING_COMMA_IN_CALL, Feature.TRAILING_COMMA_IN_DEF}
if supports_feature(versions, feature)
}
block: Optional[LinesBlock] = None
for current_line in lines.visit(src_node):
dst_contents.append(str(empty_line) * after)
before, after = elt.maybe_empty_lines(current_line)
dst_contents.append(str(empty_line) * before)
block = elt.maybe_empty_lines(current_line)
dst_blocks.append(block)
for line in transform_line(
current_line, mode=mode, features=split_line_features
):
dst_contents.append(str(line))
block.content_lines.append(str(line))
if dst_blocks:
dst_blocks[-1].after = 0
dst_contents = []
for block in dst_blocks:
dst_contents.extend(block.all_lines())
return "".join(dst_contents)


Expand Down

0 comments on commit 320f582

Please sign in to comment.