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

Code Flag Options #2259

Merged
merged 16 commits into from
Jun 2, 2021
Merged

Code Flag Options #2259

merged 16 commits into from
Jun 2, 2021

Conversation

HassanAbouelela
Copy link
Contributor

@HassanAbouelela HassanAbouelela commented May 26, 2021

Closes #2104, closes #1801.

Fixes a bug in the --code flag which would ignore options such as --diff, --check, and --fast.

This PR implements the fix described in the issue. It also adds tests for this fix.

Properly handles the diff, color, and fast option when black is run with
 the `--code` option.

Signed-off-by: Hassan Abouelela <[email protected]>
Adds test for the CLI `--code` option.

Signed-off-by: Hassan Abouelela <[email protected]>
Updates the docs to remove the warning about the code options bug, and
adds a changelog entry.

Signed-off-by: Hassan Abouelela <[email protected]>
@HassanAbouelela
Copy link
Contributor Author

I've fixed the linting issues now, could someone rerun the workflows.

Copy link
Collaborator

@cooperlees cooperlees left a comment

Choose a reason for hiding this comment

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

Wow, great tests here! Thanks for your contribution.

Signed-off-by: Hassan Abouelela <[email protected]>
Copy link
Collaborator

@ichard26 ichard26 left a comment

Choose a reason for hiding this comment

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

Thanks for working on this fix! I got some feedback but nothing truly blocking (except for possibly a Windows bug).

Great tests btw!

src/black/__init__.py Outdated Show resolved Hide resolved
src/black/__init__.py Outdated Show resolved Hide resolved
tests/test_black.py Outdated Show resolved Hide resolved
src/black/__init__.py Outdated Show resolved Hide resolved
Wraps the output stream for windows to fix the color option when using
diff.

Signed-off-by: Hassan Abouelela <[email protected]>
Switches to a plain assert instead of unittest's self.assertEqual in the
 `--code` option tests.

Signed-off-by: Hassan Abouelela <[email protected]>
Copy link
Collaborator

@ichard26 ichard26 left a comment

Choose a reason for hiding this comment

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

This time I did a hands-on review / test and this still isn't quite right. The error message when formatting fails isn't right. For normal cases, this is handled by the Report class. Check out the difference between these cases:

--code:

Screenshot from 2021-05-27 18-51-43

STDIN:

Screenshot from 2021-05-27 18-50-41

It honestly might be better to generalize reformat_one to handle the --code case too.

Thanks for being patient with me!

tests/test_black.py Show resolved Hide resolved
src/black/__init__.py Outdated Show resolved Hide resolved
@HassanAbouelela
Copy link
Contributor Author

Thanks for the feedback, and I totally agree. Originally I wanted to maintain the whole “thrown together, mainly for testing” behavior, but as the PR has progressed, the feature has become quite bloated, and repeated a lot of the same code from the other functions. I’ll see what I can do to formalize this flag a bit more.

Switches the code option to use the STDIN formatter instead of manually
formatting the input, to add support for other flags, and properly
handle the output.

Signed-off-by: Hassan Abouelela <[email protected]>
@HassanAbouelela
Copy link
Contributor Author

Updated the code to use the STDIN formatter.

As a note for maintainers: we should probably squash/rebase this PR before merging. Most of the commits prior to switching to the STDIN formatter are irrelevant.

@JelleZijlstra
Copy link
Collaborator

We always squash merge, fyi. Also, could you fix the merge conflict in CHANGES.md?

# Conflicts:
#	CHANGES.md
@HassanAbouelela
Copy link
Contributor Author

Thanks, good to know. The conflicts have been fixed.

Copy link
Collaborator

@JelleZijlstra JelleZijlstra left a comment

Choose a reason for hiding this comment

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

Thanks, this looks significantly cleaner

src/black/__init__.py Outdated Show resolved Hide resolved
src/black/report.py Outdated Show resolved Hide resolved
@JelleZijlstra
Copy link
Collaborator

Just tried this out locally and decided that it's better to make -c apply --quiet by default because the extra "All done" output isn't really useful.

There might also be a discrepancy regarding whether pyproject.toml is used; looking into that more now.

@JelleZijlstra
Copy link
Collaborator

OK, there's indeed a problem with pyproject.toml:

(black) jelle@mbpt-root tmp % black -c "a = 'b'"  
a = "b"
(black) jelle@mbpt-root tmp % echo "a = 'b'" | black -
a = 'b'
All done! ✨ 🍰 ✨
1 file left unchanged.
(black) jelle@mbpt-root tmp % cat pyproject.toml 
[tool.black]
skip-string-normalization = true

I think the correct behavior is to pick up the pyproject.toml. That way, users can run black -c in their project directory to see how black would format code in that project.

The reason for the discrepancy is that in black.read_pyproject_toml, we pass () to black.files.find_pyproject_toml, and that function returns Path("/").resolve() if no src is provided. I think it should instead default to searching for a toml in the cwd. The current behavior would make it pick up /pyproject.toml at the file system root, which doesn't make much sense.

Changes the find project root function to look in the current directory
instead of system root when no sources are passed.

Signed-off-by: Hassan Abouelela <[email protected]>
src/black/files.py Outdated Show resolved Hide resolved
Changes the find project root function to look in the current directory
instead of system root when no sources are passed.

Signed-off-by: Hassan Abouelela <[email protected]>
Changes the assertions made on mock calls to work with python version
earlier than 3.8.

Signed-off-by: Hassan Abouelela <[email protected]>
Copy link
Collaborator

@ichard26 ichard26 left a comment

Choose a reason for hiding this comment

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

I am happy with the final product for the most part. The only thing I'd dock against this patch is the general awkwardness of using format_stdin_to_stdout in reformat_code, but this is already quite complex so I'd rather refactor this (and other parts of the "main called to formatting will start" pathway) in a separate PR.

Thank you so much for your contribution! This project is only possible by contributions like these 🖤. You're awesome, @HassanAbouelela! Congratulations on your first PR for psf/black. It's great to be able to get rid of that documentation warning for once and all. Also thanks for playing a role in the very nice welcome to Python Discord.

P.S. since you seem to be *the* person who manages to break all of our development workflows, it would be even more awesome if you could provide some feedback on your experience contributing here, we got a lot to learn (especially in the Windows department) so yeah. More details in #2238.

@JelleZijlstra JelleZijlstra merged commit 7567cdf into psf:main Jun 2, 2021
@HassanAbouelela HassanAbouelela deleted the code-options branch June 2, 2021 01:55
@cooperlees
Copy link
Collaborator

This needs the CHNAGES.md fixed ... Wonder how we can automate checking for that ...

@ichard26
Copy link
Collaborator

ichard26 commented Jun 2, 2021

Oh fun! Probably automatiable although that will make it harder to cleanly separate the logic into own repo when I eventually do that.

Good catch!

cooperlees added a commit that referenced this pull request Jun 2, 2021
@cooperlees
Copy link
Collaborator

cooperlees commented Jun 2, 2021

5de8c5f - Just pushed it.

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.

Safety checks are disabled when passing in code using -c / --code --code doesn't work with --check or --diff
4 participants