Updated code complexity calculation in binder to take into account the number of unique expressions that are affected by branch and loop nodes in the code flow graph. This addresses https://github.com/microsoft/pyright/issues/4519.

This commit is contained in:
Eric Traut 2023-01-29 07:44:15 -08:00
parent cce89d8212
commit 248e5bd4ea

View File

@ -152,7 +152,12 @@ interface ClassVarInfo {
// amount to the complexity factor. Without this, the complexity
// calculation fails to take into account large numbers of non-cyclical
// flow nodes. This number is somewhat arbitrary and is tuned empirically.
const flowNodeComplexityFactor = 0.05;
const flowNodeComplexityContribution = 0.05;
// For branch and loop nodes that enumerate expressions affected by
// that branch/loop, we add in a small complexity contribution for each
// expression. This number is somewhat arbitrary and is tuned empirically.
const branchNodeExpressionComplexityContribution = 0.25;
export class Binder extends ParseTreeWalker {
private readonly _fileInfo: AnalyzerFileInfo;
@ -3274,6 +3279,8 @@ export class Binder extends ParseTreeWalker {
this._currentScopeCodeFlowExpressions = savedExpressions;
this._codeFlowComplexity += scopedExpressions.size * branchNodeExpressionComplexityContribution;
return scopedExpressions;
}
@ -4194,7 +4201,7 @@ export class Binder extends ParseTreeWalker {
}
private _getUniqueFlowNodeId() {
this._codeFlowComplexity += flowNodeComplexityFactor;
this._codeFlowComplexity += flowNodeComplexityContribution;
return getUniqueFlowNodeId();
}