Unable to set/use knobs with yapf(A python formatter by google)

时间:2019-04-08 13:19:03

标签: python formatter pep8 pep8-checker

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

1 个答案:

答案 0 :(得分:0)

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