Giter Site home page Giter Site logo

psf / black Goto Github PK

View Code? Open in Web Editor NEW
37.4K 37.4K 2.4K 6.57 MB

The uncompromising Python code formatter

Home Page: https://black.readthedocs.io/en/stable/

License: MIT License

Python 99.74% Dockerfile 0.02% Jupyter Notebook 0.05% Vim Script 0.19%
autopep8 code codeformatter formatter gofmt hacktoberfest pre-commit-hook python yapf

black's People

Contributors

alexwaygood avatar ambv avatar asottile avatar bbugyi200 avatar cooperlees avatar dependabot[bot] avatar felix-hilden avatar hauntsaninja avatar henriholopainen avatar hugovk avatar ichard26 avatar isidentical avatar jackenmen avatar jayaddison avatar jdufresne avatar jellezijlstra avatar jgirardet avatar jpy-git avatar marcogorelli avatar msullivan avatar nipunn1313 avatar pre-commit-ci[bot] avatar rdrll avatar redguy12 avatar saroad2 avatar uriyyo avatar willingc avatar yilei avatar zac-hd avatar zsol avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

black's Issues

"Black produced different code on the second pass of the formatter"

$ cat setup.py
#!/usr/bin/env python
from setuptools import find_packages, setup


setup(
    name="pylast",
    version="2.2.0.dev0",
    author="Amr Hassan <[email protected]> and Contributors",
    install_requires=['six'],
    tests_require=['mock', 'pytest', 'coverage', 'pycodestyle', 'pyyaml',
                   'pyflakes', 'flaky'],
    description="A Python interface to Last.fm and Libre.fm",
    author_email="[email protected]",
    url="https://github.com/pylast/pylast",
    classifiers=[
        "Development Status :: 5 - Production/Stable",
        "License :: OSI Approved :: Apache Software License",
        "Topic :: Internet",
        "Topic :: Multimedia :: Sound/Audio",
        "Topic :: Software Development :: Libraries :: Python Modules",
        "Programming Language :: Python :: 2",
        "Programming Language :: Python :: 2.7",
        "Programming Language :: Python :: 3",
        "Programming Language :: Python :: 3.4",
        "Programming Language :: Python :: 3.5",
        "Programming Language :: Python :: 3.6",
        "Programming Language :: Python :: Implementation :: CPython",
        "Programming Language :: Python :: Implementation :: PyPy",
    ],
    python_requires='>=2.7.10, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*',
    keywords=["Last.fm", "music", "scrobble", "scrobbling"],
    packages=find_packages(exclude=('tests*',)),
    license="Apache2"
)

# End of file
$ black setup.py
error: cannot format setup.py: INTERNAL ERROR: Black produced different code on the second pass of the formatter.  Please report a bug on https://github.com/ambv/black/issues.  This diff might be helpful: /var/folders/kt/j77sf4_n6fnbx6pg199rbx700000gn/T/blk_d33mlhef.log
$ cat /var/folders/kt/j77sf4_n6fnbx6pg199rbx700000gn/T/blk_d33mlhef.log
--- source
+++ first pass
@@ -1,16 +1,16 @@
 #!/usr/bin/env python
 from setuptools import find_packages, setup
-
 
 setup(
     name="pylast",
     version="2.2.0.dev0",
     author="Amr Hassan <[email protected]> and Contributors",
     install_requires=['six'],
-    tests_require=['mock', 'pytest', 'coverage', 'pycodestyle', 'pyyaml',
-                   'pyflakes', 'flaky'],
+    tests_require=[
+        'mock', 'pytest', 'coverage', 'pycodestyle', 'pyyaml', 'pyflakes', 'flaky'
+    ],
     description="A Python interface to Last.fm and Libre.fm",
     author_email="[email protected]",
     url="https://github.com/pylast/pylast",
     classifiers=[
         "Development Status :: 5 - Production/Stable",
@@ -28,10 +28,9 @@
         "Programming Language :: Python :: Implementation :: PyPy",
     ],
     python_requires='>=2.7.10, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*',
     keywords=["Last.fm", "music", "scrobble", "scrobbling"],
     packages=find_packages(exclude=('tests*',)),
-    license="Apache2"
+    license="Apache2",
 )
-
 # End of file
 

--- first pass
+++ second pass
@@ -29,8 +29,7 @@
     ],
     python_requires='>=2.7.10, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*',
     keywords=["Last.fm", "music", "scrobble", "scrobbling"],
     packages=find_packages(exclude=('tests*',)),
     license="Apache2",
-)
-# End of file
+)  # End of file
 

Link to VS Code extension

@ambv I've been waiting a long time for something like Black to come to Python land, and so to lend my support I just put together a VS Code extension for Black, as it's my editor of choice:

https://marketplace.visualstudio.com/items?itemName=joslarson.black-vscode

It depends on the upcoming piping feature to work, so it won't be "stable" until that lands, but I'm wondering, once it does, what you think about linking to it in the README under the "Editor integration" heading to help other VS Code users find it.

Thanks for your great work on this 👍

Document standard input/output handling

Hi!

Thanks for the great package!

I'm interested to implement Emacs text editor integration for black.

Is it possible to pass file content to standard input followed by EOF and get the formatted code at the standard output?

Instead of replacing file content in place. This will allow preserving undo history of the editor buffer.

Regards, Artem.

black introduces PEP 8 violation: operators should go at beginning of lines when possible

 @contextmanager
 def ki_manager(deliver_cb, restrict_keyboard_interrupt_to_checkpoints):
     if (
-        threading.current_thread() != threading.main_thread()
-        or signal.getsignal(signal.SIGINT) != signal.default_int_handler
+        threading.current_thread() != threading.main_thread() or
+        signal.getsignal(signal.SIGINT) != signal.default_int_handler
     ):

Ok, technically PEP 8 waffles a bit b/c it used to recommend the other version. But these days it says that operator-at-beginning "usually results in more readable code" and "For new code [operator-at-beginning] style is suggested".

Introduces 'W503 line break before binary operator' and 'E303 too many blank lines (3)'

Operating system: macOS
Python version: 3.6.4
Black version: latest master: 10d8976
Does also happen on master: yes

To answer the last question, follow these steps:

  • create a new virtualenv (make sure it's the same Python version);
  • clone this repository;
  • run pip install -e .;
  • make sure it's sane by running python setup.py test; and
  • run black like you did last time.
⌂149% [hugo:/tmp/black] [black] % git clone https://github.com/ambv/black/
Cloning into 'black'...
remote: Counting objects: 237, done.
remote: Compressing objects: 100% (39/39), done.
remote: Total 237 (delta 0), reused 37 (delta 0), pack-reused 198
Receiving objects: 100% (237/237), 130.37 KiB | 604.00 KiB/s, done.
Resolving deltas: 100% (130/130), done.
⌂136% [hugo:/tmp/black] [black] % cd black
⌂120% [hugo:/tmp/black/black] [black] master 2 ± pip install -e .
Obtaining file:///private/tmp/black/black
Collecting click (from black==18.3a2)
  Downloading click-6.7-py2.py3-none-any.whl (71kB)
    100% |████████████████████████████████| 71kB 2.2MB/s
Collecting attrs (from black==18.3a2)
  Using cached attrs-17.4.0-py2.py3-none-any.whl
Installing collected packages: click, attrs, black
  Running setup.py develop for black
Successfully installed attrs-17.4.0 black click-6.7
⌂113% [hugo:/tmp/black/black] [black] master* 3s ± wget https://raw.githubusercontent.com/pylast/pylast/master/pylast/__init__.py
--2018-03-20 12:31:21--  https://raw.githubusercontent.com/pylast/pylast/master/pylast/__init__.py
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.84.133
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.84.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 84160 (82K) [text/plain]
Saving to: ‘__init__.py’

__init__.py                             100%[============================================================================>]  82.19K  --.-KB/s    in 0.01s

2018-03-20 12:31:21 (5.42 MB/s) - ‘__init__.py’ saved [84160/84160]

⌂116% [hugo:/tmp/black/black] [black] master* ± pycodestyle --ignore E501 __init__.py
⌂111% [hugo:/tmp/black/black] [black] master* ± black __init__.py
reformatted __init__.py
⌂104% [hugo:/tmp/black/black] [black] master* 3s 1 ± pycodestyle --ignore E501 __init__.py
__init__.py:170:13: W503 line break before binary operator
__init__.py:171:13: W503 line break before binary operator
__init__.py:1350:17: W503 line break before binary operator
__init__.py:2477:1: E303 too many blank lines (3)

Here's the first two W503s:

-        if ((self.api_key and self.api_secret) and not self.session_key and
-           (self.username and self.password_hash)):
+        if (
+            (self.api_key and self.api_secret)
+            and not self.session_key
+            and (self.username and self.password_hash)
+        ):

The other W503:

     def _get_children_by_tag_name(self, node, tag_name):
         for child in node.childNodes:
-            if (child.nodeType == child.ELEMENT_NODE and
-               (tag_name == '*' or child.tagName == tag_name)):
+            if (
+                child.nodeType == child.ELEMENT_NODE
+                and (tag_name == '*' or child.tagName == tag_name)
+            ):
                 yield child

And the E303:

 def _unescape_htmlentity(string):
-
     # string = _unicode(string)
-
     mapping = htmlentitydefs.name2codepoint
     for key in mapping:
         string = string.replace("&%s;" % key, unichr(mapping[key]))
-
     return string


+
 # End of file

Opinions: do not remove logical block new line inside of functions

Just ran black on one of the repos I have. While many things are fine or OK, I have several business logic files where only new lines were removed between logical blocks inside of functions.

Even by looking at black's own codebase it would benefit from newline separation inside of some functions, like this one: https://github.com/ambv/black/blob/master/black.py#L1188

Wall of text is not making something more readable, especially for people with sight disabilities. But maybe I'm missing some trend in Python community, so would like to hear others' opinions on this.

Numpy-style indexing gets "mushed" (i.e. loses a space)

For the repro file its_science.py

def first_column1d(values):
    """First column, as a 1D array."""
    return values[:, 0]


def first_column2d(values):
    """First column, but still a 2D array."""
    return values[:, 0:1]

Running black (installed from HEAD as in #30)

$ venv-HEAD/bin/black its_science.py
reformatted its_science.py

changes the "exotic" slice argument:

diff --git a/its_science.py b/its_science.py
index f3c4a8b..3a5dba0 100644
--- a/its_science.py
+++ b/its_science.py
@@ -5,4 +5,4 @@ def first_column1d(values):
 
 def first_column2d(values):
     """First column, but still a 2D array."""
-    return values[:, 0:1]
+    return values[:,0:1]

PS @ambv This is my fourth and last issue (for now) and it's just "wrong" things I encountered when running this on a large codebase of mine. I am SOOOOO happy you have created black and look forward to evangelizing it in the community.

Can't parse module names with async in them?

Minimal repro:

from util.async import (abc, foo, bar)

error: cannot format /tmp/foo.py: Cannot parse: 1:10: from util.async import (abc, foo, bar)

This is with python3.6 + master

Cannot parse dict comprehension with negative number

Operating system: MacOS 10.12.6
Python version: 3.6
Black version: 18.3a3
Does also happen on master: yes

{a: b * -2 for a, b in dictionary.items()}
becomes
{a: b * -2for a, b in dictionary.items()}

which, surprisingly to me, doesn't break, but is clearly wrong.

{a: b * 2 for a, b in dictionary.items()}
is not affected by this issue (black leaves it alone.)

What about comments before functions?

i don't know whether black handles this phenomenon purposely this way. i usually put annotations regarding a specific function in general right before it's definition:

# foobar functions


def bar() -> None:
    pass

# TODO add type annotations
def foo():
    pass

black adds two lines between the annotation and a specific function which looks like general, rather module oriented comments or 'section headings' i usually use as well:

# foobar functions


def bar() -> None:
    pass


# TODO add type annotations


def foo():
    pass

are there rationales i'm not aware of that lead to this transformation or could this behaviour get changed?

edit: interestingly the snippets in the README also contain such style.

attrs<17.3.0 does not provide dataclass

Operating system: Linux
Python version: 3.6.4
Black version: 18.3a3
Does also happen on master: N/A

I installed black install an existing project that makes use of attrs via hypothesis. Since black doesn't require a specific version of attrs, the existing installation was used causing this issue. In order to fix the issue, I installed the latest version of attrs==17.4.0. The old version was 17.2.0, which doesn't provide attr.dataclass. Looks like attr.dataclass was introduced in version 17.3.0.

Regarding if this issue occurs on master: not really. Installing black by itself into a clean virtualenv doesn't cause any problems, since the latest version of attrs is used.

Solution may be to pin the dependency at:

install_requires=['click', 'attrs>17.2.0'],

in https://github.com/ambv/black/blob/1b696555e18afdfee3ca45526370de1af2cd961d/setup.py#L43

Merge implicitly concatenated string literals that fit on one line

Black could make single-line strings over multiple lines (i.e. a number of single quotes strings on multiple lines surrounded by parentheses) more efficient, by resizing them to the full length of the line.

Even if that was overreach, there's a peculiar situation where you end up with multiple strings on the same line, like below:

-        warnings.warn('Dataset.sel_points is deprecated: use Dataset.sel()'
-                      'instead.', DeprecationWarning, stacklevel=2)
-
+        warnings.warn(
+            'Dataset.sel_points is deprecated: use Dataset.sel()' 'instead.',
+            DeprecationWarning,
+            stacklevel=2,
+        )

Certain kwargs get a space inserted before the equals sign

For example if the empty tuple is a default it gets a space

$ cat kwarg_change.py
def example(a, b=()):
    return a + b
$ venv/bin/black kwarg_change.py
reformatted kwarg_change.py
$ cat kwarg_change.py
def example(a, b= ()):
    return a + b

but if True if the default, no change is needed:

$ cat kwarg_same.py
def example(a, b, c=True):
    if c:
        return a

    else:
        return b
$ venv/bin/black kwarg_same.py
kwarg_same.py already well formatted, good job.

Environment info:

$ venv/bin/pip show black
Name: black
Version: 18.3a0
Summary: The uncompromising code formatter.
Home-page: https://github.com/ambv/black
Author: Łukasz Langa
Author-email: [email protected]
License: MIT
Location: .../venv/lib/python3.6/site-packages
Requires: click, attrs

relative imports convert `. .` to `..`

Excited about the effort - thanks for kicking this off! Hopefully of what yapf once dreamed

One quick issue when scanning the output: is this intended?

# existing
from ..convert import from_iris

# black
from . .convert import from_iris

Black should not write to the file directly

Writing to a file directly has all sorts of drawback:

  • A file may not be a real file (it may be something faking to be a file to make it readable)
  • You may not have the permission to write the file right now.
  • Something else might be writing on the file.
  • IDE integration might want to use the output of the tool to do something before applying the changes.
  • You have no easy way to make a diff of the changes.

Black should output the changes on stdout by default.

If you want the changes to be applied, you do: black file.py > file.py

If you want the changed to be able to be applied implicitly, you can add an option such as --in-place. This is what sed or gofmt do.

Alternatively, you can specify more write modes, life cargo does: https://github.com/rust-lang-nursery/rustfmt

`black` mishandles functions that end in a comment

For the repro file too_many_returns.py, I have a pylint: disable / pylint: enable at the beginning and end of a function (but it is within the scope of the function):

"""``black`` issue repro."""


def squared(value):
    """Just squares a number."""
    # pylint: disable=too-many-return-statements
    if value == 1:
        return 1

    elif value == 2:
        return 4

    elif value == 3:
        return 9

    elif value == 4:
        return 16

    elif value == 5:
        return 25

    elif value == 6:
        return 36

    elif value == 7:
        return 49

    elif value == 8:
        return 64

    elif value == 9:
        return 81

    elif value == 10:
        return 100

    else:
        return value * value
    # pylint: enable=too-many-return-statements


def cubed(value):
    """Just cubes a number."""
    return value * squared(value)

Running black (installed from HEAD as in #30) does two "strange" things:

  • Moves the # pylint: enable comment to just before the next function
  • Adds 2 empty lines in between the two functions (so there is now 4, instead of 2)
$ venv-HEAD/bin/black too_many_returns.py
reformatted too_many_returns.py

See the diff

diff --git a/too_many_returns.py b/too_many_returns.py
index b7251d5..3b65b6c 100644
--- a/too_many_returns.py
+++ b/too_many_returns.py
@@ -36,9 +36,11 @@ def squared(value):
 
     else:
         return value * value
-    # pylint: enable=too-many-return-statements
 
 
+
+
+# pylint: enable=too-many-return-statements
 def cubed(value):
     """Just cubes a number."""
     return value * squared(value)

I realize the pylint: enable... at the end isn't needed, since the disable is scoped to the function, but I figured you'd still want to see this surprising reformatted result.

Black installs on Python 3.5 and older from the wheel

Howdy! Sorry you're having trouble. To expedite your experience,
provide some basics for me:

Operating system: ubuntu16.04
Python version: Python 3.5.2
Black version:
Does also happen on master:

after I install black by using pip3 install black ,
and execute command black, error occur.


root@leguproject001:~/Pocsuite# black
Traceback (most recent call last):
File "/usr/local/bin/black", line 7, in
from black import main
File "/usr/local/lib/python3.5/dist-packages/black.py", line 72
sources: List[Path] = []
^
SyntaxError: invalid syntax

Document missing trailing comma on function calls with unpacking on non-3.6 files

Howdy! Sorry you're having trouble. To expedite your experience,
provide some basics for me:

Operating system: Mac OS 10.13.3
Python version: 3.6.4
Black version: 18.3a2
Does also happen on master: Yes

To answer the last question, follow these steps:

  • create a new virtualenv (make sure it's the same Python version);
  • clone this repository;
  • run pip install -e .;
  • make sure it's sane by running python setup.py test; and
  • run black like you did last time.

Hi, and a huge thank you for this project!
I think I might have found a glitch in how function calls are formatted.

func_1(
    first_argument_with_a_very_long_name,
    second_argument_with_a_very_long_name,
    third_argument_with_a_very_long_name,
)
func_2(
    *first_argument_with_a_very_long_name,
    second_argument_with_a_very_long_name,
    third_argument_with_a_very_long_name
)

When an argument has the unpacking notation, black removes the trailing comma from the last one.
Is this intended behaviour?

Thanks again!

Black introduces E302 with a Comment at Line 0 and no Imports

Operating system: Debian 9
Python version: 3.6.4
Black version: black.py, version 18.3a2

Black converts this:

# License/Header comment

def includeme(config):
    pass

into this:

# License/Header comment
def includeme(config):
    pass

which causes this:

./compute/storage/__init__.py:14:1: E302 expected 2 blank lines, found 0

Support for tab indentation

Black always output 4 spaces for indentation.

It would be nice to have a --use-tabs or similar to tell it to output tabs instead. By manually switching the ' '*self.depth into '\t'*self.depth in the Line.str() method it seems to support this rather nicely.

I'm not sure if there are other edge cases to consider (i see another ' ' literal in the code but i don't understand when it applies)

Would something like this be considered ?

Standalone comments inside nested bracketed expressions block the expression from wrapping

This is... not what I expected black to do :-)

     # Capture each of the exceptions in the MultiError along with each of their causes and contexts
     if isinstance(exc_value, MultiError):
         embedded = []
         for exc in exc_value.exceptions:
             if exc not in _seen:
                 embedded.append(
-                    traceback.TracebackException.from_exception(
-                        exc,
-                        limit=limit,
-                        lookup_lines=lookup_lines,
-                        capture_locals=capture_locals,
-                        # copy the set of _seen exceptions so that duplicates
-                        # shared between sub-exceptions are not omitted
-                        _seen=set(_seen)
-                    )
+                    traceback.TracebackException.from_exception(exc, limit=limit, lookup_lines=lookup_lines, capture_locals=capture_locals, _seen=set(_seen))
+                    # copy the set of _seen exceptions so that duplicates
+                    # shared between sub-exceptions are not omitted
                 )

Document that W503 generated by pycodestyle is not PEP 8-compliant

Not sure if feature or bug.

 $ black --version
black, version 18.3a3
 $ flake8 --version
3.5.0 (mccabe: 0.6.1, pycodestyle: 2.3.1, pyflakes: 1.6.0) CPython 3.6.2 on Linux
 $ cat test.py
def meh(eh):
    if ("a" not in eh or ("b" not in eh and "c" not in eh and "d" not in eh) or "e" not in eh):
        return True
 $ black .    
reformatted test.py
 $ cat test.py
def meh(eh):
    if (
        "a" not in eh
        or ("b" not in eh and "c" not in eh and "d" not in eh)
        or "e" not in eh
    ):
        return True
 $ flake8 --select=W
./test.py:4:9: W503 line break before binary operator
./test.py:5:9: W503 line break before binary operator
 $ 

Adds E251 unexpected space after equals

macOS High Sierra
Python 3.6.4
black==18.3a0

# existing
def __init__(params={}):
    pass

#black
def __init__(params= {}):
    pass
# existing
$ pycodestyle 1.py

# black
$ pycodestyle 2.py
2.py:1:21: E251 unexpected spaces around keyword / parameter equals

Black puts a space before exec() (E211)

Operating system: Debian 9
Python version: 3.6.4
Black version: 18.3a3

Black converts the following code:

exec('import os')

into:

exec ('import os')

which produces this flake error:

setup.py:22:9: E211 whitespace before '('

"Black produced different code on the second pass of the formatter" (3)

Using current master, fece8ae.

--- source
+++ first pass
@@ -1,6 +1,6 @@
-#
+# 
 # The Python Imaging Library.
 # $Id$
 #
 # Basic McIdas support for PIL
 #
@@ -13,39 +13,36 @@
 # Copyright (c) Secret Labs AB 1997.
 # Copyright (c) Fredrik Lundh 1997.
 #
 # See the README file for information on usage and redistribution.
 #
-
 import struct
 from . import Image, ImageFile
 
 __version__ = "0.2"
 
 
 def _accept(s):
     return s[:8] == b"\x00\x00\x00\x00\x00\x00\x00\x04"
 
 
+
+
 ##
 # Image plugin for McIdas area images.
-
 class McIdasImageFile(ImageFile.ImageFile):
-
     format = "MCIDAS"
     format_description = "McIdas area file"
 
     def _open(self):
-
         # parse area file directory
         s = self.fp.read(256)
         if not _accept(s) or len(s) != 256:
             raise SyntaxError("not an McIdas area file")
 
         self.area_descriptor_raw = s
         self.area_descriptor = w = [0] + list(struct.unpack("!64i", s))
-
         # get mode
         if w[11] == 1:
             mode = rawmode = "L"
         elif w[11] == 2:
             # FIXME: add memory map support
@@ -58,19 +55,15 @@
         else:
             raise SyntaxError("unsupported McIdas format")
 
         self.mode = mode
         self.size = w[10], w[9]
-
         offset = w[34] + w[15]
-        stride = w[15] + w[10]*w[11]*w[14]
-
+        stride = w[15] + w[10] * w[11] * w[14]
         self.tile = [("raw", (0, 0) + self.size, offset, (rawmode, stride, 1))]
 
 
 # --------------------------------------------------------------------
 # registry
-
 Image.register_open(McIdasImageFile.format, McIdasImageFile, _accept)
-
 # no default extension
 

--- first pass
+++ second pass
@@ -62,8 +62,9 @@
         self.tile = [("raw", (0, 0) + self.size, offset, (rawmode, stride, 1))]
 
 
 # --------------------------------------------------------------------
 # registry
-Image.register_open(McIdasImageFile.format, McIdasImageFile, _accept)
-# no default extension
+Image.register_open(
+    McIdasImageFile.format, McIdasImageFile, _accept
+)  # no default extension
 

"Black produced different code on the second pass of the formatter" (2)

Using current master, fece8ae.

--- source
+++ first pass
@@ -9,278 +9,217 @@
 # Note that not all possible configuration values are present in this
 # autogenerated file.
 #
 # All configuration values have a default; values that are commented out
 # serve to show the default.
-
 # If extensions (or modules to document with autodoc) are in another directory,
 # add these directories to sys.path here. If the directory is relative to the
 # documentation root, use os.path.abspath to make it absolute, like shown here.
 #sys.path.insert(0, os.path.abspath('.'))
-
 # -- General configuration ------------------------------------------------
-
 # If your documentation needs a minimal Sphinx version, state it here.
 #needs_sphinx = '1.0'
-
 # Add any Sphinx extension module names here, as strings. They can be
 # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
 # ones.
-extensions = ['sphinx.ext.autodoc', 'sphinx.ext.viewcode',
-              'sphinx.ext.intersphinx']
-
+extensions = ['sphinx.ext.autodoc', 'sphinx.ext.viewcode', 'sphinx.ext.intersphinx']
 # Add any paths that contain templates here, relative to this directory.
 templates_path = ['_templates']
-
 # The suffix(es) of source filenames.
 # You can specify multiple suffix as a list of string:
 # source_suffix = ['.rst', '.md']
 source_suffix = '.rst'
-
 # The encoding of source files.
 #source_encoding = 'utf-8-sig'
-
 # The master toctree document.
 master_doc = 'index'
-
 # General information about the project.
 project = u'Pillow (PIL Fork)'
 copyright = u'1995-2011 Fredrik Lundh, 2010-2018 Alex Clark and Contributors'
 author = u'Fredrik Lundh, Alex Clark and Contributors'
-
 # The version info for the project you're documenting, acts as replacement for
 # |version| and |release|, also used in various other places throughout the
 # built documents.
 #
 # The short X.Y version.
 import PIL
+
 version = PIL.PILLOW_VERSION
 # The full version, including alpha/beta/rc tags.
 release = PIL.PILLOW_VERSION
-
 # The language for content autogenerated by Sphinx. Refer to documentation
 # for a list of supported languages.
 #
 # This is also used if you do content translation via gettext catalogs.
 # Usually you set "language" from the command line for these cases.
 language = None
-
 # There are two options for replacing |today|: either, you set today to some
 # non-false value, then it is used:
 #today = ''
 # Else, today_fmt is used as the format for a strftime call.
 #today_fmt = '%B %d, %Y'
-
 # List of patterns, relative to source directory, that match files and
 # directories to ignore when looking for source files.
 exclude_patterns = ['_build']
-
 # The reST default role (used for this markup: `text`) to use for all
 # documents.
 #default_role = None
-
 # If true, '()' will be appended to :func: etc. cross-reference text.
 #add_function_parentheses = True
-
 # If true, the current module name will be prepended to all description
 # unit titles (such as .. function::).
 #add_module_names = True
-
 # If true, sectionauthor and moduleauthor directives will be shown in the
 # output. They are ignored by default.
 #show_authors = False
-
 # The name of the Pygments (syntax highlighting) style to use.
 pygments_style = 'sphinx'
-
 # A list of ignored prefixes for module index sorting.
 #modindex_common_prefix = []
-
 # If true, keep warnings as "system message" paragraphs in the built documents.
 #keep_warnings = False
-
 # If true, `todo` and `todoList` produce output, else they produce nothing.
 todo_include_todos = False
-
-
 # -- Options for HTML output ----------------------------------------------
-
 # The theme to use for HTML and HTML Help pages.  See the documentation for
 # a list of builtin themes.
 
 import sphinx_rtd_theme
+
 html_theme = "sphinx_rtd_theme"
 html_theme_path = [sphinx_rtd_theme.get_html_theme_path()]
-
 # Theme options are theme-specific and customize the look and feel of a theme
 # further.  For a list of options available for each theme, see the
 # documentation.
 #html_theme_options = {}
-
 # Add any paths that contain custom themes here, relative to this directory.
 #html_theme_path = []
-
 # The name for this set of Sphinx documents.  If None, it defaults to
 # "<project> v<release> documentation".
 #html_title = None
-
 # A shorter title for the navigation bar.  Default is the same as html_title.
 #html_short_title = None
-
 # The name of an image file (relative to this directory) to place at the top
 # of the sidebar.
 #html_logo = None
-
 # The name of an image file (within the static path) to use as favicon of the
 # docs.  This file should be a Windows icon file (.ico) being 16x16 or 32x32
 # pixels large.
 #html_favicon = None
-
 # Add any paths that contain custom static files (such as style sheets) here,
 # relative to this directory. They are copied after the builtin static files,
 # so a file named "default.css" will overwrite the builtin "default.css".
 html_static_path = ['_static']
-
 # Add any extra paths that contain custom files (such as robots.txt or
 # .htaccess) here, relative to this directory. These files are copied
 # directly to the root of the documentation.
 #html_extra_path = []
-
 # If not '', a 'Last updated on:' timestamp is inserted at every page bottom,
 # using the given strftime format.
 #html_last_updated_fmt = '%b %d, %Y'
-
 # If true, SmartyPants will be used to convert quotes and dashes to
 # typographically correct entities.
 #html_use_smartypants = True
-
 # Custom sidebar templates, maps document names to template names.
 #html_sidebars = {}
-
 # Additional templates that should be rendered to pages, maps page names to
 # template names.
 #html_additional_pages = {}
-
 # If false, no module index is generated.
 #html_domain_indices = True
-
 # If false, no index is generated.
 #html_use_index = True
-
 # If true, the index is split into individual pages for each letter.
 #html_split_index = False
-
 # If true, links to the reST sources are added to the pages.
 #html_show_sourcelink = True
-
 # If true, "Created using Sphinx" is shown in the HTML footer. Default is True.
 #html_show_sphinx = True
-
 # If true, "(C) Copyright ..." is shown in the HTML footer. Default is True.
 #html_show_copyright = True
-
 # If true, an OpenSearch description file will be output, and all pages will
 # contain a <link> tag referring to it.  The value of this option must be the
 # base URL from which the finished HTML is served.
 #html_use_opensearch = ''
-
 # This is the file name suffix for HTML files (e.g. ".xhtml").
 #html_file_suffix = None
-
 # Language to be used for generating the HTML full-text search index.
 # Sphinx supports the following languages:
 #   'da', 'de', 'en', 'es', 'fi', 'fr', 'hu', 'it', 'ja'
 #   'nl', 'no', 'pt', 'ro', 'ru', 'sv', 'tr'
 #html_search_language = 'en'
-
 # A dictionary with options for the search language support, empty by default.
 # Now only 'ja' uses this config value
 #html_search_options = {'type': 'default'}
-
 # The name of a javascript file (relative to the configuration directory) that
 # implements a search results scorer. If empty, the default will be used.
 #html_search_scorer = 'scorer.js'
-
 # Output file base name for HTML help builder.
 htmlhelp_basename = 'PillowPILForkdoc'
-
 # -- Options for LaTeX output ---------------------------------------------
-
-latex_elements = {
-# The paper size ('letterpaper' or 'a4paper').
-#'papersize': 'letterpaper',
-
-# The font size ('10pt', '11pt' or '12pt').
-#'pointsize': '10pt',
-
-# Additional stuff for the LaTeX preamble.
-#'preamble': '',
-
-# Latex figure (float) alignment
-#'figure_align': 'htbp',
-}
-
+latex_elements = {}
+    # The paper size ('letterpaper' or 'a4paper').
+    #'papersize': 'letterpaper',
+    # The font size ('10pt', '11pt' or '12pt').
+    #'pointsize': '10pt',
+    # Additional stuff for the LaTeX preamble.
+    #'preamble': '',
+    # Latex figure (float) alignment
+    #'figure_align': 'htbp',
 # Grouping the document tree into LaTeX files. List of tuples
 # (source start file, target name, title,
 #  author, documentclass [howto, manual, or own class]).
 latex_documents = [
-  (master_doc, 'PillowPILFork.tex', u'Pillow (PIL Fork) Documentation',
-   u'Alex Clark', 'manual'),
+    (
+        master_doc,
+        'PillowPILFork.tex',
+        u'Pillow (PIL Fork) Documentation',
+        u'Alex Clark',
+        'manual',
+    )
 ]
-
 # The name of an image file (relative to this directory) to place at the top of
 # the title page.
 #latex_logo = None
-
 # For "manual" documents, if this is true, then toplevel headings are parts,
 # not chapters.
 #latex_use_parts = False
-
 # If true, show page references after internal links.
 #latex_show_pagerefs = False
-
 # If true, show URL addresses after external links.
 #latex_show_urls = False
-
 # Documents to append as an appendix to all manuals.
 #latex_appendices = []
-
 # If false, no module index is generated.
 #latex_domain_indices = True
-
-
 # -- Options for manual page output ---------------------------------------
-
 # One entry per manual page. List of tuples
 # (source start file, name, description, authors, manual section).
 man_pages = [
-    (master_doc, 'pillowpilfork', u'Pillow (PIL Fork) Documentation',
-     [author], 1)
+    (master_doc, 'pillowpilfork', u'Pillow (PIL Fork) Documentation', [author], 1)
 ]
-
 # If true, show URL addresses after external links.
 #man_show_urls = False
-
-
 # -- Options for Texinfo output -------------------------------------------
-
 # Grouping the document tree into Texinfo files. List of tuples
 # (source start file, target name, title, author,
 #  dir menu entry, description, category)
 texinfo_documents = [
-  (master_doc, 'PillowPILFork', u'Pillow (PIL Fork) Documentation',
-   author, 'PillowPILFork', 'Pillow is the friendly PIL fork by Alex Clark and Contributors.',
-   'Miscellaneous'),
+    (
+        master_doc,
+        'PillowPILFork',
+        u'Pillow (PIL Fork) Documentation',
+        author,
+        'PillowPILFork',
+        'Pillow is the friendly PIL fork by Alex Clark and Contributors.',
+        'Miscellaneous',
+    )
 ]
-
 # Documents to append as an appendix to all manuals.
 #texinfo_appendices = []
-
 # If false, no module index is generated.
 #texinfo_domain_indices = True
-
 # How to display URL addresses: 'footnote', 'no', or 'inline'.
 #texinfo_show_urls = 'footnote'
-
 # If true, do not generate a @detailmenu in the "Top" node's menu.
 #texinfo_no_detailmenu = False
 

--- first pass
+++ second pass
@@ -80,11 +80,10 @@
 # If true, `todo` and `todoList` produce output, else they produce nothing.
 todo_include_todos = False
 # -- Options for HTML output ----------------------------------------------
 # The theme to use for HTML and HTML Help pages.  See the documentation for
 # a list of builtin themes.
-
 import sphinx_rtd_theme
 
 html_theme = "sphinx_rtd_theme"
 html_theme_path = [sphinx_rtd_theme.get_html_theme_path()]
 # Theme options are theme-specific and customize the look and feel of a theme
@@ -155,18 +154,18 @@
 #html_search_scorer = 'scorer.js'
 # Output file base name for HTML help builder.
 htmlhelp_basename = 'PillowPILForkdoc'
 # -- Options for LaTeX output ---------------------------------------------
 latex_elements = {}
-    # The paper size ('letterpaper' or 'a4paper').
-    #'papersize': 'letterpaper',
-    # The font size ('10pt', '11pt' or '12pt').
-    #'pointsize': '10pt',
-    # Additional stuff for the LaTeX preamble.
-    #'preamble': '',
-    # Latex figure (float) alignment
-    #'figure_align': 'htbp',
+# The paper size ('letterpaper' or 'a4paper').
+#'papersize': 'letterpaper',
+# The font size ('10pt', '11pt' or '12pt').
+#'pointsize': '10pt',
+# Additional stuff for the LaTeX preamble.
+#'preamble': '',
+# Latex figure (float) alignment
+#'figure_align': 'htbp',
 # Grouping the document tree into LaTeX files. List of tuples
 # (source start file, target name, title,
 #  author, documentclass [howto, manual, or own class]).
 latex_documents = [
     (

Add a --check flag

For continuous integration, it would be helpful to have a --check flag that returns a non-zero status code without changing the source files.

black inserts 'unnecessary' newline for consistency

Operating system: Mac OS X 10.12.5
Python version: 3.6.1
Black version: 18.3a2
Does also happen on master: Yes

def example(session):
    result = session.query(models.Customer.id).filter(
        models.Customer.account_id == account_id,
        models.Customer.email == email_address,
    ).order_by(
        models.Customer.id.asc(),
    ).all()

becomes:

def example(session):
    result = session.query(models.Customer.id).filter(
        models.Customer.account_id == account_id, models.Customer.email == email_address
    ).order_by(
        models.Customer.id.asc(),
    ).all(
    )

Although the newline in all() is consistent with the other calls above it, I think it's unlikely that the code would be written this way normally.

Space before method brackets for built-in functions

I have the problem that black does a reformat of built-in functions, in my case on exec and eval as those have been statements in Python 2 but are methods in Python 3.

-        return exec("code", {}, {})
+        return exec ("code", {}, {})

Operating system: MacOS
Python version: 3.6.4
Black version: black, version 18.3a3
Does also happen on master: Yes

pylint: [pylint] C0330:Wrong hanging indentation before block (add 4 spaces).

Operating system: Win7 x64
Python version: 3.6.0
Black version: black==18.3a3 installed from pypi into venv
Does also happen on master:

Code:

def doc_dl_job(
    doc_ids,
    dl_folder,
    root_url=None,
    api_username=None,
    api_password=None,
    updatedcontent_root_url=None,
):
    '''Download documents by list of doc ids.'''
    api = API(api_username, api_password, root_url=root_url)

Pylint complains about all lines with function arguments.

Command: black -l 100 testfile.py

Unneeded spaces added after open parens?

Weirdness around the space after the parentheses. Also weird because I'd have thought the existing line was fine, and fit into the 79 characters I passed as an option

# existing
-    new_coord_names = (coord_names | set(vars_to_create)) - set(vars_to_remove)
-

# black
+    new_coord_names = ( coord_names | set(vars_to_create)) - set(
+        vars_to_remove
+    )

Internal error on a complex variable type annotation

Black reports internal error on this code

import typing as t

filters: t.List[
    t.Tuple[str, t.Union[str, t.List[t.Optional[str]]]],
] = []

Operating system: Ubuntu
Python version: 3.6.3
Black version: 18.3a3
Does also happen on master: yes

Long lines in dictionary comprehensions break due to trailing comma

This input:

{k: v for k, v in this_is_a_very_long_variable_which_will_cause_a_trailing_comma_which_breaks_the_comprehension}

becomes

{
    k: v
    for k,
    v in this_is_a_very_long_variable_which_will_cause_a_trailing_comma_which_breaks_the_comprehension,
}

Note the trailing comma after ...comprehension, this is not allowed in dictionary comprehensions.

Consider being even more opinionated about quotes

One thing I've come to appreciate from JS land is that even though ' and " are interchangeable, you can set the linters to require one or the other. My Python code is littered with alternating ' and " strings, sometimes even in the same dictionary, like so:

{
    'My key': "has a value",
    "my quote": 'is doubled',
}

I don't have an opinion on which is best (and I don't want to), but I wish to hell my code was consistent. There are good reasons to use one or the other (if the string contains other quotes, for example), but in general, I wish they were all the same. Maybe it could adjust any string that is enclosed in ' characters and does not contain a " character to be wrapped with " characters (or vice versa):

e.g
'hello' becomes "hello"
'She said "yes"' doesn't change
"Don't do that" never changes.

Should also be able to change all triple quotes to the same character as well unless the string contains the inverse quote.

When an expression is in star-args, a space gets added

For example, the expression gidgets[:2] is treated differently than gidgets when passed as star args.

For the file star_arg.py:

def f(*widgets):
    result = 0
    for widget in widgets:
        result += widget
    return result


def g(a, b, c, d):
    gidgets = (a, b, c)
    if d > 4:
        return f(*gidgets[:2])

    else:
        return f(*gidgets)

Running black (installed from HEAD as in #30) adds the space between the * and the star arg:

$ venv-HEAD/bin/black star_arg.py
reformatted star_arg.py

See the diff

diff --git a/star_arg.py b/star_arg.py
index e98997c..12ce82d 100644
--- a/star_arg.py
+++ b/star_arg.py
@@ -8,7 +8,7 @@ def f(*widgets):
 def g(a, b, c, d):
     gidgets = (a, b, c)
     if d > 4:
-        return f(*gidgets[:2])
+        return f(* gidgets[:2])

     else:
         return f(*gidgets)

Extra space in kwarg unpacking

I thought I'd seen this (and maybe put an issue in already on this?) but I can't find it anywhere. So here goes:

-        return security_screen(**self.screen_kwargs)
+        return security_screen(** self.screen_kwargs)

Operating system: MacOS
Python version: 3.6.4
Black version: black, version 18.3a3
Does also happen on master: Yes

black produces long lines for top-level code

Operating system: Mac OS X 10.12.5
Python version: 3.6.1
Black version: 18.3a2
Does also happen on master: Yes

black produces long lines for top-level code that isn't in a function. For example, the following:

session = get_session()
result = session.query(models.Customer.id).filter(
    models.Customer.account_id == account_id,
    models.Customer.email == email_address,
).order_by(
    models.Customer.id.asc(),
).all()

becomes:

session = get_session()
result = session.query(models.Customer.id).filter(models.Customer.account_id == account_id, models.Customer.email == email_address).order_by(models.Customer.id.asc(),).all()

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.