Yapf: Ignore files

Created on 24 Jan 2017  路  16Comments  路  Source: google/yapf

It was great if one could add files to ignore to a knobs config. This will apply for recursive mode. Sometimes there are auto generated files where it makes no sense to improve the style.

enhancement

Most helpful comment

Personally I don't understand why this can't live in e.g. setup.cfg. That's how isort works, for instance. Example:

[isort]
skip_glob = *snapshots*.py,.serverless,node_modules,.vscode

All 16 comments

+1

shouldn't have to specify every excluded directory on the command line every time you run yapf

+1

would be useful if you could specify this in config file. Especially when running yapf in a VCS pre-commit hook scenario.

This would enabble yapf to skip autogenareted files (django migrations, etc)

What would this look like in the config file? I can imagine doing something like .gitignore, but adding yet another config file seems clunky...

something like flake8 config perhaps? (http://flake8.pycqa.org/en/latest/user/configuration.html)

exclude = .git,__pycache__,docs/source/conf.py,old,build,dist

Are you guys OK with adding an exclude option on YAPF config file / section ?

I could submit a PR if that's the case.

Yeah. Please create one and we can include it. :-)

Shouldn't style configuration preferably be kept project independent/sharable between projetcs? IMHO it's not the right place to keep this setting. Adapting a .gitignore approach makes more sense in this respect

@bj00rn I'm not sure I'm following your approach.
What do you suggest ?

after more thought, @heron182. I suggest adopting .gitignore approach. This way, style configuration and configuration of which files to exclude are kept seperate.

what are your thoughts on this @gwelymernans?

@bj00rn Good point. This would be an issue if the developer uses a global .yapf.style file. I think your idea of a .gitignore approach (.yapfignore?) is a good one. I'm sorry I didn't catch this sooner. Things have been crazy at work recently.

A question first. I'm not a git pro, so I'm not familiar with how it works on every level. Does the .gitignore work even if the cwd is in a subdirectory? That is, if the git root is foo/ and I'm currently in foo/bar/, will the git commands still find the .gitignore in the foo/ directory?

If it does use the .gitignore no matter what the cwd is, then we have two options:

  1. Crawl up the directories until we find a .yapfignore or run out of directories, or
  2. Just look in the cwd for a .yapfignore and if we don't find one proceed as if one wasn't specified.

Thoughts?

:+1: for .yapfignore. This would also allow to have sub repositories with different ignore rules the upper repo does not have to know.

AFAIK git does upwards scanning but only to the root of the repository (and you can have a global gitignore). So I think yapf should behave similar and the directory where .style.yapf is located should be the "root".

@gwelymernans i think just looking for a .yapfignore in CWD is a good enough starting point.

AFAIK this is how eslint does it

When ESLint is run, it looks in the current working directory to find an .eslintignore file before determining which files to lint. If this file is found, then those preferences are applied when traversing directories. Only one .eslintignore file can be used at a time, so .eslintignore files other than the one in the current working directory will not be used.

https://eslint.org/docs/user-guide/configuring#ignoring-files-and-directories

@sedrubal Git in contrast to yapf, has a very strict understanding of what the repository root is which enables the "cascading" behavior you refer to. I dont' like the idea of relying on the existence of a local .style.yapf within the project for this purpose.

I agree with @bj00rn (my secret preference was for option 2). If we had a definite idea of what constituted a root directory, then it would be different. But as he mentioned it's more nebulous.

@gwelymernans I made a quick PR to test out option 2

Throwing a use-case for this feature:

I'm using https://github.com/syrusakbary/snapshottest. Snapshots need to be committed to the repo but not formatted by yapf.

Personally I don't understand why this can't live in e.g. setup.cfg. That's how isort works, for instance. Example:

[isort]
skip_glob = *snapshots*.py,.serverless,node_modules,.vscode
Was this page helpful?
0 / 5 - 0 ratings

Related issues

pankdm picture pankdm  路  8Comments

eli-schwartz picture eli-schwartz  路  5Comments

hayd picture hayd  路  8Comments

haphut picture haphut  路  3Comments

MagSec-Arts picture MagSec-Arts  路  3Comments