Handle null keys gracefully in dictionary lookup methods - #9679
Open
jhonabreul wants to merge 3 commits into
Open
Handle null keys gracefully in dictionary lookup methods#9679jhonabreul wants to merge 3 commits into
jhonabreul wants to merge 3 commits into
Conversation
ContainsKey and TryGetValue overrides across the ExtendedDictionary family (SecurityManager, CashBook, Slice, BaseExtendedDictionary, OptionChains, SecurityPositionGroupModel) passed the key straight into the backing .NET dictionary, which throws ArgumentNullException for a null key. From Python, a defensive guard like self.securities.contains_key(symbol) would itself throw when the symbol was None instead of returning False like a Python dictionary would.
jhonabreul
marked this pull request as ready for review
August 12, 2026 13: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
A Python algorithm guarding a securities lookup with
contains_keycrashes when the key isNone:This is easy to hit when a symbol field is initialized to
Noneand only assigned later (e.g. when the first option chain arrives), while a scheduled event fires before that assignment.Cause: the
ContainsKey/TryGetValueoverrides in theExtendedDictionary<TKey, TValue>family pass the key straight into the backing .NET dictionary, which throwsArgumentNullExceptionfor a null key — while theExtendedDictionarybase and itsget/pophelpers already treat a null/Nonekey as a missing key.The fix — a null key now behaves like a missing key (
false/default) in the lookup methods:SecurityManager.ContainsKey/TryGetValuereturnfalsefor a null symbol (this also coversSecurityPortfolioManager, which delegates toSecurities).BaseExtendedDictionary.ContainsKey/TryGetValuereturnfalsefor a null key, coveringDataDictionarycollections (TradeBars, chains, etc.),UniverseManagerandReadOnlyExtendedDictionary.Slice.ContainsKey/TryGetValuereturnfalsefor a null symbol.CashBook.ContainsKey/TryGetValuereturnfalsefor a null symbol.OptionChains.GetCanonicalOptionSymbolpasses a null symbol through instead of throwingNullReferenceException, so itsContainsKey/TryGetValuefall back to the now-guarded base implementations.SecurityPositionGroupModel.TryGetValuereturnsfalsefor a null key.Related Issue
N/A — user-reported runtime error, reproduction included in the description.
Motivation and Context
self.securities.contains_key(symbol)is the natural Python idiom for defensively guarding a lookup; in a Python dictionaryNone in d/d.get(None)returnsFalse/None, so the guard itself throwing is surprising and defeats its purpose.Requires Documentation Change
No.
How Has This Been Tested?
ExtendedDictionaryTests.DictionariesHandleNullKeysGracefully(new): parameterized with one test case per dictionary type —SecurityManager,SecurityPortfolioManager,CashBook,Slice,DataDictionary,TradeBars,OptionChains,FuturesChains,UniverseManagerandSecurityPositionGroupModel— assertingContainsKey(null)isfalse,TryGetValue(null)isfalseandget(null)is null.PythonDictionaryFeatureRegressionAlgorithm(extended): exercises the exact reported pattern from Python —contains_key(None)/get(None)onSecurities,Portfolio, theSliceandslice.bars— and fails if any of them throws or returns a non-missing result. Expected statistics unchanged.contains_key(None)onSecurities) before the fix; after the fix the same algorithm completes and the guard returnsNone.ExtendedDictionaryTests,SliceTests,CashBookTests,SecurityManagerTests,SecurityPortfolioManagerTests,OptionChainsTests,DataDictionaryTests,UniverseManagerTests, position group tests): 3,381 passed, 0 failed.Types of changes
Checklist:
bug-<issue#>-<description>orfeature-<issue#>-<description>