jason-kane/PyYapf

Picking up config files

rdrey opened this issue · 8 comments

rdrey commented

Hey all!

I'm running PyYapf in a python2 project and it's not yet picking up my config files. (The idea is to keep them in the repo for my whole team.)

I've got both types of config files (.style.yapf & setup.cfg) in /Users/rainer/work/rar/ and am formatting a file in /Users/rainer/work/rar/rar/rar/tests/lib/.

command: yapf_selection
PyYapf: Encoding is 'UTF-8'
PyYapf: Formatting selection (3369, 2199)
PyYapf: Detected indent '    '
PyYapf: Running ['/Users/rainer/.virtualenvs/rar/bin/yapf', '--in-place', '/var/folders/ry/srqbqv_n3n3fvvdbvfpj6z6w0000gq/T/tmpmaqr_2.py'] in /Users/rainer/work/rar/rar/rar/tests/lib
PyYapf: Exit code 0

Would this setup work if my virtualenv was inside the repo, too? (I think yapf also looks in the binaries' parent folders?)

Thanks!

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).

So yapf doesn't care about the binaries parent folders. pyyapf does try to set the cwd to the directory that the file being edited is in.

        fname = self.view.file_name()
        self.popen_cwd = os.path.dirname(fname) if fname else None

so I don't see why it is missing the configs from your repo.

rdrey commented

Alright, thanks. I'll dig into the source a bit and add logging to figure out why this isn't working for me.

rdrey commented

Ah, I found the issue. Unfortunately yapf.__init__ does the following:

def _FormatFile(filename,
                lines,
                style_config=None,
                no_local_style=False,
                in_place=False,
                print_diff=False,
                verify=True):
  logging.info('Reformatting %s', filename)
  if style_config is None and not no_local_style:
    style_config = (
        file_resources.GetDefaultStyleForDir(os.path.dirname(filename)))

and PyYapf's filename is in a temp folder: /var/folders/ry/srqbqv_n3n3fvvdbvfpj6z6w0000gq/T/tmp3pqy6j.py

(I hacked PyYapf.py to always print logging from yapf, even when the exit code is good and modified GetDefaultStyleForDir to do lots of logging.)

you might try the "use_stdin" setting, it will choke on unicode but doesn't need the tempfile stuff.

Yeah, please do try `use_stdin'. (That's what I am using personally.)

rdrey commented

Thanks. That works for me 👍 I just didn't want to have to duplicate config in my own sublime settings and team-wide config files. I have thrown some unicode into my source and don't see any issues (yet). (And I see the comment only when this option is enabled yapf will pick up the style as described in yapf --help now 😄 )

Great, thanks for your debugging!

Also, please do let us know if there are any issues with the use_stdin option. It might be nice to fully transition to it at some point.

rdrey commented

Thanks. I work with a bunch of unicode in unit tests at the moment, will let you know if I run into anything.