Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added --include and --exclude cli options #281

Merged
merged 5 commits into from
Jun 1, 2018

Conversation

autophagy
Copy link
Member

@autophagy autophagy commented May 30, 2018

These 2 options allow you to pass in regular expressions that determine whether files/directories are included or excluded in the recursive search.

This should address #270. I put the change log entry for this under 18.6b0 since I don't think it has been released yet - but let me know if this was incorrect! ✨

@autophagy
Copy link
Member Author

Also, I genuinely do not know how I would check the types of exclude and include here https://github.com/ambv/black/pull/281/files#diff-a75051ebf8e3452bf3d590c186944252R2791 since I am passing in compiled regular expressions which have a type of _sre.SRE_Pattern, but apparently AttributeError: module '_sre' has no attribute 'SRE_Pattern' 😅

@JelleZijlstra
Copy link
Collaborator

Try typing.Pattern[str].

@coveralls
Copy link

coveralls commented May 30, 2018

Pull Request Test Coverage Report for Build 447

  • 53 of 53 (100.0%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.2%) to 96.203%

Totals Coverage Status
Change from base Build 437: 0.2%
Covered Lines: 2280
Relevant Lines: 2370

💛 - Coveralls

@autophagy
Copy link
Member Author

@JelleZijlstra Ah! Thank you a bunch.

Copy link
Collaborator

@ambv ambv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is almost there! See review comments, and more importantly, add tests for this.

black.py Outdated
@@ -46,6 +46,10 @@

__version__ = "18.5b1"
DEFAULT_LINE_LENGTH = 88
DEFAULT_EXCLUDES = (
"build\/|buck-out\/|dist\/|_build\/|.git\/|.hg\/|.mypy_cache\/|.tox\/|.venv\/"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Use r-strings for patterns.
  2. Escaping slashes is not necessary.
  3. But escaping periods is.

Pro-tip: run Python with warnings, this will make Python tell you things like:

black.py:50: DeprecationWarning: invalid escape sequence \/
  "build\/|buck-out\/|dist\/|_build\/|.git\/|.hg\/|.mypy_cache\/|.tox\/|.venv\/"
black.py:52: DeprecationWarning: invalid escape sequence \.
  DEFAULT_INCLUDES = "\.pyi?$"

black.py Outdated
DEFAULT_EXCLUDES = (
"build\/|buck-out\/|dist\/|_build\/|.git\/|.hg\/|.mypy_cache\/|.tox\/|.venv\/"
)
DEFAULT_INCLUDES = "\.pyi?$"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto about r-strings.

black.py Outdated
"""
for child in path.iterdir():
if child.is_dir():
if child.name in BLACKLISTED_DIRECTORIES:
if not exclude.search(str(child) + "/"):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$ mkdir -p a/b/c/d
$ touch a/b/c/d/file.py
$ cd a
$ black .
No paths given. Nothing to do 😴

(This works on master.)

These 2 options allow you to pass in regular expressions that determine
whether files/directories are included or excluded in the recursive file
search.
black.py Outdated
raise SyntaxError(
"Invalid regular expression for exclude given: {}".format(exclude)
)
sources.extend(gen_python_files_in_dir(p, include_regex, exclude_regex))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, just noticed this now.

  1. Why are we compiling the regex again for every entry in src?
  2. Could we use a better exception type than Exception?
  3. Use an f-string for the exception message?
  4. Use the !r modifier so that the error is less likely to be confusing.
  5. We actually don't want to raise an exception but rather just use err() followed by ctx.exit(2) (1 is already used by --check and 123 is internal error). It's a tool, we don't want to trouble people with unnecessary tracebacks.

@@ -46,6 +46,10 @@

__version__ = "18.5b1"
DEFAULT_LINE_LENGTH = 88
DEFAULT_EXCLUDES = (
r"build/|buck-out/|dist/|_build/|\.git/|\.hg/|\.mypy_cache/|\.tox/|\.venv/"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a problem to solve here. In the current model, "not_a_build/" is also matching. It would be more robust to prepend all those directory names with slashes. However, if the user specified a relative directory, we might not have a leading slash always. If they said black ., it's fine. If they said black *, it's not.

So up to you how we solve this but it will have to be addressed before we land this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I think prepending the directories with slashes would be the best route. I'll play around with it and see if I can come up with an elegant way of doing this.

autophagy added a commit to autophagy/black that referenced this pull request May 31, 2018
autophagy added a commit to autophagy/black that referenced this pull request May 31, 2018
autophagy added a commit to autophagy/black that referenced this pull request May 31, 2018
@autophagy
Copy link
Member Author

autophagy commented May 31, 2018

@ambv Added some logic to allow for prepended directories in the filter, and confirmed it works well with both * and .. I'm building up the path to search from using the children, rather than the path, as it's a little easier when the path itself is something like .

Do you think it would be best to mention in the help/documentation that the include/exclude filters only support POSIX formats? Windows' use of \ instead of / was causing a few problems, and supporting /|\ in the defaults seems like it could be a little fragile (for example, if someone on a system with POSIX paths decides to call a directory /build\me/, the exclude filter would match on the /build\)

I'd also like to add a couple more test cases too.

@ambv
Copy link
Collaborator

ambv commented Jun 1, 2018

Makes sense to just mention: "use forward-slashes on Windows, too."

@ambv ambv merged commit 51756a4 into psf:master Jun 1, 2018
@ambv
Copy link
Collaborator

ambv commented Jun 1, 2018

Awesome! ✨ 🍰 ✨

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants