-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Conversation
384ef08
to
1b28854
Compare
Also, I genuinely do not know how I would check the types of |
Try |
Pull Request Test Coverage Report for Build 447
💛 - Coveralls |
@JelleZijlstra Ah! Thank you a bunch. |
92b08ea
to
35062ae
Compare
There was a problem hiding this 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\/" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Use r-strings for patterns.
- Escaping slashes is not necessary.
- 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?$" |
There was a problem hiding this comment.
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) + "/"): |
There was a problem hiding this comment.
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.
d217dca
to
b185331
Compare
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)) |
There was a problem hiding this comment.
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.
- Why are we compiling the regex again for every entry in
src
? - Could we use a better exception type than Exception?
- Use an f-string for the exception message?
- Use the
!r
modifier so that the error is less likely to be confusing. - We actually don't want to raise an exception but rather just use
err()
followed byctx.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/" |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
@ambv Added some logic to allow for prepended directories in the filter, and confirmed it works well with both 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 I'd also like to add a couple more test cases too. |
b6f3e1d
to
1021da5
Compare
1021da5
to
57e86ab
Compare
Makes sense to just mention: "use forward-slashes on Windows, too." |
Awesome! ✨ 🍰 ✨ |
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! ✨