mirror of
https://github.com/microsoft/pyright.git
synced 2024-08-17 03:40:44 +03:00
Added support for enableTypeIngoreComments switch.
This commit is contained in:
parent
294cc3d12c
commit
3d9c094918
@ -36,6 +36,8 @@ The following settings control pyright’s diagnostic output (warnings or errors
|
||||
|
||||
**strictParameterNoneValue** [boolean]: PEP 484 indicates that when a function parameter is assigned a default value of None, its type should implicitly be Optional even if the explicit type is not. When enabled, this rule requires that parameter type annotations use Optional explicitly in this case. The default value for this setting is 'false'.
|
||||
|
||||
**enableTypeIgnoreComments** [boolean]: PEP 484 defines support for "# type ignore" comments. This switch enables or disables support for these comments. The default value for this setting is 'true'.
|
||||
|
||||
**reportTypeshedErrors** [boolean or string, optional]: Generate or suppress diagnostics for typeshed type stub files. In general, these type stub files should be “clean” and generate no errors. The default value for this setting is 'none'.
|
||||
|
||||
**reportMissingImports** [boolean or string, optional]: Generate or suppress diagnostics for imports that have no corresponding imported python file or type stub file. The default value for this setting is 'none', although pyright can do a much better job of static type checking if type stub files are provided for all imports.
|
||||
|
@ -212,18 +212,20 @@ export class SourceFile {
|
||||
this._analysisJob.typeAnalysisFinalDiagnostics);
|
||||
|
||||
// Filter the diagnostics based on "type: ignore" lines.
|
||||
const typeIgnoreLines = this._analysisJob.parseResults ?
|
||||
this._analysisJob.parseResults.tokenizerOutput.typeIgnoreLines : {};
|
||||
if (Object.keys(typeIgnoreLines).length > 0) {
|
||||
diagList = diagList.filter(d => {
|
||||
for (let line = d.range.start.line; line <= d.range.end.line; line++) {
|
||||
if (typeIgnoreLines[line]) {
|
||||
return false;
|
||||
if (options.diagnosticSettings.enableTypeIgnoreComments) {
|
||||
const typeIgnoreLines = this._analysisJob.parseResults ?
|
||||
this._analysisJob.parseResults.tokenizerOutput.typeIgnoreLines : {};
|
||||
if (Object.keys(typeIgnoreLines).length > 0) {
|
||||
diagList = diagList.filter(d => {
|
||||
for (let line = d.range.start.line; line <= d.range.end.line; line++) {
|
||||
if (typeIgnoreLines[line]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
return true;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (options.diagnosticSettings.reportImportCycles !== 'none' && this._analysisJob.circularDependencies.length > 0) {
|
||||
@ -264,8 +266,10 @@ export class SourceFile {
|
||||
|
||||
// If there is a "type: ignore" comment at the top of the file, clear
|
||||
// the diagnostic list.
|
||||
if (this._analysisJob.parseResults && this._analysisJob.parseResults.tokenizerOutput.typeIgnoreAll) {
|
||||
diagList = [];
|
||||
if (options.diagnosticSettings.enableTypeIgnoreComments) {
|
||||
if (this._analysisJob.parseResults && this._analysisJob.parseResults.tokenizerOutput.typeIgnoreAll) {
|
||||
diagList = [];
|
||||
}
|
||||
}
|
||||
|
||||
return diagList;
|
||||
|
@ -52,6 +52,9 @@ export interface DiagnosticSettings {
|
||||
// Use strict type rules for parameters assigned default of None?
|
||||
strictParameterNoneValue: boolean;
|
||||
|
||||
// Enable support for type: ignore comments?
|
||||
enableTypeIgnoreComments: boolean;
|
||||
|
||||
// Report diagnostics in typeshed files?
|
||||
reportTypeshedErrors: DiagnosticLevel;
|
||||
|
||||
@ -154,6 +157,11 @@ export function getBooleanDiagnosticSettings() {
|
||||
DiagnosticRule.strictListInference,
|
||||
DiagnosticRule.strictDictionaryInference,
|
||||
DiagnosticRule.strictParameterNoneValue
|
||||
|
||||
// Do not include this this one because we don't
|
||||
// want to override it in strict mode or support
|
||||
// it within pyright comments.
|
||||
// DiagnosticRule.enableTypeIgnoreComments
|
||||
];
|
||||
}
|
||||
|
||||
@ -195,6 +203,7 @@ export function getStrictDiagnosticSettings(): DiagnosticSettings {
|
||||
strictListInference: true,
|
||||
strictDictionaryInference: true,
|
||||
strictParameterNoneValue: true,
|
||||
enableTypeIgnoreComments: true, // Not overridden by strict mode
|
||||
reportTypeshedErrors: 'error',
|
||||
reportMissingImports: 'error',
|
||||
reportMissingTypeStubs: 'error',
|
||||
@ -233,6 +242,7 @@ export function getDefaultDiagnosticSettings(): DiagnosticSettings {
|
||||
strictListInference: false,
|
||||
strictDictionaryInference: false,
|
||||
strictParameterNoneValue: false,
|
||||
enableTypeIgnoreComments: true,
|
||||
reportTypeshedErrors: 'none',
|
||||
reportMissingImports: 'error',
|
||||
reportMissingTypeStubs: 'none',
|
||||
@ -456,6 +466,11 @@ export class ConfigOptions {
|
||||
configObj.strictParameterNoneValue, DiagnosticRule.strictParameterNoneValue,
|
||||
defaultSettings.strictParameterNoneValue),
|
||||
|
||||
// Should "# type: ignore" be honored?
|
||||
enableTypeIgnoreComments: this._convertBoolean(
|
||||
configObj.enableTypeIgnoreComments, DiagnosticRule.enableTypeIgnoreComments,
|
||||
defaultSettings.enableTypeIgnoreComments),
|
||||
|
||||
// Read the "reportTypeshedErrors" entry.
|
||||
reportTypeshedErrors: this._convertDiagnosticLevel(
|
||||
configObj.reportTypeshedErrors, DiagnosticRule.reportTypeshedErrors,
|
||||
|
@ -12,6 +12,7 @@ export const enum DiagnosticRule {
|
||||
strictListInference = 'strictListInference',
|
||||
strictDictionaryInference = 'strictDictionaryInference',
|
||||
strictParameterNoneValue = 'strictParameterNoneValue',
|
||||
enableTypeIgnoreComments = 'enableTypeIgnoreComments',
|
||||
|
||||
reportTypeshedErrors = 'reportTypeshedErrors',
|
||||
reportMissingImports = 'reportMissingImports',
|
||||
|
@ -2390,7 +2390,7 @@ export class Parser {
|
||||
const typeString = match[2];
|
||||
|
||||
// Ignore all "ignore" comments.
|
||||
if (typeString.trim().match(/^ignore(\s|$)'/)) {
|
||||
if (typeString.trim().match(/^ignore(\s|$)/)) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
|
@ -653,13 +653,25 @@ test('TypedDict6', () => {
|
||||
});
|
||||
|
||||
test('TypeIgnore1', () => {
|
||||
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['typeIgnore1.py']);
|
||||
const configOptions = new ConfigOptions('.');
|
||||
|
||||
let analysisResults = TestUtils.typeAnalyzeSampleFiles(['typeIgnore1.py'], configOptions);
|
||||
validateResults(analysisResults, 0);
|
||||
|
||||
// Disable type ignore
|
||||
configOptions.diagnosticSettings.enableTypeIgnoreComments = false;
|
||||
analysisResults = TestUtils.typeAnalyzeSampleFiles(['typeIgnore1.py'], configOptions);
|
||||
validateResults(analysisResults, 2);
|
||||
});
|
||||
|
||||
test('TypeIgnore2', () => {
|
||||
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['typeIgnore2.py']);
|
||||
const configOptions = new ConfigOptions('.');
|
||||
|
||||
let analysisResults = TestUtils.typeAnalyzeSampleFiles(['typeIgnore2.py'], configOptions);
|
||||
validateResults(analysisResults, 0);
|
||||
|
||||
// Disable type ignore
|
||||
configOptions.diagnosticSettings.enableTypeIgnoreComments = false;
|
||||
analysisResults = TestUtils.typeAnalyzeSampleFiles(['typeIgnore2.py'], configOptions);
|
||||
validateResults(analysisResults, 3);
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user