Skip to content

Commit

Permalink
fix unreachable, unused & deprecated diagnostic severity levels not w…
Browse files Browse the repository at this point in the history
…orking wqhen set in the language server config
  • Loading branch information
DetachHead committed Aug 15, 2024
1 parent 7b616d6 commit b3a5d68
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
6 changes: 6 additions & 0 deletions packages/pyright-internal/src/common/commandLineOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ export const enum DiagnosticSeverityOverrides {
Warning = 'warning',
Information = 'information',
None = 'none',
Unused = 'unused',
Unreachable = 'unreachable',
Deprecated = 'deprecated',
}

export function getDiagnosticSeverityOverrides() {
Expand All @@ -26,6 +29,9 @@ export function getDiagnosticSeverityOverrides() {
DiagnosticSeverityOverrides.Warning,
DiagnosticSeverityOverrides.Information,
DiagnosticSeverityOverrides.None,
DiagnosticSeverityOverrides.Unused,
DiagnosticSeverityOverrides.Unreachable,
DiagnosticSeverityOverrides.Deprecated,
];
}

Expand Down
9 changes: 9 additions & 0 deletions packages/pyright-internal/src/common/configOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1901,6 +1901,15 @@ export function parseDiagLevel(value: string | boolean): DiagnosticSeverityOverr
case 'information':
return DiagnosticSeverityOverrides.Information;

case 'unused':
return DiagnosticSeverityOverrides.Unused;

case 'unreachable':
return DiagnosticSeverityOverrides.Unreachable;

case 'deprecated':
return DiagnosticSeverityOverrides.Deprecated;

default:
return undefined;
}
Expand Down
2 changes: 2 additions & 0 deletions packages/pyright-internal/src/languageServerBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,8 @@ export abstract class LanguageServerBase implements LanguageServerInterface, Dis

protected getSeverityOverrides(value: string | boolean): DiagnosticSeverityOverrides | undefined {
const enumValue = parseDiagLevel(value);
// TODO: this allows additional diagnostic severity levels (unreachable, unused & deprecated) on all rules
// in the lsp config. it should use the same logic as ConfigOptions._convertDiagnosticLevel instead
if (!enumValue) {
return undefined;
}
Expand Down
16 changes: 12 additions & 4 deletions packages/pyright-internal/src/tests/languageServer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
CancellationToken,
CompletionRequest,
ConfigurationItem,
DiagnosticSeverity,
InitializedNotification,
InitializeRequest,
MarkupContent,
Expand Down Expand Up @@ -197,7 +198,7 @@ describe(`Basic language server tests`, () => {
test('Diagnostic severity overrides test', async () => {
const code = `
// @filename: test.py
//// def test([|/*marker*/x|]): ...
//// def _test([|/*marker*/x|]): ...
////
// @filename: pyproject.toml
////
Expand All @@ -211,6 +212,7 @@ describe(`Basic language server tests`, () => {
value: {
diagnosticSeverityOverrides: {
reportUnknownParameterType: 'warning',
reportUnusedFunction: 'unused',
},
},
},
Expand All @@ -230,11 +232,17 @@ describe(`Basic language server tests`, () => {

// Wait for the diagnostics to publish
const diagnostics = await waitForDiagnostics(info);
const diagnostic = diagnostics.find((d) => d.uri.includes('test.py'));
assert(diagnostic);
const file = diagnostics.find((d) => d.uri.includes('test.py'));
assert(file);

// Make sure the error has a special rule
assert.equal(diagnostic.diagnostics[0].code, 'reportUnknownParameterType');
assert.equal(file.diagnostics[0].code, 'reportUnknownParameterType');

// make sure additional diagnostic severities work
assert.equal(
file.diagnostics.find((diagnostic) => diagnostic.code === 'reportUnusedFunction')?.severity,
DiagnosticSeverity.Hint // TODO: hint? how do we differentiate between unused/unreachable/deprecated?
);
});
describe('module/package renaming', () => {
describe('import statement', () => {
Expand Down

0 comments on commit b3a5d68

Please sign in to comment.