Skip to content

Feature request: Add support for python Black #403

Closed

Description

@iemejia

Python's Black has become the de-facto code auto-formatter for python. It would be great if we could call it with spotless.

Activity

  1. nedtwigg commented on May 23, 2019

    @nedtwigg
    Member

    The key thing needed to integrate with Spotless is a Function<String, String>. So if you can either

    A) Write some python code that configures the formatter, then has a function that consumes a string and returns a string
    B) Find some commandline flags which accomplishes this (e.g. read on stdin and write to stdout, or possibly read some tempfile and write back out to it)

    then that's the first step. The second step is integrating python and JVM, for which I believe there are many alternatives.

  2. rpdelaney commented on Jun 24, 2020

    @rpdelaney

    Find some commandline flags which accomplishes this (e.g. read on stdin and write to stdout, or possibly read some tempfile and write back out to it)

    black --code "$text"
    demo
    $ cat source.py
    from seven_dwwarfs import Grumpy, Happy, Sleepy, Bashful, Sneezy, Dopey, Doc
    x = {  'a':37,'b':42,
    
    'c':927}
    
    x = 123456789.123456789E123456789
    
    if very_long_variable_name is not None and \
     very_long_variable_name.field > 0 or \
     very_long_variable_name.is_debug:
     z = 'hello '+'world'
    else:
     world = 'world'
     a = 'hello {}'.format(world)
     f = rf'hello {world}'
    if (this
    and that): y = 'hello ''world'#FIXME: https://github.com/python/black/issues/26
    class Foo  (     object  ):
      def f    (self   ):
        return       37*-2
      def g(self, x,y=42):
          return y
    def f  (   a: List[ int ]) :
      return      37-a[42-u :  y**3]
    def very_important_function(template: str,*variables,file: os.PathLike,debug:bool=False,):
        """Applies `variables` to the `template` and writes to `file`."""
        with open(file, "w") as f:
         ...
    # fmt: off
    custom_formatting = [
        0,  1,  2,
        3,  4,  5,
        6,  7,  8,
    ]
    # fmt: on
    regular_formatting = [
        0,  1,  2,
        3,  4,  5,
        6,  7,  8,
    ]
    $ black --code "$(<source.py)"
    from seven_dwwarfs import Grumpy, Happy, Sleepy, Bashful, Sneezy, Dopey, Doc
    
    x = {"a": 37, "b": 42, "c": 927}
    
    x = 123456789.123456789e123456789
    
    if (
        very_long_variable_name is not None
        and very_long_variable_name.field > 0
        or very_long_variable_name.is_debug
    ):
        z = "hello " + "world"
    else:
        world = "world"
        a = "hello {}".format(world)
        f = rf"hello {world}"
    if this and that:
        y = "hello " "world"  # FIXME: https://github.com/python/black/issues/26
    
    
    class Foo(object):
        def f(self):
            return 37 * -2
    
        def g(self, x, y=42):
            return y
    
    
    def f(a: List[int]):
        return 37 - a[42 - u : y ** 3]
    
    
    def very_important_function(
        template: str, *variables, file: os.PathLike, debug: bool = False,
    ):
        """Applies `variables` to the `template` and writes to `file`."""
        with open(file, "w") as f:
            ...
    
    
    # fmt: off
    custom_formatting = [
        0,  1,  2,
        3,  4,  5,
        6,  7,  8,
    ]
    # fmt: on
    regular_formatting = [
        0,
        1,
        2,
        3,
        4,
        5,
        6,
        7,
        8,
    ]
    
    $
  3. nedtwigg commented on Aug 25, 2020

    @nedtwigg
    Member

    Implemented in #672, shipped in plugin-gradle 5.2.0 (tweet), PR for maven is welcome. The setup experience could be better, we'll track that at #674.

  4. iemejia commented on Aug 25, 2020

    @iemejia
    ContributorAuthor

    Great ! 👏 thanks!

  5. nedtwigg commented on Aug 25, 2020

    @nedtwigg
    Member

    @iemejia, @rpdelaney, do you have any feedback on what info would be helpful for the black docs: psf/black#1622

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions