Commit Graph

275 Commits

Author SHA1 Message Date
Łukasz Langa
d240ca25ea Make is_complex_subscript() ignore list literals
This fixes catastrophically quadratic behavior on long lists.
2018-06-09 16:04:09 -07:00
Zsolt Dollenstein
5d0a469e8e Fix string normalization sometimes producing invalid fstrings (#327) 2018-06-09 12:30:49 -07:00
Łukasz Langa
ff2e5dd6e6 18.6b2 2018-06-08 12:07:43 -07:00
Łukasz Langa
8482d38fb4 Return early from comment placement calculation on lines without comments 2018-06-08 12:00:53 -07:00
Loren Carvalho
39e9347687 Add -h as a shortcut for --help (#316) 2018-06-07 19:43:04 -07:00
Zsolt Dollenstein
7f3678885f fix handling of empty triple quoted strings (#314) 2018-06-07 11:41:34 -07:00
Łukasz Langa
489d00ed8f Support pyproject.toml
Fixes #65
2018-06-06 17:02:10 -07:00
Łukasz Langa
92a150d9e4 Fix improper unmodified file caching when -S was used
This will also future-proof the cache to changes to FileMode.
2018-06-06 15:46:30 -07:00
Zsolt Dollenstein
c1a962b885 Don't put a space after * in g = 1, *"x" (#309)
Fixes #305.
2018-06-06 15:44:12 -07:00
Hugo
9e9ee449b4 Link to GitHub + HTTPS + typos (#303)
* Link to GitHub, update 3.6 minor version

* http -> https

* Fix typos

* The Black style for Black, the project, is italics
2018-06-06 09:50:08 -07:00
Łukasz Langa
ed50737290 18.6b1 2018-06-05 09:49:06 -07:00
Peter Bengtsson
aeaaf571ad 🍰 isn't appropriate when it fails, fixes #300 (#301) 2018-06-05 09:35:35 -07:00
Zsolt Dollenstein
61a8d401c7 Print report on stderr.\n\nFixes #299. 2018-06-05 10:38:20 +01:00
Łukasz Langa
f3ea58cbc7 18.6b0
Fixes #289
2018-06-04 21:29:09 -07:00
Łukasz Langa
d638d56e0e Fix unnecessary parentheses when a line contains multiline strings
Fixes #232
2018-06-04 21:27:51 -07:00
Łukasz Langa
23a00f0515 Fix long trivial assignments being wrapped in unnecessary parentheses
Fixes #273
2018-06-04 20:24:50 -07:00
Łukasz Langa
7fc6ce9906 Fix handling of empty files 2018-06-04 19:04:29 -07:00
Łukasz Langa
bbc09a4f01 Consider stars in testlist_star_expr unpacking (because they are)
Fixes #297
2018-06-04 18:42:16 -07:00
Łukasz Langa
728e5a2f1e Properly format unified diff
Previously we weren't using timestamps.
2018-06-04 18:07:03 -07:00
Łukasz Langa
041ec995e5 Nits 2018-06-04 16:57:27 -07:00
Łukasz Langa
ee02ebe3e9 Always show summary of reformatting 2018-06-04 15:53:37 -07:00
Łukasz Langa
ed91dd4de6 Make source handling use sets instead of lists
Also, sort cached file output to be (more) deterministic.
2018-06-04 15:53:37 -07:00
Łukasz Langa
e5452a6b67 Make sure --verbose trumps --quiet
This is so that users can have a --quiet alias in their environment and only
occasionally add --verbose if they are surprised by the result.
2018-06-04 15:53:37 -07:00
Zsolt Dollenstein
00a302560b Preserve line endings when formatting a file in place (#288) 2018-06-04 15:52:06 -07:00
Łukasz Langa
75d2af2e3a Fix missing leading slash due to relative_to() resolution 2018-06-04 12:18:27 -07:00
Łukasz Langa
4c352ad4be Add --verbose and report excluded paths in it, too
Fixes #283
2018-06-04 12:09:18 -07:00
Łukasz Langa
e7b312fb43 [trivial] Simplify mode and write_back calculation in main() 2018-06-04 11:20:15 -07:00
Łukasz Langa
1aa14c5db0 [trivial] Simplify stdin handling 2018-06-04 11:06:38 -07:00
Łukasz Langa
36bed855e1 Introduce "project root" as a concept
This is required for regular expressions in `--include=` and `--exclude=` not
to catch false positives from directories outside of the project.
2018-06-04 11:03:18 -07:00
Łukasz Langa
a80e037a9a Don't over-eagerly make a path absolute if only one passed
If a directory or more than one file is passed, Black nicely shows the relative
paths in output.  Before this change, it showed an absolute path if only
a single file was passed as an argument.  This fixes the inconsistency.
2018-05-31 19:47:24 -07:00
Łukasz Langa
8a82e0bf9a Make empty --include mean "anything goes", simplify gen_python_files_in_dir 2018-05-31 19:40:07 -07:00
Łukasz Langa
435aa7ac4a Reorder command-line options 2018-05-31 19:24:09 -07:00
Łukasz Langa
6fa60ba39d Sort default excludes, include the leading slash 2018-05-31 19:09:51 -07:00
Mika⠙
51756a405c Added --include and --exclude cli options (#281)
These 2 options allow you to pass in regular expressions that determine
whether files/directories are included or excluded in the recursive file
search.

Fixes #270
2018-05-31 17:51:15 -07:00
Łukasz Langa
8ebbd26888 Add --skip-string-normalization
Fixes #118
2018-05-30 12:56:16 -07:00
Łukasz Langa
b2067aabbf 18.5b1 2018-05-29 02:07:09 -07:00
Łukasz Langa
023e61a254 Refactor --pyi and --py36 into FileMode 2018-05-29 01:53:54 -07:00
Łukasz Langa
2057bf6fae Clean up PEP 257 support
I documented the new behavior, added it to the change log, greatly expanded
tests, added support for inner defs and classes, and added Luka to ACKS.

Fixes #196
2018-05-29 00:47:52 -07:00
Luka Sterbic
e818260f1a Class new line between docstrings / vars / methods (#219)
Partially addresses #144
2018-05-28 23:48:59 -07:00
Łukasz Langa
e9a940d69e Remove remains of extra empty lines for flow control statements 2018-05-24 13:32:52 -07:00
Łukasz Langa
bbed104e6d Reword --pyi and --py36 documentation 2018-05-23 11:52:41 -07:00
Carl Meyer
07b1b2f3dd Add --pyi and --py36 flags (#249)
Fixes #244.
2018-05-23 11:01:26 -07:00
Łukasz Langa
658eb7161d Fix unstable formatting on trailers omitted from line splitting with comments
Fixes #238
2018-05-22 00:38:31 -07:00
Łukasz Langa
9a6c88c7f4 Fix invalid code on stars in long from-imports being wrapped in parentheses
Fixes #234
2018-05-21 16:37:29 -07:00
Łukasz Langa
808754af18 Fix optional parentheses being removed within # fmt: off sections
Fixes #224
2018-05-21 15:21:14 -07:00
Łukasz Langa
91de9ea6e3 Fix invalid code in an omitted trailer on large expressions
Fixes #237
2018-05-21 12:29:36 -07:00
Łukasz Langa
c891c65b6b Store grammar pickle caches in CACHE_DIR
Fixes #192

Fixes #203
2018-05-19 14:06:27 -07:00
Łukasz Langa
7395f55564 18.5b0 2018-05-17 17:32:43 -07:00
Łukasz Langa
180bc06ede Don't explode a one-element collection ending with a comma. 2018-05-17 17:32:43 -07:00
Łukasz Langa
87b8df28c4 Fix overly optimistic removal of optional parentheses
The current behavior is explained with much detail in
`can_omit_invisible_parens`.
2018-05-17 17:32:43 -07:00
Łukasz Langa
385e3a1049 Don't allow indexing to trigger omitting optional parentheses 2018-05-17 17:32:43 -07:00
Łukasz Langa
31fbd1982f Avoid treating ellipsis as a dot delimiter 2018-05-17 17:32:43 -07:00
Łukasz Langa
dd4477b701 Always explode data structure literals
Fixes #152
2018-05-17 17:32:43 -07:00
Łukasz Langa
3ad0f5855c Fix multiline strings unnecessarily wrapped in optional parentheses
Fixes #215
2018-05-16 19:19:48 -07:00
Łukasz Langa
8c74d7901f Implement fluent interfaces
Fixes #67
2018-05-16 15:21:40 -07:00
Łukasz Langa
1dadeef47a Consider in, not in, is, is not operators 2018-05-16 14:46:43 -07:00
Łukasz Langa
f24635635e For omitting optional parentheses, ignore delimiters of lower priorities 2018-05-16 14:40:23 -07:00
Łukasz Langa
96e68f0341 Report progress on multiple files incrementally 2018-05-16 11:45:32 -07:00
Łukasz Langa
5a47fd13cc Don't use optional parentheses in unnecessary situations
If an expression starts or ends with a bracket and only contains a single
delimiter, don't wrap it in additional optional parentheses.  We can use the
brackets for the split.

Fixes #177

Fixes #193
2018-05-15 22:13:16 -07:00
Łukasz Langa
8b64e916f6 Simplify is_trivial_* methods 2018-05-15 13:25:00 -07:00
Łukasz Langa
634dc5a20e Warn that right_hand_split() modifies bracket_depth in leaves 2018-05-15 12:31:33 -07:00
Jelle Zijlstra
14ba1bf8b6 Add support for pyi files (#210)
Fixes #207
2018-05-15 15:09:35 -04:00
Łukasz Langa
3eea3aad86 Don't explode trailers that fit in a single line 2018-05-15 01:05:39 -07:00
Łukasz Langa
a677713ebf Re-use indexes of current iteration in comments_after() 2018-05-15 01:05:39 -07:00
Miguel Gaiowski
c667b85a7f Check for broken symlinks before checking file data (#202) 2018-05-15 01:13:48 -04:00
Jelle Zijlstra
21c8dc85f5 Remove u prefix if unicode_literals is present (#199) 2018-05-09 18:26:41 -04:00
Łukasz Langa
90e14e8b61 Don't make parentheses invisible around yield expressions 2018-05-08 23:31:41 -07:00
Łukasz Langa
7595dabb43 Fix docstring of is_vararg 2018-05-08 23:03:59 -07:00
Łukasz Langa
1b51e2c078 Automatic management of parentheses in elif, too 2018-05-08 21:57:09 -07:00
Łukasz Langa
c9b73b99ac Support nested lambdas in BracketTracker 2018-05-08 21:57:09 -07:00
Łukasz Langa
793450aeb0 Automatic management of parentheses in assignments
Fixes #140

Note: this is an evolution but the end result needs to be different.  See
cantfit.py for some good examples on bad formatting caused by this change.
2018-05-08 21:57:09 -07:00
Łukasz Langa
a68dd928e6 Fix docstrings of visit_stmt and normalize_invisible_parens 2018-05-08 21:57:09 -07:00
Łukasz Langa
7811f957f3 Delimit multiline expressions according to math operator priority
Fixes #148
2018-05-08 21:57:09 -07:00
Łukasz Langa
1747c388bb Discover whether a file is Python 3.6+ also by stars in calls
Fixes a pathological situation where if a function signature used a trailing
comma but was later reformatted to a single line (with the trailing comma
removed), Black would change its mind whether a file is Python
3.6-compatible between runs.
2018-05-08 21:57:09 -07:00
Łukasz Langa
0967dfcbeb Don't fail the entire right_hand_split if an optional split failed
Fixes splitting long import lines with only a single name.
2018-05-08 10:11:16 -07:00
Łukasz Langa
27a36671fe Diff version 2018-05-07 11:36:51 -07:00
Łukasz Langa
cfb003f51c Make parentheses invisible recursively in atoms
This fixes non-deterministic formatting when multiple pairs of removable
parentheses are used.

Fixes #183
2018-05-07 11:13:55 -07:00
Łukasz Langa
dc0c14240e Don't leave invalid trailing comma on imports
Fixes #185
2018-05-07 10:40:18 -07:00
Łukasz Langa
b1cd59f267 Formatting nits 2018-05-07 10:22:43 -07:00
snlkapil
6dbb657681 Removing empty parentheses after class name (#180) 2018-05-07 10:15:24 -07:00
Christian Heimes
2d9eaafa97 Output something when no files are reformatted (#190)
Just executing ``black`` without any argument does not print any message
to stdout or stderr. It's rather confusing, because the user doesn't
know what happened.

In ``len(sources) == 0`` case, black now prints ``No paths given. Nothing to
do``.

Signed-off-by: Christian Heimes <christian@python.org>
2018-05-07 10:12:47 -07:00
Christian Heimes
adf4ebd2d9 Remove unnecessary shebang lines (#189)
Since black.py is not marked as executable, the shebang in black.py serves
no purpose. black should be invoked through its entry point any way.

token.py is an internal module without a __name__ == '__main__' block or
other executable code. It contains just list of constants and small
helper functions.

Signed-off-by: Christian Heimes <christian@python.org>
2018-05-04 13:52:17 -07:00
Zsolt Dollenstein
9f096d5536 Format subscriptions in a PEP-8 compliant way (#178)
Fixes #157
2018-04-30 22:49:30 -07:00
Łukasz Langa
0f3ecb7e50 18.4a4 hotfix: don't populate the cache on --check
Fixes #175
2018-04-27 14:04:46 -07:00
Łukasz Langa
b62bd4de75 Add explode_split to documentation 2018-04-24 14:27:21 -07:00
Łukasz Langa
1445bea97e 18.4a3 2018-04-24 13:52:12 -07:00
Łukasz Langa
09f5ee3a19 Split imports like isort
Fixes #127

Partially addresses #152
2018-04-24 13:44:28 -07:00
Łukasz Langa
b250aed47b Do not enforce empty lines after control flow statements
Fixes #90
2018-04-24 12:42:46 -07:00
Łukasz Langa
9d671bdbe1 Split ternary expressions
Fixes #141
2018-04-24 12:21:56 -07:00
Jonas Obrist
92957a41e3 Make cache work with non-default line lenghts (#163) 2018-04-24 11:56:50 -07:00
Łukasz Langa
52fda8b0e9 Support sticky standalone comments (comments preceding defs, classes, and decorators)
Fixes #56
Fixes #154
2018-04-24 11:50:31 -07:00
Vishwas B Sharma
29e97d1d4a [#154] Handle comments between decorators properly (#166) 2018-04-24 09:38:12 -07:00
Łukasz Langa
54d707e10a Allow standalone comments to close code blocks
Fixes #16
Fixes #32
2018-04-23 16:17:12 -07:00
Łukasz Langa
82198030ee Remove nonsensical grammar from blib2to3 2018-04-23 14:24:14 -07:00
Łukasz Langa
06e95b1e9b Show full path on diffs
Fixes #130
2018-04-23 12:00:03 -07:00
Łukasz Langa
3ddf73337d Refactor reformat_one and schedule_formatting to decrease state 2018-04-23 11:48:01 -07:00
Vishwas B Sharma
5e1e1a1820 [#149] Make check and diff not mutually exclusive (#161)
Fixes #149.
2018-04-23 11:23:11 -07:00
Łukasz Langa
d73166c42b Move delimiter token skipping to BracketTracker
Also, added lambda argument delimiter skipping.

Fixes #133
2018-04-21 15:08:36 -07:00
Zsolt Dollenstein
b5b658da06 Skip handling signals on event loops that don't support it (#156) 2018-04-21 12:16:02 -07:00
Łukasz Langa
2446941824 Remove dead code 2018-04-19 00:09:13 -07:00
Łukasz Langa
aa5629204c Docstring for max_delimiter_priority_in_atom() 2018-04-18 18:13:36 -07:00
Łukasz Langa
15d5e36ea3 Simplify single-file vs. multi-file modes 2018-04-18 17:59:28 -07:00
Łukasz Langa
8429f3a63b Revert format_file_in_place() and format_stdin_to_stdout() to return bools
`Changed.CACHED` is meaningless for those two functions.
2018-04-18 17:46:03 -07:00
Jonas Obrist
639b62dcd3 Added caching (#136)
Black will cache already formatted files using their file size and
modification timestamp. The cache is per-user and will always be used
unless Black is used with --diff or with code provided via standard
input.
2018-04-18 17:27:07 -07:00
Zsolt Dollenstein
661908cd02 use STARS instead of STAR | DOUBLESTAR 2018-04-17 20:35:59 +01:00
Zsolt Dollenstein
5192ed484b Parse complex expressions in parameters after * and ** 2018-04-17 20:34:02 +01:00
Łukasz Langa
a764f1bb3b Generalize star expression handling
Fixes #132
2018-04-16 01:34:15 -07:00
Łukasz Langa
f294cc272c 18.4a2 2018-04-13 12:06:40 -07:00
Zsolt Dollenstein
2104b7cdcb Handle unnecessarily escaped strings (#128) 2018-04-13 11:31:23 -07:00
Zsolt Dollenstein
8e7848c63e Open temporary files with utf-8 encoding (#126)
This is not the default on Windows.
2018-04-12 11:19:21 -07:00
Łukasz Langa
19d69b34e5 Fix placement of dictionary unpacking inside dict literals
Fixes #111
2018-04-11 23:22:22 -07:00
Łukasz Langa
39a5fe7a55 18.4a1 2018-04-11 19:22:16 -07:00
Łukasz Langa
cf8e998f46 Don't omit escaping the second consecutive quote
This would produce invalid code for strings like `"x = ''; y = \"\""`.
2018-04-11 16:25:47 -07:00
Łukasz Langa
bc6b912fcb Fix an embarrassing UnboundLocalError 2018-04-11 15:49:14 -07:00
Łukasz Langa
cb5aadad74 Automatic parentheses management
Fixes #4
2018-04-11 01:36:18 -07:00
Zsolt Dollenstein
ecdbf085a7 Add support for all valid string literals (#115) 2018-04-09 14:36:40 -07:00
Zsolt Dollenstein
2e0bb0fa9f Handle arbitrary number of backslashes during string normalization (#110) 2018-04-05 02:28:46 -07:00
Łukasz Langa
30d921f74c Simplify delimiter logic 2018-04-04 16:33:10 -07:00
Zsolt Dollenstein
51b3b2624d Handle backslashes in raw strings while normalizing (#105)
In raw strings, a single backslash means a literal backslash. It is also used to escape quotes if it precedes them. This means it is impossible to change the quote type for strings that contain an unescaped version of the other quote type.
Fixes #100
2018-04-04 13:20:46 -07:00
Ivan Katanić
1d3d743dfb Fix --check for multiple files (#101) 2018-04-04 09:53:34 -07:00
Łukasz Langa
21c32d80b6 Add --quiet
Fixes #78
2018-04-02 23:17:39 -07:00
Łukasz Langa
65c52a655f 18.4a0 2018-04-01 00:30:45 -07:00
Łukasz Langa
2f260514f6 Ignore # fmt: off as inline comment
Black cannot currently support this form due to its generator-based nature.
This is mostly a problem for existing `# yapf: disable` usage as trailing
comment.

Fixes #95
2018-04-01 00:08:09 -07:00
Łukasz Langa
4787294622 Don't insert trailing commas after standalone comments 2018-03-31 23:32:11 -07:00
Łukasz Langa
a20a3eeb0f Support --diff for both files and stdin
Fixes #87
2018-03-31 15:46:09 -07:00
Zsolt Dollenstein
a42aef7806 Describe how string literals are handled (#96) 2018-03-31 14:42:48 -07:00
Mika⠙
3455389e48 Lines now break before all delimiters (#94)
The default behaviour is that now all lines break *before* delimiters,
instead of afterwards. The special cases for this are commas and
behaviour around args.

Resolves #73
2018-03-31 11:52:11 -07:00
Zsolt Dollenstein
80bd2b3134 Normalize string quotes (#75)
* Normalize string quotes

Convert single-quoted strings to double-quoted. Convert triple single-quoted strings to triple double-quoted. Do not touch any strings where conversion would increase the number of backslashes.

Fixes #51.

* reformat Black itself
2018-03-31 11:21:25 -07:00
Łukasz Langa
ba61bfe386 Graceful shutdown in case of cancellation 2018-03-30 19:38:08 -07:00
Łukasz Langa
c55d08d0b9 Remove standalone comment hacks
Now Black properly splits standalone comments within bracketed expressions.
They are treated as another type of split instead of being bolted on with
whitespace prefixes.

A related fix: now multiple comments might appear after a given leaf.

Fixes #22
2018-03-29 21:10:00 -07:00
Josh Holland
3ee9ebb091 Fix --check with multiple files (#88)
Passing multiple files to --check would previously result in the report
being printed as if the files had been written to.
2018-03-29 15:21:18 -07:00
Łukasz Langa
44a2351731 Use imperative language in all docstrings 2018-03-28 19:31:53 -07:00
Łukasz Langa
7f7b31058a More minor documentation-related changes 2018-03-28 19:03:16 -07:00
Łukasz Langa
d01460d939 Auto-generated documentation-related fixes 2018-03-28 17:43:18 -07:00
Łukasz Langa
e5f8251704 Allow up to two empty lines on module level and single empty lines otherwise
Fixes #74
2018-03-26 22:55:56 -07:00
Łukasz Langa
c5fd578279 18.3a4 2018-03-26 00:57:02 -07:00
Łukasz Langa
0677a53937 Implement # fmt: off and # fmt: on
Fixes #5
2018-03-23 17:16:08 -07:00
Łukasz Langa
7e1c5b2ba6 Introduce DebugVisitor.show() + tests 2018-03-23 16:08:05 -07:00
Łukasz Langa
475179a53a Omit extra space in Sphinx auto-attribute comments
This feature of Sphinx is described in:
http://www.sphinx-doc.org/en/stable/ext/autodoc.html#directive-autoattribute

Fixes #68
2018-03-23 08:56:55 -07:00
Łukasz Langa
cf6f577928 Properle space complex expressions in default values of typed arguments
Fixes #60
2018-03-22 23:52:21 -07:00
Łukasz Langa
7bd6f3cb2f Ignore typing error around Node/Leaf 2018-03-22 23:22:46 -07:00
Łukasz Langa
6316e293ac Automatic detection of deprecated Python 2 forms of print and exec
Note: if those are handled, you can't use --safe because this check is using
Python 3.6+ builtin AST.

Fixes #49
2018-03-22 23:19:23 -07:00
Łukasz Langa
8de552eb4f Only return exit code 1 when --check is used
Also, output less confusing messages in --check.

Fixes #50
2018-03-22 17:33:44 -07:00
Łukasz Langa
7914a5b0a2 Mention delimiter_split() in CannotSplit docstring 2018-03-22 17:15:28 -07:00
Łukasz Langa
a970a205bc Don't remove the single trailing comma from square bracket indexing
Fixes #59
2018-03-22 16:33:50 -07:00
Łukasz Langa
df7aacb43e Omit extra space in kwarg unpacking if it's an argument
Fixes #46
2018-03-21 18:35:25 -07:00
Łukasz Langa
92b377556e Don't omit whitespace when the factor is not a math operator
Fixes #55
2018-03-21 18:34:16 -07:00
Łukasz Langa
1b696555e1 18.3a3 2018-03-20 19:05:51 -07:00
Łukasz Langa
b1a7600b0a Treat comments less magically 2018-03-20 18:55:19 -07:00
Łukasz Langa
d1e0d79e38 Don't write back stdin to stdout when --check is passed 2018-03-20 18:54:55 -07:00