7

I have recently been trying to get familiar with google/yapf. But somehow, I am unable to set up a .style.yapf and even setup.cfg. I am not sure where to place these files. Currently, I am using any/or both files in my current directory but the global settings always override my .style.yapf/setup.cfg files. Can someone please help me out, I am not getting expected results using their knobs with the sample examples given on github/yapf

Shivam Chauhan
  • 103
  • 1
  • 7

1 Answers1

8

From the docs:

YAPF will search for the formatting style in the following manner:

  1. Specified on the command line
  2. In the [style] section of a .style.yapf file in either the current directory or one of its parent directories.
  3. In the [yapf] section of a setup.cfg file in either the current directory or one of its parent directories.
  4. In the ~/.config/yapf/style file in your home directory.

If none of those files are found, the default style is used (PEP8).

If I use the following Python input for testing:

class Example: # This is an example
    def __init__(self):
        pass

Running yapf example.py produces no changes:

class Example:  # This is an example
    def __init__(self):
        pass

But if I create .style.yapf in the current directory with the following content:

[style]
SPACES_BEFORE_COMMENT=5
BLANK_LINE_BEFORE_NESTED_CLASS_OR_DEF=true

And then rerun yapf example.py, I get:

class Example:     # This is an example

    def __init__(self):
        pass

If I wanted these changes to apply globaly, I would create instead ~/.config/yapf/style with the same content.

I could also create a file named setup.cfg in the current directory (or a parent) with the following content:

[yapf]
SPACES_BEFORE_COMMENT=5
BLANK_LINE_BEFORE_NESTED_CLASS_OR_DEF=true
larsks
  • 277,717
  • 41
  • 399
  • 399
  • Can i use this with **FormatCode** or **FormatFile** with the `in_place` parameter set to **True**? – Shivam Chauhan Apr 08 '19 at 13:37
  • From [the docs](https://github.com/google/yapf#example-as-a-module) it looks like you have to pass an explicit `style_config` to either of those methods, but if you look at [the source code](https://github.com/google/yapf/blob/master/yapf/__init__.py#L182) you can see how the command line tool does it. – larsks Apr 08 '19 at 13:44