-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fix: formatting .pyi files with black #14736
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
fix: formatting .pyi files with black #14736
Conversation
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
|
|
||
| const blackArgs = ['--diff', '--quiet']; | ||
|
|
||
| if (document.fileName.endsWith('.pyi')) { |
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.
| if (document.fileName.endsWith('.pyi')) { | |
| if (path.extname(document.fileName) === 'pyi') { |
|
|
||
| if (document.fileName.endsWith('.pyi')) { | ||
| blackArgs.push('--pyi'); | ||
| } |
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.
[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:
| 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'); | |
| } |
Co-authored-by: Karthik Nadig <[email protected]>
|
Kudos, SonarCloud Quality Gate passed!
|
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:
The problem with this is that the ending is lost so black will default
to using the
.pyformatting, even when it's a.pyifile.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:
vscode-python/src/test/format/extension.format.test.ts
Lines 43 to 47 in 2a62cf7
Test plan is updated as appropriate.package-lock.jsonhas been regenerated by runningnpm install(if dependencies have changed).The wiki is updated with any design decisions/details.