Skip to content

Commit

Permalink
Fixed a bug that leads to a false positive error when first argument …
Browse files Browse the repository at this point in the history
…to `super` call is an instance of a metaclass. This addresses #7931.
  • Loading branch information
erictraut committed May 16, 2024
1 parent 022b30a commit 6d79c5c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/pyright-internal/src/analyzer/typeEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8378,7 +8378,11 @@ export function createTypeEvaluator(
targetClassType = getTypeOfExpression(node.arguments[0].valueExpression).type;
const concreteTargetClassType = makeTopLevelTypeVarsConcrete(targetClassType);

if (!isAnyOrUnknown(concreteTargetClassType) && !isInstantiableClass(concreteTargetClassType)) {
if (
!isAnyOrUnknown(concreteTargetClassType) &&
!isInstantiableClass(concreteTargetClassType) &&
!isMetaclassInstance(concreteTargetClassType)
) {
addDiagnostic(
DiagnosticRule.reportArgumentType,
LocMessage.superCallFirstArg().format({ type: printType(targetClassType) }),
Expand Down
11 changes: 11 additions & 0 deletions packages/pyright-internal/src/tests/samples/super13.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This sample tests the use of `super` outside of a method.

def func1(t: type) -> super:
return super(t, t)


class ClassA:
pass


func1(ClassA)
6 changes: 6 additions & 0 deletions packages/pyright-internal/src/tests/typeEvaluator2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,12 @@ test('Super12', () => {
TestUtils.validateResults(analysisResults, 1);
});

test('Super13', () => {
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['super13.py']);

TestUtils.validateResults(analysisResults, 0);
});

test('MissingSuper1', () => {
const configOptions = new ConfigOptions(Uri.empty());

Expand Down

0 comments on commit 6d79c5c

Please sign in to comment.