Skip to content

Commit

Permalink
Fixed a bug that causes a false positive "no overload implementation"…
Browse files Browse the repository at this point in the history
… error when a non-overloaded function uses a decorator that changes its type to a an overloaded function. This addresses #9803. (#9853)
  • Loading branch information
erictraut authored Feb 8, 2025
1 parent cf2d07e commit 72778de
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
11 changes: 9 additions & 2 deletions packages/pyright-internal/src/analyzer/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3137,6 +3137,8 @@ export class Checker extends ParseTreeWalker {
}

if (!implementation) {
// If this is a method within a protocol class, don't require that
// there is an implementation.
const containingClassNode = ParseTreeUtils.getEnclosingClassOrFunction(primaryDecl.node);
if (containingClassNode && containingClassNode.nodeType === ParseNodeType.Class) {
const classType = this._evaluator.getTypeOfClass(containingClassNode);
Expand All @@ -3158,8 +3160,13 @@ export class Checker extends ParseTreeWalker {
}
}

// If this is a method within a protocol class, don't require that
// there is an implementation.
// If the declaration isn't associated with any of the overloads in the
// type, the overloads came from a decorator that captured the overload
// from somewhere else.
if (!overloads.find((overload) => overload.shared.declaration === primaryDecl)) {
return;
}

this._evaluator.addDiagnostic(
DiagnosticRule.reportNoOverloadImplementation,
LocMessage.overloadWithoutImplementation().format({
Expand Down
9 changes: 9 additions & 0 deletions packages/pyright-internal/src/tests/samples/overload2.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,12 @@ def deco2(
@deco2(x=dict)
def func7() -> dict[str, str]:
return {}


class ClassC[T]:
def __init__(self, _: T): ...
def __call__(self, a) -> T: ...


@ClassC(print)
def func8(a: int) -> None: ...

0 comments on commit 72778de

Please sign in to comment.