Skip to content

Conversation

@sbdchd
Copy link

@sbdchd sbdchd commented Nov 15, 2020

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
Copy link

ghost commented Nov 15, 2020

CLA assistant check
All CLA requirements met.


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

if (document.fileName.endsWith('.pyi')) {
Copy link
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
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');
}

@karthiknadig karthiknadig requested a review from int19h November 16, 2020 23:15
@sonarqubecloud
Copy link

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

@sbdchd sbdchd requested a review from karthiknadig November 17, 2020 01:11
@karthiknadig karthiknadig merged commit a30303a into microsoft:main Nov 30, 2020
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.

Auto-formatting of pyi files with black produces unexpected results

3 participants