Restore Python's builtin Exception shadowed by AlgorithmImports star imports - #9691
Open
jhonabreul wants to merge 1 commit into
Open
Conversation
from System import * shadows the builtin Exception with System.Exception, so except Exception clauses in Python algorithms silently miss Python exceptions (TypeError, KeyError, ...). Rebind the builtin after the star imports and keep the CLR type reachable as System.Exception.
jhonabreul
marked this pull request as ready for review
August 12, 2026 16:25
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.
Description
AlgorithmImports.pydoesfrom System import *, which shadows Python's builtinExceptionwithSystem.Exception. Pure-Python exceptions (TypeError,KeyError, ...) do not derive from it, soexcept Exception:silently fails to catch them:The fix restores the builtin after the star imports:
Exception = builtins.Exceptionat the end ofAlgorithmImports.py. The builtin also catches CLR exceptions raised into Python, so it is strictly broader than the previous binding.import Systemalongside the star import, so the CLR type stays reachable asSystem.Exception.Behavior changes:
raise Exception("msg")now creates a PythonExceptioninstead of aSystem.Exception(both propagate and log fine), andisinstance(x, Exception)is broader.Related Issue
N/A
Motivation and Context
Python algorithms commonly wrap calls in
try/except Exceptionand still crash with "unhandled"TypeError/KeyError. The code is idiomatic Python and correct anywhere except inside Lean.Requires Documentation Change
No.
How Has This Been Tested?
New
Tests/Python/AlgorithmImportsTestsfixture, written red-first against master:ExceptionIsThePythonBuiltinException—Exception is builtins.Exceptionafterfrom AlgorithmImports import *.ExceptClauseCatchesPythonExceptions—except Exception:catches a raisedTypeError.ExceptClauseCatchesClrExceptions—except Exception:still catches a CLR exception thrown from C# (no regression).StarImportsDoNotShadowPythonBuiltins— audits every exported name againstbuiltins; before the fix it reported exactlyException, now empty. Guards against future shadowing.Full
QuantConnect.Tests.PythonandQuantConnect.Tests.Common.Exceptionsfixtures (1458 tests): all green.Types of changes
Checklist:
bug-<issue#>-<description>orfeature-<issue#>-<description>