Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix second generic for AbstractContextManager being ignored when determining whether the context manager can suppress exceptions #963

Merged
merged 1 commit into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading