Commit Graph

790 Commits

Author SHA1 Message Date
Łukasz Langa
9270a10f6f Improve docstring re-indentation handling
This addresses a few crashers, namely:

* producing non-equivalent code due to mangling escaped newlines,

* invalid hugging quote characters in the docstring body to the docstring outer
  triple quotes (causing a quadruple quote which is a syntax error),

* lack of handling for docstrings that start on the same line as the `def`, and

* invalid stripping of outer triple quotes when the docstring contained
  a string prefix.

As a bonus, tests now also run when string normalization is disabled.
2020-08-25 23:14:39 +02:00
Łukasz Langa
586d24236e Address pre-existing trailing commas when not in the rightmost bracket pair
This required some hackery.  Long story short, we need to reuse the ability to
omit rightmost bracket pairs (which glues them together and splits on something
else instead), for use with pre-existing trailing commas.

This form of user-controlled formatting is brittle so we have to be careful not
to cause a scenario where Black first formats code without trailing commas in
one way, and then looks at the same file with pre-existing trailing commas
(that it itself put on the previous run) and decides to format the code again.

One particular ugly edge case here is handling of optional parentheses.  In
particular, the long-standing `line_length=1` hack got in the way of
pre-existing trailing commas and had to be removed.  Instead, a more
intelligent but costly solution was put in place: a "second opinion" if the
formatting that omits optional parentheses ended up causing lines to be too
long.  Again, for efficiency purposes, Black reuses Leaf objects from blib2to3
and modifies them in place, which was invalid for having two separate
formattings.  Line cloning was used to mitigate this.

Fixes #1619
2020-08-25 22:10:05 +02:00
Łukasz Langa
d46268cd67
Run trailing comma tests with TargetVersion.PY38 2020-08-24 21:33:51 +02:00
Łukasz Langa
292bceb9fd
Add more trailing comma test variants 2020-08-24 18:48:11 +02:00
Łukasz Langa
5faabb5616
Make doc generation a little smarter, update doc sections 2020-08-24 14:42:17 +02:00
Łukasz Langa
fd5928c589
Update the changelog 2020-08-24 14:11:18 +02:00
Zac-HD
cd3a93a146 Property-based fuzz test 2020-08-21 16:47:49 +02:00
Łukasz Langa
205f3b67fb Fix dealing with generated files in docs 2020-08-21 16:45:30 +02:00
Łukasz Langa
cb6f2198b8 Use properly renamed function name in docs 2020-08-21 16:45:30 +02:00
Łukasz Langa
2fc1dfca96 Require Sphinx 3 2020-08-21 16:45:30 +02:00
Łukasz Langa
f3ab907a96 Mark Primer projects that will change formatting 2020-08-21 16:45:30 +02:00
Łukasz Langa
bc71685d22 Open file explicitly with UTF-8 so it works on Windows, too 2020-08-21 16:45:30 +02:00
Łukasz Langa
05cc7ede6a Reformat docs/conf.py according to the new style 2020-08-21 16:45:30 +02:00
Łukasz Langa
788268bc39 Re-implement magic trailing comma handling:
- when a trailing comma is specified in any bracket pair, that signals to Black
  that this bracket pair needs to be always exploded, e.g. presented as "one
  item per line";

- this causes some changes to previously formatted code that erroneously left
  trailing commas embedded into single-line expressions;

- internally, Black needs to be able to identify trailing commas that it put
  itself compared to pre-existing trailing commas. We do this by using/abusing
  lib2to3's `was_checked` attribute.  It's True for internally generated
  trailing commas and False for pre-existing ones (in fact, for all
  pre-existing leaves and nodes).

Fixes #1288
2020-08-21 16:45:30 +02:00
Łukasz Langa
4938cc9e9a Reset trailing comma handling 2020-08-21 16:45:30 +02:00
Richard Si
37a0020e07
Upgrade docs to Sphinx 3+ and add doc build test (#1613)
* Upgrade docs to Sphinx 3+
* Fix all the warnings...

- Fixed bad docstrings
- Fixed bad fenced code blocks in documentation
- Blocklisted some sections from being generated from the README
- Added missing documentation to index.rst
- Fixed an invalid autofunction directive in reference/reference_functions.rst
- Pin another documentation dependency

* Add documentation build test
2020-08-21 00:06:41 +02:00
Jelle Zijlstra
e5bb92f53c
Disable string splitting/merging by default (#1609)
* put experimental string stuff behind a flag
* update tests
* don't need an output section if it's the same as the input
* Primer: Expect no formatting changes in attrs, hypothesis and poetry with --experimental-string-processing off

Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com>
2020-08-20 14:23:28 +02:00
Łukasz Langa
e1027e2bee
Update all dependencies to latest versions 2020-08-18 12:31:15 +02:00
Daanyaal Syed
80425ca440
Fix inline code style in README (#1608)
Refer to `pyproject.toml` in HTML section of README with HTML code tags
2020-08-17 11:55:38 -07:00
David Szotten
820f38708f
fix unary op detection (#1600) 2020-08-14 09:17:56 -07:00
David Szotten
d1ad8730e3
don't strip brackets before lsqb (#1575) (#1590)
if the string contains a PERCENT, it's not safe to remove brackets that
follow and operator with the same or higher precedence than PERCENT
2020-08-13 19:20:46 -07:00
Jelle Zijlstra
5e1f620af7
fix some docstring crashes (#1593)
Allow removing some trailing whitespace
2020-08-13 16:40:45 -07:00
Jelle Zijlstra
8842c5ffa8
in verbose mode, print stack trace (#1594)
Make Black failures easier to debug
2020-08-13 11:14:34 -07:00
Richard Si
97c11f22aa
Make --exclude only apply to recursively found files (#1591)
Ever since --force-exclude was added, --exclude started to touch files
that were given to Black through the CLI too. This is not documented
behaviour and neither expected as --exclude and --force-exclude now
behave the same!

Before this commit, get_sources() when encountering a file that was passed
explicitly through the CLI would pass a single Path object list to
gen_python_files(). This causes bad behaviour since that function
doesn't treat the exclude and force_exclude regexes differently. Which
is fine for recursively found files, but *not* for files given through
the CLI.

Now when get_sources() iterates through srcs and encounters
a file, it checks if the force_exclude regex matches, if not, then the
file will be added to the computed sources set.

A new function had to be created since before you can do regex matching,
the path must be normalized. The full process of normalizing the path is
somewhat long as there is special error handling. I didn't want to
duplicate this logic in get_sources() and gen_python_files() so that's
why there is a new helper function.
2020-08-12 20:07:19 -07:00
Chris Rose
149b38d674
Add the direnv base directory to the default excludes (#1564)
Co-authored-by: Chris Rose <offline@offby1.net>
2020-08-12 19:28:01 -07:00
Jelle Zijlstra
f825e7ef28
Remove slow assertion (#1592)
Partial fix for #1581

This assertion produces behavior quadratic in the number of leaves in a line, which is making Black extremely slow on files with very long expressions. On my benchmark file this change makes Black 10x faster.
2020-08-12 19:12:21 -07:00
Kaligule
b59a524657
fix spelling (#1567)
Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com>
2020-08-01 14:21:55 -07:00
Maximilian Cosmo Sitter
537ea8df35
Update to accomodate isort 5 release changes. (#1559)
Isort 5 introduced profiles and ensure_newline_before_comments options. Either needs to be added to work correctly with black.

Co-authored-by: Richard Si <63936253+ichard26@users.noreply.github.com>
2020-07-21 17:29:38 -07:00
Vinicius Gubiani Ferreira
2c5041cfa0
docs: Improve pre-commit use (#1551)
Stable tag wasn't available and crashed when attempting to set initial
pre-commit. Also the python version needs to be installed so it would
be better to use the generic "python3" command.
2020-07-15 21:51:39 -07:00
Hugo van Kemenade
2955bdc676
pre-commit: show diff on failure on CI (#1552)
* pre-commit: --show-diff-on-failure

* pre-commit: --show-diff-on-failure
2020-07-15 14:53:48 -07:00
Steven Maude
5010bb4279
Update curl command to use stable branch (#1543) 2020-07-15 11:36:14 -07:00
dhaug-op
98ac69f04c
Ensure path for finding root is absolute (#1550)
As Path.resolve() is buggy on windows (see https://bugs.python.org/issue38671)
an absolute path is ensured by prepending the Path.cwd()
2020-07-15 08:06:30 -07:00
jtpavlock
8d036ceb3f
Spelling fix in CONTRIBUTING.md (#1547) 2020-07-13 15:27:05 -07:00
Olexiy
cc2facaac6
ISSUE 1533: Fix --config argument description (#1534)
Change --config argument description to "Read configuration from FILE."
The "--config FILE                   Read configuration from FILE path"
2020-07-08 08:51:18 -07:00
Jelle Zijlstra
11f130b7de
add Quora to orgs that use Black (#1532) 2020-07-06 12:45:13 -07:00
Sylvestre Ledru
3209bf7ffa
Mozilla uses black too (#1531) 2020-07-06 07:06:34 -07:00
Cooper Lees
81a093e82a
Add pip install from GitHub command to README.md (#1529)
* Add pip install from GitHub command to README.md
* Make it prettier ...
2020-07-01 08:15:24 -07:00
Lihu Ben-Ezri-Ravin
2471b9256d
Find project root correctly (#1518)
Ensure root dir is a common parent of all inputs
Fixes #1493
2020-06-24 10:09:07 +01:00
Richard Si
f90f50a743
Fix toml hashes and make it clear that only TOML is supported (#1510)
* Fix TOML hashes

* Make it clear that only TOML is supported
2020-06-19 22:29:53 -07:00
Richard Si
d3cc63316b
Add missing contributors to README.md (Thank you everyone!) (#1508)
Black wouldn't be where it is without the countless outside contributions.
So thank you y'all and we will share our appreciation by listing your names
(and emails) in the README.md :D
2020-06-18 11:54:02 +01:00
Richard Si
6ebdc5a644
Fix toml parsing and bump toml from 0.10.0 to 0.10.1 (#1501)
* Bump toml from 0.10.0 to 0.10.1 to fix a bug

* Add tests for TOML parsing and reading

* Fix configuration bug affecting vim plugin

The vim plugin directly calls parse_pyproject and skips the Click processing
, but parse_pyproject assumed that it would only be used before Click processing
and therefore made the config values click friendly. This moves the "make the values
click friendly processing" into read_pyproject_toml which is only called by a Click
callback.

* Please mypy and flake8
2020-06-16 11:58:33 -07:00
Bharat Raghunathan
e4c55b4887
Fix grammatical typos in black_primer and blackd (#1504) 2020-06-16 11:57:33 -07:00
Hugo van Kemenade
ea03b6c5e1
Issue template: Add Python code formatting (#1485)
Makes things a bit more readable
2020-06-15 09:15:11 -07:00
Ralf Schmitt
b2c13de490
Fix find_pyproject_toml type hint (#1495) 2020-06-15 08:56:16 -07:00
Keith Smiley
4ccc8c86f5
Move vim flag cast to outside of get (#1486)
With this config:

```toml
[tool.black]
line-length = 79
```

In neovim, this is loaded as a string which later causes an exception to
be thrown. This makes sure the value is always cast to an int
2020-06-15 08:55:42 -07:00
Adam Williamson
88d12f88a9
expression tests: adjust starred expression for Python 3.9 (#1441) (#1477)
As discussed in #1441, Python 3.9's new parser will not parse
`(*starred)` even using `compile()` with the `PyCF_ONLY_AST`
flag (as `ast.parse()` does), it raises a `SyntaxError`. This
breaks the four tests that use this file with Python 3.9.
Upstream does not consider this to be a bug - see
https://bugs.python.org/issue40848#msg370643 - so we must
adjust the expression. As suggested by @JelleZijlstra, this just
adds a comma, which makes the new parser happy with it (the old
parser is fine with this form also).

Signed-off-by: Adam Williamson <awilliam@redhat.com>
2020-06-03 15:15:54 -07:00
Hugo Barrera
fcf9796106
GitHub action (#1421)
* Implement a re-usable GitHub Action

Implement a GitHub action that can be reused across projects that want
to run black as part of their CI workflows.

* Fix typo in README.md

Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com>

* Use latest Python 3

Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com>
2020-06-01 11:11:49 -07:00
Richard Si
d10f85738d
Convert (most of the) configuration values from pyproject.toml to strings (#1466)
* Convert config values to string

We need to convert all configuration values from the pyproject.toml
file because Click already does value processing and conversion and
it only expects the exact Python type or string. Click doesn't like
the integer 1 as a boolean for example. This also means other
unsupported types like datetime.time will be rejected by Click as
a unvalid value instead of throwing an exception.

We only skip converting objects that are an instance of
collections.abc.Iterable because it's almost impossible to get back
the original iterable of a stringified iterable.

* Move where the conversion happens

Instead of converting the values in the merged 'default_map', I should
convert the values that were read from the 'pyproject.toml' file.

* Change collections.abc.Iterable to (list, dict)

I also moved where the conversion happens... again. I am rather indecisive
if you haven't noticed. It should be better as it takes place in the
parse_pyproject_toml logic where configuration modification already takes
place.

Actually when this PR was first created I had the conversion happen in that
return statement, but the target_version check was complaining about it being
a string. So I moved the conversion after that check, but then Click didn't
like the stringifed list, which led me to check whether the value was an
instance of an Iterable before turning it into a string. And... I forgot that
type checking before conversion would allow it to work before the
target_version check anyway.
2020-06-01 11:00:00 -07:00
Richard Si
34613383ab
Make 'python -m black' work (#1460) 2020-05-26 19:42:07 -07:00
Cooper Lees
91715352cd
Update CONTRIBUTION.md with pre-commit + black-primer instructions (#1459)
* Update CONTRIBUTION with pre-commit + black-primer instructions
- Inform people how to run primer and alter it's config
- Link to main documentation

* Apply suggestions from code review

Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com>
2020-05-24 12:02:52 -07:00