Improve error messages for datetime vs date operations in Python algorithms - #9659
Open
jhonabreul wants to merge 2 commits into
Open
Improve error messages for datetime vs date operations in Python algorithms#9659jhonabreul wants to merge 2 commits into
jhonabreul wants to merge 2 commits into
Conversation
Martin-Molinero
approved these changes
Aug 10, 2026
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
Python algorithms mixing
datetime.datetimeanddatetime.datevalues (typically DTE math like(contract.id.date - self.time.date()).days) fail with TypeErrors that don't tell the user how to fix their code:Cause: the summation/subtraction message never says how to make the types match, and the comparison TypeError is not intercepted by any exception interpreter at all (
UnsupportedOperandPythonExceptionInterpreteronly matches"unsupported operand type").The fix:
UnsupportedOperandPythonExceptionInterpreter.Interpretappends a hint when the offending operands are adatetime.datetime/datetime.datemix (either order): use the date part of the datetime value, e.g.(expiry.date() - today).daysorself.time.date().DatetimeDateComparisonPythonExceptionInterpreterintercepts thecan't compare datetime.datetime to datetime.dateTypeError (raised for both comparison orders) and rewrites it with the same.date()guidance plus the parsed Python stack trace. It is auto-discovered byStackExceptionInterpreter.CreateFromAssemblies.Messages.Exceptions.cs, following the existing per-interpreter message-class pattern.This is a user-facing-message backstop: the companion QuantConnect/pythonnet#143 makes .NET
DateTimevalues converted to Python coerce against puredateoperands, which removes most of these failures at the root.This PR also updates the
QuantConnect.pythonnetpackage reference to 2.0.65 (the version carrying that coercion change) in the 11 consuming csproj files. Do not merge until QuantConnect/pythonnet#143 is merged and 2.0.65 is indexed on NuGet — restore fails before then.Related Issue
N/A — recurring user-facing runtime errors reported from production backtests.
Motivation and Context
DTE filtering (
(expiry - today).days) is one of the most common lines in options/futures Python algorithms, and today's errors leave users without an actionable fix.Requires Documentation Change
No — error message improvements only.
How Has This Been Tested?
UnsupportedOperandPythonExceptionInterpreterTests: new tests assert the.date()hint is appended fordatetime - dateanddate - datetime(realPythonExceptions raised fromTest_PythonExceptionInterpreter.py), and not appended for unrelated operand pairs (None + str). Proven red before the fix (3 failed / 12 passed), green after.DatetimeDateComparisonPythonExceptionInterpreterTests:CanInterpretmatrix (claims only the datetime/date comparisonPythonException; rejects other exception types and the unsupported-operand TypeError, and vice versa),Interpretthrow matrix, message content + Python stack line assertions, and an end-to-endStackExceptionInterpreter.CreateFromAssembliestest proving the new interpreter is discovered and applied.QuantConnect.Tests.Common.Exceptionsnamespace: 147 passed, 0 failed.PythonUtilTests: 18 passed, 0 failed.Python.Runtime.dll(built from Support subtraction and ordering between converted DateTime values and datetime.date pythonnet#143) injected over this branch's test output, mirroring pythonnet's Lean CI workflows: Python unit tests 16045 passed / 0 failed / 9 skipped; Python regression algorithms 324/324 passed.Types of changes
Checklist:
bug-<issue#>-<description>orfeature-<issue#>-<description>