Fix except* type inference for BaseException subclasses - #11571
Open
hsusul wants to merge 1 commit into
Open
Conversation
Collaborator
|
🔒 Automated review in progress — @rchiodo is auto-reviewing this PR. |
rchiodo
approved these changes
Jul 28, 2026
rchiodo
left a comment
Collaborator
There was a problem hiding this comment.
Approved via Review Center.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
BaseExceptionGroup[T]forexcept*targets that derive fromBaseExceptionbut not fromException.ExceptionGroup[T]for ordinaryExceptionsubclasses.Problem
For an
except* KeyboardInterrupt as excclause, Pyright currently revealsExceptionGroup[KeyboardInterrupt]. At runtime,ExceptionGroupcan containonly
Exceptionsubclasses, so a group containingKeyboardInterruptis aBaseExceptionGroup.Root cause
The exception-group evaluator selected
BaseExceptionGrouponly when thetarget was exactly the built-in
BaseExceptionclass. It did not account forsubclasses such as
KeyboardInterrupt,SystemExit, or user-definedBaseExceptionsubclasses.Solution
Use the existing standard-library MRO helper to select
BaseExceptionGroupwhen any
except*target derives fromBaseExceptionwithout also derivingfrom
Exception. The existingExceptionGroupbehavior remains unchanged forExceptionsubclasses.Tests
PYLANCE_JEST_TRANSPILE_ONLY=1 node --max-old-space-size=8192 --expose-gc ./node_modules/jest/bin/jest src/tests/typeEvaluator7.test.ts --runInBand --forceExit -t exceptionGroup1— passed.PYLANCE_JEST_TRANSPILE_ONLY=1 node --max-old-space-size=8192 --expose-gc ./node_modules/jest/bin/jest src/tests/typeEvaluator7.test.ts --runInBand --forceExit— 165 tests passed.node --max-old-space-size=8192 --expose-gc ./node_modules/jest/bin/jest --forceExit --testPathIgnorePatterns src/tests/benchmarks --runInBand— 62 suites and 2,549 tests passed.Validation
npm run check— syncpack, ESLint, and Prettier passed.npx lerna exec --stream --no-bail -- "tsc --noEmit"— TypeScript passed in all four packages.npm run build:cli:dev— CLI bundle built successfully.npm run build:extension:dev— VS Code extension bundle built successfully.BaseExceptionGroup[KeyboardInterrupt]for the original reproduction and continues to revealExceptionGroup[ValueError]for the control case.npm testrun completed 61 of 62 suites and timed out in the unrelated language-server background-diagnostics test. All four variants of that test then passed in isolation, and the complete serial suite passed as reported above.Compatibility and risk
This is an analyzer-only correction for Python 3.11+
except*targetinference. It does not change the public API, diagnostics, parsing, or runtime
behavior. The predicate reuses existing MRO metadata and retains the previous
result for all
Exceptionsubclasses.Documentation
No documentation change is needed because this aligns inferred types with the
existing Python exception-group semantics.
Related issues and prior work
#9467 introduced the
ExceptionGroupversusBaseExceptionGroupdistinctionfor #9466. This change covers the remaining subclass boundary in that logic.
No new issue was created for this focused correction.