Skip to content

Commit

Permalink
fix second generic for AbstractContextManager being ignored when de…
Browse files Browse the repository at this point in the history
…termining whether the context manager can suppress exceptions
  • Loading branch information
DetachHead committed Dec 21, 2024
1 parent 9b874de commit 10d8466
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 19 deletions.
39 changes: 20 additions & 19 deletions packages/pyright-internal/src/analyzer/codeFlowEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1946,27 +1946,28 @@ export function getCodeFlowEngine(
const exitMethodName = isAsync ? '__aexit__' : '__exit__';
const exitType = evaluator.getBoundMagicMethod(cmType, exitMethodName);

if (exitType && isFunction(exitType) && exitType.shared.declaredReturnType) {
let returnType = exitType.shared.declaredReturnType;

// If it's an __aexit__ method, its return type will typically be wrapped
// in a Coroutine, so we need to extract the return type from the third
// type argument.
if (isAsync) {
if (
isClassInstance(returnType) &&
ClassType.isBuiltIn(returnType, 'Coroutine') &&
returnType.priv.typeArgs &&
returnType.priv.typeArgs.length >= 3
) {
returnType = returnType.priv.typeArgs[2];
if (exitType && isFunction(exitType)) {
let returnType = FunctionType.getEffectiveReturnType(exitType);
if (returnType) {
// If it's an __aexit__ method, its return type will typically be wrapped
// in a Coroutine, so we need to extract the return type from the third
// type argument.
if (isAsync) {
if (
isClassInstance(returnType) &&
ClassType.isBuiltIn(returnType, 'Coroutine') &&
returnType.priv.typeArgs &&
returnType.priv.typeArgs.length >= 3
) {
returnType = returnType.priv.typeArgs[2];
}
}
}

cmSwallowsExceptions = false;
if (isClassInstance(returnType) && ClassType.isBuiltIn(returnType, 'bool')) {
if (returnType.priv.literalValue === undefined || returnType.priv.literalValue === true) {
cmSwallowsExceptions = true;
cmSwallowsExceptions = false;
if (isClassInstance(returnType) && ClassType.isBuiltIn(returnType, 'bool')) {
if (returnType.priv.literalValue === undefined || returnType.priv.literalValue === true) {
cmSwallowsExceptions = true;
}
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions packages/pyright-internal/src/tests/checker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,14 @@ test('With6', () => {
TestUtils.validateResults(analysisResults, 0);
});

test('`AbstractContextManager` with exit type specified with a generic', () => {
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['withGenericExitType.py']);

TestUtils.validateResultsButBased(analysisResults, {
hints: [{ code: DiagnosticRule.reportUnreachable, line: 11 }],
});
});

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

Expand Down
12 changes: 12 additions & 0 deletions packages/pyright-internal/src/tests/samples/withGenericExitType.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import contextlib
from typing import Literal

def _(cm: contextlib.AbstractContextManager[object, Literal[True]]):
with cm:
raise Exception
print(1) # reachable

def _(cm: contextlib.AbstractContextManager[object, Literal[False]]):
with cm:
raise Exception
print(1) # error: unreachable

0 comments on commit 10d8466

Please sign in to comment.