Skip to content

fix: formatting .pyi files with black - #14736

Merged
Karthik Nadig (karthiknadig) merged 4 commits into
microsoft:mainfrom
sbdchd:steve/fix-formatting-pyi-files-with-black
Nov 30, 2020
Merged

Karthik Nadig (karthiknadig) merged 4 commits into
microsoft:mainfrom
sbdchd:steve/fix-formatting-pyi-files-with-black

Conversation

@sbdchd

@sbdchd Steve Dignam (sbdchd) commented Nov 15, 2020 •

Copy link
Copy Markdown

Black has different formatting depending on the file ending.

When formatting a modified buffer or saving a file black will be run
against a temp file that looks roughly like:

./.venv/bin/black --diff --quiet ./queryset.pyi.26c0667c29728299036ec32a0f6f7729.tmp

The problem with this is that the ending is lost so black will default
to using the .py formatting, even when it's a .pyi file.

partially fixes #13341

  • Pull request represents a single change (i.e. not fixing disparate/unrelated things in a single PR).

  • Title summarizes what is changing.

  • Has a news entry file (remember to thank yourself!).

  • Appropriate comments and documentation strings in the code.

  • Has sufficient logging.

  • Has telemetry for enhancements.

  • Unit tests & system/integration tests are added/updated.
    Seems the integration suite isn't being run currently:

    // https://github.com/microsoft/vscode-python/issues/12564
    // Skipping one test in the file is resulting in the next one failing, so skipping the entire suiteuntil further investigation.
    // tslint:disable-next-line: no-invalid-this
    return this.skip();
    await initialize();

  • Test plan is updated as appropriate.

  • package-lock.json has been regenerated by running npm install (if dependencies have changed).

  • The wiki is updated with any design decisions/details.

Black has different formatting depending on the file ending.

When formatting a modified buffer or saving a file black will be run
against a temp file that looks roughly like:

```
./.venv/bin/black --diff --quiet ./queryset.pyi.26c0667c29728299036ec32a0f6f7729.tmp
```

The problem with this is that the ending is lost so black will default
to using the `.py` formatting, even when it's a `.pyi` file.

fixes: #13341
@ghost

Deleted user (ghost) commented Nov 15, 2020 •

Copy link
Copy Markdown

CLA assistant check
All CLA requirements met.

Comment thread news/2 Fixes/13341.md Resolved
Comment thread src/client/formatters/blackFormatter.ts Outdated

const blackArgs = ['--diff', '--quiet'];

if (document.fileName.endsWith('.pyi')) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
if (document.fileName.endsWith('.pyi')) {
if (path.extname(document.fileName) === 'pyi') {
Comment on lines +43 to +46

if (document.fileName.endsWith('.pyi')) {
blackArgs.push('--pyi');
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

[Not blocking] Alternatively, we could also handle the tmp case with a regex. I have not tested this thoroughly, but it should be able to handle most cases:

Suggested change
if (document.fileName.endsWith('.pyi')) {
blackArgs.push('--pyi');
}
const ext = path.extname(document.fileName);
const pyiTmpPattern = /\.pyi(\.[0-9a-fA-F]*\.tmp)?/;
// Matches patterns like:
// something.pyi.26c0667c29728299036ec32a0f6f7729.tmp
// something.pyi
// .pyi.26c0667c29728299036ec32a0f6f7729.tmp
// .pyi
// py.pyi
// some.pyi.py <--- This is a odd case but we can eliminate it be checking ext === 'tmp'
if (ext === 'pyi' || (ext === 'tmp' && pyiTmpPattern.test(document.fileName))) {
blackArgs.push('--pyi');
}
@sonarqubecloud

Copy link
Copy Markdown

Kudos, SonarCloud Quality Gate passed!

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities (and Security Hotspot 0 Security Hotspots to review)
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants