Skip to content

Commit

Permalink
fix reportUnsafeMultipleInheritance false positive in diamond inher…
Browse files Browse the repository at this point in the history
…itance scenarios, which is safe due to how MRO works
  • Loading branch information
DetachHead committed Dec 9, 2024
1 parent 2fd23b1 commit 1040794
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/pyright-internal/src/analyzer/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5712,6 +5712,7 @@ export class Checker extends ParseTreeWalker {
}
/** if it's a dataclass then no further base classes in the MRO can have constructors */
let constructorIsSafe = !this._isDataclassWithGeneratedConstructor(classType);
const uniqueBaseClasses = new Set<ClassType>();
const baseClassesWithPossiblyUncalledConstructors: ClassType[] = [];
const isTypedDict = ClassType.isTypedDictClass(classType);
const diagAddendum = new DiagnosticAddendum();
Expand All @@ -5732,12 +5733,15 @@ export class Checker extends ParseTreeWalker {
constructorMethodResult &&
constructorMethodResult.classType &&
isClass(constructorMethodResult.classType) &&
// prevent the same base class from being checked twice which can happen in diamond inheritance situations
!uniqueBaseClasses.has(constructorMethodResult.classType) &&
// synthesized dataclass constructors are safe as the runtime machinery seems to account for multiple inheritance moments
!(
isFunction(constructorMethodResult.type) &&
FunctionType.isSynthesizedMethod(constructorMethodResult.type)
)
) {
uniqueBaseClasses.add(constructorMethodResult.classType);
if (constructorIsSafe) {
constructorIsSafe = false;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,16 @@ class U(A, C): # error

@dataclass(init=False)
class V(A, C): # no error
...
...

class Up:
def __init__(self): ...


class Left(Up): ...


class Right(Up): ...


class Down(Left, Right): ... # no error

0 comments on commit 1040794

Please sign in to comment.