@zzzen/pyright-internal 1.2.0-dev.20261004 → 1.2.0-dev.20261011
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/analyzer/checker.d.ts +0 -1
- package/dist/analyzer/checker.js +17 -39
- package/dist/analyzer/checker.js.map +1 -1
- package/dist/analyzer/codeFlowEngine.js +2 -1
- package/dist/analyzer/codeFlowEngine.js.map +1 -1
- package/dist/analyzer/constraintSolver.js +3 -2
- package/dist/analyzer/constraintSolver.js.map +1 -1
- package/dist/analyzer/operations.d.ts +2 -0
- package/dist/analyzer/operations.js +2 -1
- package/dist/analyzer/operations.js.map +1 -1
- package/dist/analyzer/parameterUtils.js +2 -1
- package/dist/analyzer/parameterUtils.js.map +1 -1
- package/dist/analyzer/patternMatching.js +41 -9
- package/dist/analyzer/patternMatching.js.map +1 -1
- package/dist/analyzer/properties.d.ts +2 -2
- package/dist/analyzer/properties.js +2 -2
- package/dist/analyzer/properties.js.map +1 -1
- package/dist/analyzer/protocols.d.ts +3 -1
- package/dist/analyzer/protocols.js +24 -2
- package/dist/analyzer/protocols.js.map +1 -1
- package/dist/analyzer/typeEvaluator.js +143 -24
- package/dist/analyzer/typeEvaluator.js.map +1 -1
- package/dist/analyzer/typeEvaluatorTypes.d.ts +2 -1
- package/dist/analyzer/typeEvaluatorTypes.js +4 -0
- package/dist/analyzer/typeEvaluatorTypes.js.map +1 -1
- package/dist/analyzer/typeGuards.js +208 -68
- package/dist/analyzer/typeGuards.js.map +1 -1
- package/dist/analyzer/typePrinter.js +32 -6
- package/dist/analyzer/typePrinter.js.map +1 -1
- package/dist/analyzer/typeUtils.d.ts +1 -0
- package/dist/analyzer/typeUtils.js +15 -1
- package/dist/analyzer/typeUtils.js.map +1 -1
- package/dist/analyzer/types.d.ts +2 -1
- package/dist/analyzer/types.js +9 -0
- package/dist/analyzer/types.js.map +1 -1
- package/dist/tests/cacheManager.test.js +19 -15
- package/dist/tests/cacheManager.test.js.map +1 -1
- package/dist/tests/constraintSolver.test.d.ts +1 -0
- package/dist/tests/constraintSolver.test.js +39 -0
- package/dist/tests/constraintSolver.test.js.map +1 -0
- package/dist/tests/fourslash/hover.typedDictUnpacking.dictDisplay.fourslash.d.ts +1 -0
- package/dist/tests/fourslash/hover.typedDictUnpacking.dictDisplay.fourslash.js +16 -0
- package/dist/tests/fourslash/hover.typedDictUnpacking.dictDisplay.fourslash.js.map +1 -0
- package/dist/tests/mypyPrimerAnalysis.test.js +67 -11
- package/dist/tests/mypyPrimerAnalysis.test.js.map +1 -1
- package/dist/tests/typeEquality.test.js +85 -2
- package/dist/tests/typeEquality.test.js.map +1 -1
- package/dist/tests/typeEvaluator1.test.js +28 -1
- package/dist/tests/typeEvaluator1.test.js.map +1 -1
- package/dist/tests/typeEvaluator3.test.js +23 -0
- package/dist/tests/typeEvaluator3.test.js.map +1 -1
- package/dist/tests/typeEvaluator6.test.js +63 -1
- package/dist/tests/typeEvaluator6.test.js.map +1 -1
- package/dist/tests/typeEvaluator7.test.js +22 -0
- package/dist/tests/typeEvaluator7.test.js.map +1 -1
- package/dist/tests/typeEvaluator8.test.js +13 -0
- package/dist/tests/typeEvaluator8.test.js.map +1 -1
- package/package.json +1 -1
|
@@ -332,7 +332,8 @@ export declare const enum AssignTypeFlags {
|
|
|
332
332
|
AllowIsinstanceSpecialForms = 16384,
|
|
333
333
|
SkipSelfClsParamCheck = 32768,
|
|
334
334
|
AllowProtocolClassSource = 65536,
|
|
335
|
-
DisallowExtraKwargsForTd = 131072
|
|
335
|
+
DisallowExtraKwargsForTd = 131072,
|
|
336
|
+
RejectCyclicLowerBound = 262144
|
|
336
337
|
}
|
|
337
338
|
export interface TypeEvaluator {
|
|
338
339
|
runWithCancellationToken<T>(token: CancellationToken, callback: () => T): T;
|
|
@@ -197,5 +197,9 @@ var AssignTypeFlags;
|
|
|
197
197
|
// When assigning callables, should a kwargs with an unpacked TypedDict
|
|
198
198
|
// disallow additional named arguments if it does not have extraItems?
|
|
199
199
|
AssignTypeFlags[AssignTypeFlags["DisallowExtraKwargsForTd"] = 131072] = "DisallowExtraKwargsForTd";
|
|
200
|
+
// Reject a cyclic TypeVar lower bound even when no constraint tracker
|
|
201
|
+
// records it. Overload consistency validation uses this when comparing
|
|
202
|
+
// return types.
|
|
203
|
+
AssignTypeFlags[AssignTypeFlags["RejectCyclicLowerBound"] = 262144] = "RejectCyclicLowerBound";
|
|
200
204
|
})(AssignTypeFlags || (exports.AssignTypeFlags = AssignTypeFlags = {}));
|
|
201
205
|
//# sourceMappingURL=typeEvaluatorTypes.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"typeEvaluatorTypes.js","sourceRoot":"","sources":["../../../../../src/analyzer/typeEvaluatorTypes.ts"],"names":[],"mappings":";AAAA;;;;;;;GAOG;;;AA2bH,oEAEC;AA1YD,gEAAgE;AAChE,kDAAkD;AACrC,QAAA,0BAA0B,GAAG,EAAE,CAAC;AAE7C,2EAA2E;AAC3E,4EAA4E;AAC5E,yBAAyB;AACZ,QAAA,yBAAyB,GAAG,CAAC,CAAC;AAE3C,IAAkB,SA0IjB;AA1ID,WAAkB,SAAS;IACvB,yCAAQ,CAAA;IAER,uDAAuD;IACvD,yEAA6B,CAAA;IAE7B,0DAA0D;IAC1D,gEAAgE;IAChE,SAAS;IACT,yDAAqB,CAAA;IAErB,yDAAyD;IACzD,uDAAoB,CAAA;IAEpB,kCAAkC;IAClC,iEAAyB,CAAA;IAEzB,0CAA0C;IAC1C,gDAAgB,CAAA;IAEhB,6CAA6C;IAC7C,wDAAoB,CAAA;IAEpB,gDAAgD;IAChD,8DAAuB,CAAA;IAEvB,2DAA2D;IAC3D,4BAA4B;IAC5B,mEAAyB,CAAA;IAEzB,kEAAkE;IAClE,gEAAgE;IAChE,qBAAqB;IACrB,+DAAuB,CAAA;IAEvB,qEAAqE;IACrE,2EAA6B,CAAA;IAE7B,2DAA2D;IAC3D,gDAAgD;IAChD,4DAAsB,CAA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A8B,CAAA;IAE9B,uEAAuE;IACvE,gDAAgD;IAChD,8GAAyC,CAAA;IAEzC,sEAAsE;IACtE,kEAAkE;IAClE,gEAAgE;IAChE,aAAa;IACb,gGAAkC,CAAA;IAElC,mEAAmE;IACnE,gEAAgE;IAChE,sBAAsB;IACtB,uGAAqC,CAAA;IAErC,0EAA0E;IAC1E,kEAAkE;IAClE,2FAA+B,CAAA;IAE/B,yEAAyE;IACzE,kCAAkC;IAClC,iGAAkC,CAAA;IAElC,uEAAuE;IACvE,sEAAsE;IACtE,kGAAkC,CAAA;IAElC,sEAAsE;IACtE,uEAAuE;IACvE,gBAAgB;IAChB,8FAAgC,CAAA;AACpC,CAAC,EAnFiB,eAAe,+BAAf,eAAe,QAmFhC"}
|
|
@@ -58,6 +58,8 @@ const analyzerNodeInfo_1 = require("./analyzerNodeInfo");
|
|
|
58
58
|
const constraintSolver_1 = require("./constraintSolver");
|
|
59
59
|
const constraintTracker_1 = require("./constraintTracker");
|
|
60
60
|
const enums_1 = require("./enums");
|
|
61
|
+
const operations_1 = require("./operations");
|
|
62
|
+
const parameterUtils_1 = require("./parameterUtils");
|
|
61
63
|
const ParseTreeUtils = __importStar(require("./parseTreeUtils"));
|
|
62
64
|
const scopeUtils_1 = require("./scopeUtils");
|
|
63
65
|
const symbol_1 = require("./symbol");
|
|
@@ -250,6 +252,16 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
|
250
252
|
};
|
|
251
253
|
};
|
|
252
254
|
}
|
|
255
|
+
// Look for X == <class> or X != <class>.
|
|
256
|
+
if ((0, types_1.isInstantiableClass)(rightType)) {
|
|
257
|
+
return (type) => {
|
|
258
|
+
return {
|
|
259
|
+
type: narrowTypeForClassComparison(evaluator, type, rightType, adjIsPositiveTest,
|
|
260
|
+
/* isIsOperator */ false),
|
|
261
|
+
isIncomplete: !!rightTypeResult.isIncomplete,
|
|
262
|
+
};
|
|
263
|
+
};
|
|
264
|
+
}
|
|
253
265
|
}
|
|
254
266
|
// Look for X[<literal>] == <literal> or X[<literal>] != <literal>
|
|
255
267
|
if (testExpression.d.leftExpr.nodeType === 27 /* ParseNodeType.Index */ &&
|
|
@@ -456,82 +468,155 @@ function getTypeNarrowingCallback(evaluator, reference, testExpression, isPositi
|
|
|
456
468
|
}
|
|
457
469
|
}
|
|
458
470
|
// Look for a TypeGuard function.
|
|
459
|
-
|
|
460
|
-
const
|
|
461
|
-
if (
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
471
|
+
const isFunctionReturnTypeGuard = (type) => {
|
|
472
|
+
const returnType = type.shared.declaredReturnType;
|
|
473
|
+
if (!returnType) {
|
|
474
|
+
return false;
|
|
475
|
+
}
|
|
476
|
+
if ((0, types_1.isClassInstance)(returnType)) {
|
|
477
|
+
return types_1.ClassType.isBuiltIn(returnType, ['TypeGuard', 'TypeIs']);
|
|
478
|
+
}
|
|
479
|
+
if ((0, types_1.isUnion)(returnType)) {
|
|
480
|
+
let isAllGuards = true;
|
|
481
|
+
(0, typeUtils_1.doForEachSubtype)(returnType, (subtype) => {
|
|
482
|
+
if (!(0, types_1.isClassInstance)(subtype) || !types_1.ClassType.isBuiltIn(subtype, ['TypeGuard', 'TypeIs'])) {
|
|
483
|
+
isAllGuards = false;
|
|
468
484
|
}
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
485
|
+
});
|
|
486
|
+
return isAllGuards;
|
|
487
|
+
}
|
|
488
|
+
return false;
|
|
489
|
+
};
|
|
490
|
+
// Reject unrelated references before evaluating the callee.
|
|
491
|
+
if (testExpression.d.args.some((arg) => isMatchingExpressionOrWalrusRhs(evaluator, reference, arg.d.valueExpr))) {
|
|
492
|
+
const callTypeResult = evaluator.getTypeOfExpression(testExpression.d.leftExpr, 2 /* EvalFlags.CallBaseDefaults */);
|
|
493
|
+
const callType = callTypeResult.type;
|
|
494
|
+
let isPossiblyTypeGuard = false;
|
|
495
|
+
if ((0, types_1.isFunction)(callType)) {
|
|
496
|
+
isPossiblyTypeGuard = isFunctionReturnTypeGuard(callType);
|
|
497
|
+
}
|
|
498
|
+
else if ((0, types_1.isOverloaded)(callType)) {
|
|
499
|
+
isPossiblyTypeGuard = types_1.OverloadedType.getOverloads(callType).some(isFunctionReturnTypeGuard);
|
|
500
|
+
}
|
|
501
|
+
else if ((0, types_1.isClassInstance)(callType)) {
|
|
502
|
+
// A callable object's __call__ may be overloaded, a property or have
|
|
503
|
+
// an inferred return type, so rely on the evaluated call below.
|
|
504
|
+
isPossiblyTypeGuard = true;
|
|
505
|
+
}
|
|
506
|
+
if (isPossiblyTypeGuard) {
|
|
507
|
+
const functionReturnTypeResult = evaluator.getTypeOfExpression(testExpression);
|
|
508
|
+
const functionReturnType = functionReturnTypeResult.type;
|
|
509
|
+
let typeGuardType;
|
|
510
|
+
let isStrictTypeGuard = false;
|
|
511
|
+
if ((0, types_1.isClassInstance)(functionReturnType) &&
|
|
512
|
+
types_1.ClassType.isBuiltIn(functionReturnType, ['TypeGuard', 'TypeIs']) &&
|
|
513
|
+
functionReturnType.priv.typeArgs &&
|
|
514
|
+
functionReturnType.priv.typeArgs.length > 0) {
|
|
515
|
+
isStrictTypeGuard = types_1.ClassType.isBuiltIn(functionReturnType, 'TypeIs');
|
|
516
|
+
typeGuardType = functionReturnType.priv.typeArgs[0];
|
|
494
517
|
}
|
|
495
|
-
if (
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
518
|
+
else if ((0, types_1.isUnion)(functionReturnType)) {
|
|
519
|
+
const typeGuardSubtypes = [];
|
|
520
|
+
let isAllGuards = true;
|
|
521
|
+
(0, typeUtils_1.doForEachSubtype)(functionReturnType, (subtype) => {
|
|
522
|
+
if ((0, types_1.isClassInstance)(subtype) &&
|
|
523
|
+
types_1.ClassType.isBuiltIn(subtype, ['TypeGuard', 'TypeIs']) &&
|
|
524
|
+
subtype.priv.typeArgs &&
|
|
525
|
+
subtype.priv.typeArgs.length > 0) {
|
|
526
|
+
typeGuardSubtypes.push(subtype);
|
|
527
|
+
}
|
|
528
|
+
else {
|
|
529
|
+
isAllGuards = false;
|
|
530
|
+
}
|
|
531
|
+
});
|
|
532
|
+
if (isAllGuards && typeGuardSubtypes.length > 0) {
|
|
533
|
+
// A union of type guards cannot be strict in the negative case because at runtime
|
|
534
|
+
// only one arm of the overload/union is selected. Treating it as strict would
|
|
535
|
+
// unsoundly eliminate types in the negative branch.
|
|
536
|
+
isStrictTypeGuard = false;
|
|
537
|
+
typeGuardType = (0, types_1.combineTypes)(typeGuardSubtypes.map((subtype) => subtype.priv.typeArgs[0]));
|
|
507
538
|
}
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
539
|
+
}
|
|
540
|
+
if (typeGuardType) {
|
|
541
|
+
// Map only guard-returning calls. Ordinary callable predicates
|
|
542
|
+
// do not need their virtual parameter lists expanded.
|
|
543
|
+
const getTargetArgExpr = (fnType) => {
|
|
544
|
+
// Callable-valued attributes can have binding metadata without a method receiver.
|
|
545
|
+
const isUnbound = fnType.shared.methodClass !== undefined &&
|
|
546
|
+
!types_1.FunctionType.isStaticMethod(fnType) &&
|
|
547
|
+
fnType.priv.strippedFirstParamType === undefined;
|
|
548
|
+
const targetIndex = isUnbound ? 1 : 0;
|
|
549
|
+
const getPositionalArgExpr = () => {
|
|
550
|
+
// Unpacking before the guarded position obscures its argument expression.
|
|
551
|
+
let positionalIndex = 0;
|
|
552
|
+
for (const arg of testExpression.d.args) {
|
|
553
|
+
if (arg.d.name) {
|
|
554
|
+
continue;
|
|
555
|
+
}
|
|
556
|
+
if (arg.d.argCategory !== 0 /* ArgCategory.Simple */) {
|
|
557
|
+
return undefined;
|
|
558
|
+
}
|
|
559
|
+
if (positionalIndex === targetIndex) {
|
|
560
|
+
return arg.d.valueExpr;
|
|
561
|
+
}
|
|
562
|
+
positionalIndex++;
|
|
517
563
|
}
|
|
518
|
-
|
|
519
|
-
|
|
564
|
+
return undefined;
|
|
565
|
+
};
|
|
566
|
+
// Simple leading parameters bind positionally without expanding the rest of the signature.
|
|
567
|
+
const firstParam = fnType.shared.parameters[0];
|
|
568
|
+
const targetParam = fnType.shared.parameters[targetIndex];
|
|
569
|
+
if (firstParam?.category === 0 /* ParamCategory.Simple */ &&
|
|
570
|
+
firstParam.name &&
|
|
571
|
+
targetParam?.category === 0 /* ParamCategory.Simple */ &&
|
|
572
|
+
targetParam.name) {
|
|
573
|
+
const positionalArgExpr = getPositionalArgExpr();
|
|
574
|
+
if (positionalArgExpr) {
|
|
575
|
+
return positionalArgExpr;
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
const paramDetails = (0, parameterUtils_1.getParamListDetails)(fnType);
|
|
579
|
+
if (targetIndex >= paramDetails.params.length) {
|
|
580
|
+
return undefined;
|
|
581
|
+
}
|
|
582
|
+
const target = paramDetails.params[targetIndex];
|
|
583
|
+
if (target.param.category !== 0 /* ParamCategory.Simple */ &&
|
|
584
|
+
target.param.category !== 1 /* ParamCategory.ArgsList */) {
|
|
585
|
+
return undefined;
|
|
586
|
+
}
|
|
587
|
+
if ((target.kind === parameterUtils_1.ParamKind.Standard || target.kind === parameterUtils_1.ParamKind.Keyword) &&
|
|
588
|
+
target.param.name) {
|
|
589
|
+
const kwArg = testExpression.d.args.find((arg) => arg.d.argCategory === 0 /* ArgCategory.Simple */ &&
|
|
590
|
+
arg.d.name?.d.value === target.param.name);
|
|
591
|
+
if (kwArg) {
|
|
592
|
+
return kwArg.d.valueExpr;
|
|
520
593
|
}
|
|
521
|
-
});
|
|
522
|
-
if (isAllGuards && typeGuardSubtypes.length > 0) {
|
|
523
|
-
// A union of type guards cannot be strict in the negative case because at runtime
|
|
524
|
-
// only one arm of the overload/union is selected. Treating it as strict would
|
|
525
|
-
// unsoundly eliminate types in the negative branch.
|
|
526
|
-
isStrictTypeGuard = false;
|
|
527
|
-
typeGuardType = (0, types_1.combineTypes)(typeGuardSubtypes.map((subtype) => subtype.priv.typeArgs[0]));
|
|
528
594
|
}
|
|
595
|
+
if (target.kind === parameterUtils_1.ParamKind.Keyword) {
|
|
596
|
+
return undefined;
|
|
597
|
+
}
|
|
598
|
+
return getPositionalArgExpr();
|
|
599
|
+
};
|
|
600
|
+
let arg0Expr;
|
|
601
|
+
const overloadsUsed = functionReturnTypeResult.overloadsUsedForCall;
|
|
602
|
+
if (overloadsUsed && overloadsUsed.length > 0) {
|
|
603
|
+
arg0Expr = getTargetArgExpr(overloadsUsed[0]);
|
|
604
|
+
for (let i = 1; arg0Expr && i < overloadsUsed.length; i++) {
|
|
605
|
+
if (getTargetArgExpr(overloadsUsed[i]) !== arg0Expr) {
|
|
606
|
+
arg0Expr = undefined;
|
|
607
|
+
break;
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
else if ((0, types_1.isFunction)(callType)) {
|
|
612
|
+
arg0Expr = getTargetArgExpr(callType);
|
|
529
613
|
}
|
|
530
|
-
if (
|
|
614
|
+
if (arg0Expr && isMatchingExpressionOrWalrusRhs(evaluator, reference, arg0Expr)) {
|
|
531
615
|
const isIncomplete = !!callTypeResult.isIncomplete || !!functionReturnTypeResult.isIncomplete;
|
|
616
|
+
const guardType = typeGuardType;
|
|
532
617
|
return (type) => {
|
|
533
618
|
return {
|
|
534
|
-
type: narrowTypeForUserDefinedTypeGuard(evaluator, type,
|
|
619
|
+
type: narrowTypeForUserDefinedTypeGuard(evaluator, type, guardType, isPositiveTest, isStrictTypeGuard, testExpression, nodeInfo),
|
|
535
620
|
isIncomplete,
|
|
536
621
|
};
|
|
537
622
|
};
|
|
@@ -1869,11 +1954,63 @@ function narrowTypeForTypeIs(evaluator, type, classTypes, isPositiveTest) {
|
|
|
1869
1954
|
});
|
|
1870
1955
|
return (0, types_1.combineTypes)(typesToCombine);
|
|
1871
1956
|
}
|
|
1957
|
+
function hasUnresolvedMetaclassInMro(classType) {
|
|
1958
|
+
return classType.shared.mro.some((baseClass) => !(0, types_1.isInstantiableClass)(baseClass) || !!(baseClass.shared.flags & 134217728 /* ClassTypeFlags.HasUnresolvedMetaclass */));
|
|
1959
|
+
}
|
|
1960
|
+
// Determines whether an equality comparison ("==" or "!=") involving the
|
|
1961
|
+
// specified class object is guaranteed to have identity semantics. This
|
|
1962
|
+
// requires that the class is statically closed (it cannot be a subclass
|
|
1963
|
+
// with a different metaclass), is not generic (specialized aliases compare
|
|
1964
|
+
// by value), and its metaclass does not override "__eq__" or "__ne__".
|
|
1965
|
+
function hasIdentityEqualitySemantics(classType, metaclassEqualityResults) {
|
|
1966
|
+
if (classType.priv.includeSubclasses && !types_1.ClassType.isFinal(classType)) {
|
|
1967
|
+
return false;
|
|
1968
|
+
}
|
|
1969
|
+
if (classType.shared.typeParams.length > 0) {
|
|
1970
|
+
return false;
|
|
1971
|
+
}
|
|
1972
|
+
const metaclass = classType.shared.effectiveMetaclass;
|
|
1973
|
+
if (!metaclass ||
|
|
1974
|
+
!(0, types_1.isInstantiableClass)(metaclass) ||
|
|
1975
|
+
hasUnresolvedMetaclassInMro(classType) ||
|
|
1976
|
+
hasUnresolvedMetaclassInMro(metaclass)) {
|
|
1977
|
+
return false;
|
|
1978
|
+
}
|
|
1979
|
+
if (types_1.ClassType.isBuiltIn(metaclass, 'type')) {
|
|
1980
|
+
return true;
|
|
1981
|
+
}
|
|
1982
|
+
// Cloned metaclasses share method definitions, but the class-specific checks above cannot be shared.
|
|
1983
|
+
const cachedResult = metaclassEqualityResults.get(metaclass.shared);
|
|
1984
|
+
if (cachedResult !== undefined) {
|
|
1985
|
+
return cachedResult;
|
|
1986
|
+
}
|
|
1987
|
+
const result = !(0, operations_1.customMetaclassSupportsMethod)(classType, '__eq__') && !(0, operations_1.customMetaclassSupportsMethod)(classType, '__ne__');
|
|
1988
|
+
metaclassEqualityResults.set(metaclass.shared, result);
|
|
1989
|
+
return result;
|
|
1990
|
+
}
|
|
1872
1991
|
// Attempts to narrow a type based on a comparison with a class using "is" or
|
|
1873
|
-
// "is not"
|
|
1874
|
-
function narrowTypeForClassComparison(evaluator, referenceType, classType, isPositiveTest) {
|
|
1992
|
+
// "is not", or "==" or "!=".
|
|
1993
|
+
function narrowTypeForClassComparison(evaluator, referenceType, classType, isPositiveTest, isIsOperator = true) {
|
|
1994
|
+
// Limit reuse to this invocation so results cannot outlive the evaluation that established them.
|
|
1995
|
+
const metaclassEqualityResults = isIsOperator ? undefined : new Map();
|
|
1996
|
+
if (metaclassEqualityResults && !hasIdentityEqualitySemantics(classType, metaclassEqualityResults)) {
|
|
1997
|
+
return referenceType;
|
|
1998
|
+
}
|
|
1875
1999
|
return (0, typeUtils_1.mapSubtypes)(referenceType, (subtype) => {
|
|
2000
|
+
// Multiple class constraints expand to a union with distinct conditions, which equality cannot narrow.
|
|
2001
|
+
if (metaclassEqualityResults &&
|
|
2002
|
+
(0, types_1.isTypeVar)(subtype) &&
|
|
2003
|
+
!subtype.shared.recursiveAlias &&
|
|
2004
|
+
subtype.shared.constraints.length > 1 &&
|
|
2005
|
+
subtype.shared.constraints.every(types_1.isClassInstance)) {
|
|
2006
|
+
return subtype;
|
|
2007
|
+
}
|
|
1876
2008
|
let concreteSubtype = evaluator.makeTopLevelTypeVarsConcrete(subtype);
|
|
2009
|
+
if (metaclassEqualityResults &&
|
|
2010
|
+
(!(0, types_1.isInstantiableClass)(concreteSubtype) ||
|
|
2011
|
+
!hasIdentityEqualitySemantics(concreteSubtype, metaclassEqualityResults))) {
|
|
2012
|
+
return subtype;
|
|
2013
|
+
}
|
|
1877
2014
|
if (isPositiveTest) {
|
|
1878
2015
|
if ((0, types_1.isClassInstance)(concreteSubtype) &&
|
|
1879
2016
|
types_1.TypeBase.isInstance(subtype) &&
|
|
@@ -1888,6 +2025,9 @@ function narrowTypeForClassComparison(evaluator, referenceType, classType, isPos
|
|
|
1888
2025
|
}
|
|
1889
2026
|
if ((0, types_1.isClass)(concreteSubtype)) {
|
|
1890
2027
|
if (types_1.TypeBase.isInstance(concreteSubtype)) {
|
|
2028
|
+
if (!isIsOperator) {
|
|
2029
|
+
return subtype;
|
|
2030
|
+
}
|
|
1891
2031
|
return types_1.ClassType.isBuiltIn(concreteSubtype, 'object') ? classType : undefined;
|
|
1892
2032
|
}
|
|
1893
2033
|
const isSuperType = isFilterSuperclass(subtype, concreteSubtype, classType, classType);
|