Skip to content

Commit

Permalink
feat: implemented allowed-untyped-libraries
Browse files Browse the repository at this point in the history
resolves #701
  • Loading branch information
Wizzerinus committed Dec 27, 2024
1 parent aba927d commit 0902b66
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
8 changes: 8 additions & 0 deletions packages/pyright-internal/src/analyzer/typeEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15127,6 +15127,14 @@ export function createTypeEvaluator(
return;
}

// Or if the object is in an untyped library that was explicitly mentioned.
if (type.shared && "moduleName" in type.shared) {
const moduleName = type.shared.moduleName;
if (ruleset.allowedUntypedLibraries.indexOf(moduleName) > -1) {
return;
}
}

const nameValue = target.d.value;

// Sometimes variables contain an "unbound" type if they're
Expand Down
3 changes: 2 additions & 1 deletion packages/pyright-internal/src/baseline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,8 @@ export class BaselineHandler {
files: {},
};
for (const fileWithDiagnostics of filesWithDiagnostics) {
const filePath = this.configOptions.projectRoot.getRelativePath(fileWithDiagnostics.fileUri)!.toString();
const filePath = this.configOptions.projectRoot.getRelativePath(fileWithDiagnostics.fileUri)?.toString();
if (filePath === undefined) continue;
const errorDiagnostics = fileWithDiagnostics.diagnostics.filter(
(diagnostic) => diagnostic.category !== DiagnosticCategory.Hint || diagnostic.baselined
);
Expand Down
28 changes: 28 additions & 0 deletions packages/pyright-internal/src/common/configOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,9 @@ export interface DiagnosticRuleSet {
reportUnusedParameter: DiagnosticLevel;
reportImplicitAbstractClass: DiagnosticLevel;
reportUnannotatedClassAttribute: DiagnosticLevel;

// What libraries' functions can be called even if they aren't typed?
allowedUntypedLibraries: String[];
}

export function cloneDiagnosticRuleSet(diagSettings: DiagnosticRuleSet): DiagnosticRuleSet {
Expand Down Expand Up @@ -687,6 +690,7 @@ export function getOffDiagnosticRuleSet(): DiagnosticRuleSet {
reportUnusedParameter: 'hint',
reportImplicitAbstractClass: 'none',
reportUnannotatedClassAttribute: 'none',
allowedUntypedLibraries: [],
};

return diagSettings;
Expand Down Expand Up @@ -803,6 +807,7 @@ export function getBasicDiagnosticRuleSet(): DiagnosticRuleSet {
reportUnusedParameter: 'hint',
reportImplicitAbstractClass: 'none',
reportUnannotatedClassAttribute: 'none',
allowedUntypedLibraries: [],
};

return diagSettings;
Expand Down Expand Up @@ -919,6 +924,7 @@ export function getStandardDiagnosticRuleSet(): DiagnosticRuleSet {
reportUnusedParameter: 'hint',
reportImplicitAbstractClass: 'none',
reportUnannotatedClassAttribute: 'none',
allowedUntypedLibraries: [],
};

return diagSettings;
Expand Down Expand Up @@ -1034,6 +1040,7 @@ export const getRecommendedDiagnosticRuleSet = (): DiagnosticRuleSet => ({
reportUnusedParameter: 'warning',
reportImplicitAbstractClass: 'warning',
reportUnannotatedClassAttribute: 'warning',
allowedUntypedLibraries: [],
});

export const getAllDiagnosticRuleSet = (): DiagnosticRuleSet => ({
Expand Down Expand Up @@ -1146,6 +1153,7 @@ export const getAllDiagnosticRuleSet = (): DiagnosticRuleSet => ({
reportUnusedParameter: 'error',
reportImplicitAbstractClass: 'error',
reportUnannotatedClassAttribute: 'error',
allowedUntypedLibraries: [],
});

export function getStrictDiagnosticRuleSet(): DiagnosticRuleSet {
Expand Down Expand Up @@ -1259,6 +1267,7 @@ export function getStrictDiagnosticRuleSet(): DiagnosticRuleSet {
reportUnusedParameter: 'hint',
reportImplicitAbstractClass: 'none',
reportUnannotatedClassAttribute: 'none',
allowedUntypedLibraries: [],
};

return diagSettings;
Expand Down Expand Up @@ -1601,6 +1610,25 @@ export class ConfigOptions {
console
);
});

// Read the config "allowedUntypedLibraries".
const allowedUntypedLibraries: String[] = [];
if (configObj.allowedUntypedLibraries !== undefined) {
if (!Array.isArray(configObj.allowedUntypedLibraries)) {
console.error(`Config "allowedUntypedLibraries" field must contain an array.`);
} else {
const pathList = configObj.allowedUntypedLibraries as string[];
pathList.forEach((lib, libIndex) => {
if (typeof lib !== 'string') {
console.error(`Config "allowedUntypedLibraries" field ${libIndex} must be a string.`);
} else {
allowedUntypedLibraries!.push(lib);
}
});
configRuleSet.allowedUntypedLibraries = allowedUntypedLibraries;
}
}

this.diagnosticRuleSet = { ...configRuleSet };

// Read the "venvPath".
Expand Down

0 comments on commit 0902b66

Please sign in to comment.