Simplified the typeGuards module so it doesn't depend on any knowledge of flow nodes. No functional change.

This commit is contained in:
Eric Traut 2021-12-09 22:43:13 -08:00
parent c49e6343ee
commit 28bdaa70ca
2 changed files with 12 additions and 10 deletions

View File

@ -725,7 +725,8 @@ export function getCodeFlowEngine(
const typeNarrowingCallback = getTypeNarrowingCallback(
evaluator,
reference,
conditionalFlowNode
conditionalFlowNode.expression,
!!(conditionalFlowNode.flags & (FlowFlags.TrueCondition | FlowFlags.TrueNeverCondition))
);
if (typeNarrowingCallback) {
// Set the cache entry to undefined before evaluating the
@ -777,7 +778,11 @@ export function getCodeFlowEngine(
const typeNarrowingCallback = getTypeNarrowingCallback(
evaluator,
conditionalFlowNode.reference,
conditionalFlowNode
conditionalFlowNode.expression,
!!(
conditionalFlowNode.flags &
(FlowFlags.TrueCondition | FlowFlags.TrueNeverCondition)
)
);
if (typeNarrowingCallback) {
const refTypeInfo = evaluator.getTypeOfExpression(conditionalFlowNode.reference!);

View File

@ -12,7 +12,6 @@
import { ArgumentCategory, ExpressionNode, ParameterCategory, ParseNodeType } from '../parser/parseNodes';
import { KeywordType, OperatorType } from '../parser/tokenizerTypes';
import { getFileInfo } from './analyzerNodeInfo';
import { FlowCondition, FlowFlags } from './codeFlowTypes';
import * as ParseTreeUtils from './parseTreeUtils';
import { Symbol, SymbolFlags } from './symbol';
import { getTypedDictMembersForClass } from './typedDicts';
@ -68,18 +67,16 @@ import { TypeVarMap } from './typeVarMap';
export type TypeNarrowingCallback = (type: Type) => Type | undefined;
// Given a reference expression and a flow node, returns a callback that
// can be used to narrow the type described by the target expression.
// If the specified flow node is not associated with the target expression,
// Given a reference expression and a test expression, returns a callback that
// can be used to narrow the type described by the reference expression.
// If the specified flow node is not associated with the test expression,
// it returns undefined.
export function getTypeNarrowingCallback(
evaluator: TypeEvaluator,
reference: ExpressionNode,
flowNode: FlowCondition
testExpression: ExpressionNode,
isPositiveTest: boolean
): TypeNarrowingCallback | undefined {
let testExpression = flowNode.expression;
const isPositiveTest = !!(flowNode.flags & (FlowFlags.TrueCondition | FlowFlags.TrueNeverCondition));
if (testExpression.nodeType === ParseNodeType.AssignmentExpression) {
if (ParseTreeUtils.isMatchingExpression(reference, testExpression.rightExpression)) {
testExpression = testExpression.rightExpression;