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

[WIP] Added black as a formatter #1234

Closed
wants to merge 2 commits into from

Conversation

jarshwah
Copy link

Fixes #1153

This is a work in progress PR. I only spent a little bit of time on it, but am sharing in case someone wants to pick up where I left off now that there is an issue open for it. I'm probably not going to have time to work on it in the next couple of weeks (away on holiday) but will pick back up when I'm back if no one else has taken over.

This pull request:

  • Has a title summarizes what is changing
  • Includes a news entry file (remember to thank yourself!)
  • Has unit tests & code coverage is not adversely affected (within reason)
  • Works on all actively maintained versions of Python (e.g. Python 2.7 & the latest Python 3 release)
  • Works on Windows 10, macOS, and Linux (e.g. considered file system case-sensitivity)

@msftclas
Copy link

msftclas commented Mar 29, 2018

CLA assistant check
All CLA requirements met.

@codecov
Copy link

codecov bot commented Apr 7, 2018

Codecov Report

Merging #1234 into master will increase coverage by 1.26%.
The diff coverage is 56%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1234      +/-   ##
==========================================
+ Coverage    71.1%   72.37%   +1.26%     
==========================================
  Files         269      261       -8     
  Lines       12395    11995     -400     
  Branches     2198     2135      -63     
==========================================
- Hits         8814     8681     -133     
+ Misses       3447     3190     -257     
+ Partials      134      124      -10
Impacted Files Coverage Δ
src/client/formatters/types.ts 100% <ø> (ø) ⬆️
src/client/common/types.ts 100% <100%> (ø) ⬆️
src/client/providers/formatProvider.ts 94.54% <100%> (+0.31%) ⬆️
src/client/formatters/blackFormatter.ts 42.1% <42.1%> (ø)
.../client/debugger/DebugClients/RemoteDebugClient.ts 17.64% <0%> (-17.36%) ⬇️
src/client/debugger/DebugClients/DebugFactory.ts 72.72% <0%> (-9.1%) ⬇️
src/client/debugger/Common/Utils.ts 44.26% <0%> (-6.56%) ⬇️
src/client/debugger/mainV2.ts 71.62% <0%> (-4.83%) ⬇️
...c/client/debugger/DebugClients/LocalDebugClient.ts 51.96% <0%> (-4.39%) ⬇️
src/client/debugger/Main.ts 50.61% <0%> (-1.24%) ⬇️
... and 31 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 67f42f0...ae2a693. Read the comment docs.

@@ -59,7 +59,7 @@ export abstract class BaseFormatter {
const executionInfo = this.helper.getExecutionInfo(this.product, args, document.uri);
executionInfo.args.push(tempFile);
const pythonToolsExecutionService = this.serviceContainer.get<IPythonToolExecutionService>(IPythonToolExecutionService);
const promise = pythonToolsExecutionService.exec(executionInfo, { cwd, throwOnStdErr: true, token }, document.uri)
const promise = pythonToolsExecutionService.exec(executionInfo, { cwd, throwOnStdErr: false, token }, document.uri)
Copy link
Author

@jarshwah jarshwah Apr 7, 2018

Choose a reason for hiding this comment

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

Black outputs Reformatted <filename> to stderr for each file, which ends up breaking due to this. I think it'd be better to check the return code rather than inferring an error due to output on stderr - which is frequently used to communicate state rather than an error condition.

Choose a reason for hiding this comment

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

Agreed. Though I don't remember why I did this. For some reason it was necessary. Please try testing it temporarily and test without the formatter installed. Ideally that should fail. If that works, my suggesting is to pass this as a flag to the base linter.

Choose a reason for hiding this comment

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

I'm reluctant to change existing code, as I have a very vague memory of the fact that we couldn't rely on exit codes (at least for the linters).

Copy link
Author

Choose a reason for hiding this comment

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

Thanks. I'll test with each of the formatters - both installed and not installed and see what happens.


const args = ['--diff'];
// if (formatSelection) {
// black does not support partial formatting, throw an error?
Copy link
Author

Choose a reason for hiding this comment

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

Unsure how we should handle the lack of partial formatting.

Choose a reason for hiding this comment

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

This is possible, all you need to do is, send in the source of the selected content.
Check the command line docs (black [OPTIONS] [SRC]...) - pass in the text
https://github.com/ambv/black#command-line-options

Copy link
Member

Choose a reason for hiding this comment

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

FYI, @ambv has said he purposefully doesn't support partial formatting in Black so we might want to simply not support it in this instance.

Copy link
Author

Choose a reason for hiding this comment

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

It wasn't possible to use stdin with the current way formatters are structured, without introducing some new concepts. I'm going to ignore partial formatting for now - maybe someone can attempt it in a future patch.

Copy link

@DonJayamanne DonJayamanne left a comment

Choose a reason for hiding this comment

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

Finally, please do add at least two tests:

  • Simple formatting (something as simple a line formatting, sample provided on their home page)
  • Formatting with arguments

You can focus on the tests as a last item.
Once again, thanks for your effort.

@brettcannon
Copy link
Member

brettcannon commented May 5, 2018

I'm closing this in favour of #1611 as I wanted to get this landed before PyCon US and took the opportunity to implement this from scratch as a learning exercise for myself.

I do want to thank you, @jarshwah , for pushing us to get this support in. I'm also making sure you're listed as a co-author on my PR as I checked your PR once or twice to see how you handled something (although one of those times you solution was a comment of WAT 😉 ). Hopefully there's no hard feelings about not directly using your PR for this.

@brettcannon brettcannon closed this May 5, 2018
@ambv

This comment has been minimized.

@brettcannon

This comment has been minimized.

@jarshwah
Copy link
Author

jarshwah commented May 5, 2018

Not at all, thanks for taking over! Family has taken up most of my time for the last few weeks, so I'm glad at the outcome. I'll check out your PR too - interested in how you handled some of the issues.

@jarshwah jarshwah deleted the formatter-black branch May 5, 2018 03:15
@lock lock bot locked as resolved and limited conversation to collaborators Jul 31, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for formatting tool "black"
5 participants