Skip to content

Commit

Permalink
Fixed a bug that results in incorrect protocol matching behaviors und…
Browse files Browse the repository at this point in the history
…er certain circumstances. (#9182)
  • Loading branch information
erictraut authored Oct 9, 2024
1 parent a1d1b8a commit 631c527
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class ConstraintSet {
return type1 === type2;
}

return isTypeSame(type1, type2);
return isTypeSame(type1, type2, { honorIsTypeArgExplicit: true, honorTypeForm: true });
}

let isSame = true;
Expand Down
4 changes: 2 additions & 2 deletions packages/pyright-internal/src/analyzer/protocols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,8 @@ function getProtocolCompatibility(
}

if (
isTypeSame(entry.destType, destType) &&
isTypeSame(entry.srcType, srcType) &&
isTypeSame(entry.destType, destType, { honorIsTypeArgExplicit: true, honorTypeForm: true }) &&
isTypeSame(entry.srcType, srcType, { honorIsTypeArgExplicit: true, honorTypeForm: true }) &&
isConstraintTrackerSame(constraints, entry.preConstraints)
) {
return entry;
Expand Down
17 changes: 17 additions & 0 deletions packages/pyright-internal/src/analyzer/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export interface TypeSameOptions {
ignoreConditions?: boolean;
ignoreTypedDictNarrowEntries?: boolean;
honorTypeForm?: boolean;
honorIsTypeArgExplicit?: boolean;
treatAnySameAsUnknown?: boolean;
}

Expand Down Expand Up @@ -3361,6 +3362,12 @@ export function isTypeSame(type1: Type, type2: Type, options: TypeSameOptions =
return false;
}

// This test is required for the "partial" class, which clones
// the symbol table to add a custom __call__ method.
if (type1.shared.fields !== classType2.shared.fields) {
return false;
}

if (!type1.priv.isUnpacked !== !classType2.priv.isUnpacked) {
return false;
}
Expand All @@ -3369,6 +3376,12 @@ export function isTypeSame(type1: Type, type2: Type, options: TypeSameOptions =
return false;
}

if (options.honorIsTypeArgExplicit) {
if (!!type1.priv.isTypeArgExplicit !== !!classType2.priv.isTypeArgExplicit) {
return false;
}
}

if (!options.ignoreTypedDictNarrowEntries && !ClassType.isTypedDictNarrowedEntriesSame(type1, classType2)) {
return false;
}
Expand Down Expand Up @@ -3502,6 +3515,10 @@ export function isTypeSame(type1: Type, type2: Type, options: TypeSameOptions =
return false;
}

if (type1.priv.nameWithScope !== type2TypeVar.priv.nameWithScope) {
return false;
}

// Handle the case where this is a generic recursive type alias. Make
// sure that the type argument types match.
if (type1.shared.recursiveAlias && type2TypeVar.shared.recursiveAlias) {
Expand Down

0 comments on commit 631c527

Please sign in to comment.