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
Provide a stdin-filename to allow stdin to respect force-exclude rules #1780
Conversation
…ude rules This will allow automatic tools to enforce the project's exclude/force-exclude rules even if they pass the file through stdin to update its buffer. This is a similar solution to --stdin-display-name in flake8.
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.
Thanks for the PR @bellini666!
Please update the help output that is in the README, add a test as this is a new feature, and see my other comments below.
Also, personally I find the option name of stdin-filename
a bit too specific as you can pass a full absolute filepath. That could be useful for cases where the filename isn't excluded, but its containing directory is excluded. Passing filepaths does break when they would point outside of the current working directory but I dunno how much of an issue that is.
Co-authored-by: Richard Si <63936253+ichard26@users.noreply.github.com>
@ichard26 I have fixed your comments and also added some tests for the new functionality! |
Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com>
@hugovk I tried requesting your review again by clicking in the |
OK, thanks! That wording looks fine now, I'll leave the functionality to the maintainers to review 👍 |
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.
README looks good and well done on the tests!
Unfortunately I found another edge case where Black incorrectly uses format_file_in_place
.
I completely forgot but please also add an entry in the change log, thank you! |
Or put it in a PR template? |
@ichard26 ok, everything should be fine now :) |
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.
Thanks! Nice addition. Would love @ichard26 to check it one last time before merge tho since they have the most context.
FYI, I plan on reviewing this soon (as in probably tomorrow). |
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.
Overall these changes look good other than the additional bug I found.
Many thanks for working on this!
Co-authored-by: Richard Si <63936253+ichard26@users.noreply.github.com>
@ichard26 after applying the suggestions (8d13a9c) some tests broke. I wasn't able to check it yet, but looking at the commit now, wouldn't that |
You're correct, my bad. Uh perhaps this patch should work. diff --git a/src/black/__init__.py b/src/black/__init__.py
index 7594cce..0850b11 100644
--- a/src/black/__init__.py
+++ b/src/black/__init__.py
@@ -633,19 +633,7 @@ def get_sources(
p = Path(s)
is_stdin = False
- if not is_stdin or p.is_dir():
- sources.update(
- gen_python_files(
- p.iterdir(),
- root,
- include_regex,
- exclude_regex,
- force_exclude_regex,
- report,
- gitignore,
- )
- )
- elif is_stdin or p.is_file():
+ if is_stdin or p.is_file():
normalized_path = normalize_path_maybe_ignore(p, root, report)
if normalized_path is None:
continue
@@ -664,6 +652,18 @@ def get_sources(
p = Path(f"{STDIN_PLACEHOLDER}{str(p)}")
sources.add(p)
+ elif p.is_dir():
+ sources.update(
+ gen_python_files(
+ p.iterdir(),
+ root,
+ include_regex,
+ exclude_regex,
+ force_exclude_regex,
+ report,
+ gitignore,
+ )
+ )
elif s == "-":
sources.add(p)
else: Didn't give much thought into whether this should work. Tests do pass though. If |
@ichard26 that suggestion worked fine! Just pushed it |
Awesome work all! Thanks for your addition to black! |
psf#1780) * Provide a stdin-filename to allow stdin to respect exclude/force-exclude rules This will allow automatic tools to enforce the project's exclude/force-exclude rules even if they pass the file through stdin to update its buffer. This is a similar solution to --stdin-display-name in flake8. * Update src/black/__init__.py Co-authored-by: Richard Si <63936253+ichard26@users.noreply.github.com> * --stdin-filename should only respect --exclude-filename * Update README with the new --stdin-filename option * Write some tests for the new stdin-filename functionality * Apply suggestions from code review Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com> * Force stdin output when we asked for stdin even if the file exists * Add an entry in the changelog regarding --stdin-filename * Reduce disk reads if possible Co-authored-by: Richard Si <63936253+ichard26@users.noreply.github.com> * Check for is_stdin and p.is_file before checking for p.is_dir() Co-authored-by: Richard Si <63936253+ichard26@users.noreply.github.com> Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com>
Fix #1776