Support subtraction and ordering between converted DateTime values and datetime.date - #143
Open
jhonabreul wants to merge 2 commits into
Open
Support subtraction and ordering between converted DateTime values and datetime.date#143jhonabreul wants to merge 2 commits into
jhonabreul wants to merge 2 commits into
Conversation
8 tasks
Martin-Molinero
approved these changes
Aug 10, 2026
11 tasks
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.
What does this implement/fix? Explain your changes.
Python algorithms mixing converted .NET
DateTimevalues (self.time,contract.id.date, option/future expiries) with pure Pythondatetime.dateobjects fail at runtime:The typical shape is DTE math, e.g.
dte = (contract.id.date - self.time.date()).days, the single most repeated runtime arithmetic failure across user deployments.Cause:
Converter.ToPythonemits stockdatetime.datetimeinstances, and CPython refuses subtraction and ordering betweendatetimeand puredateoperands.The fix:
Converternow emitsSystem.DateTimevalues as instances of adatetime.datetimesubclass, defined once (lazily) in a syntheticclr_datetimemodule.__sub__/__rsub__/__lt__/__le__/__gt__/__ge__coerce against puredatetime.dateoperands using the datetime's.date()part; any other operand defers to the stock implementation. Sincedatetimesubclassesdate, reflected-operator precedence makes both operand orders work (converted - dateanddate - converted).datetime == datestaysFalse, since making it true would break the hash contract for dicts/sets.__repr__renders identically to stock (datetime.datetime(...)), and__reduce_ex__pickles instances as plaindatetime.datetime, so pickled payloads and deep copies never reference the synthetic module.tests/test_conversion.pytest_datetime_conversionrelaxed its exact-type assertion (type(...) is datetime) toisinstance— deliberate, the emitted type is now a subclass.Does this close any currently open issues?
No open pythonnet issue; addresses the
datetime.datetimevsdatetime.datefailure class quantified in QuantConnect/Agents#305 (14/14 affected deployments in the sampled fleet).Any other comments?
Includes the version bump to 2.0.65 as its own commit (
<Version>,AssemblyVersion/AssemblyFileVersion, and the perf-test baseline package references). Merging publishes 2.0.65 to NuGet; downstream bumps are staged in Improve error messages for datetime vs date operations in Python algorithms Lean#9659 and QuantConnect/LeanCloud.Services#253 (both blocked on indexing).Perf (existing
ConvertDateTimeRoundTripPerformance, 500kToPython+ToManagedround trips, Release):Unspecifiedkind 521ms before → 515ms after;Utckind 1542ms → 1231ms. No regression.New embed tests (
TestConverter.ConvertedDateTime*, 3 of 5 red before the fix): subtraction with a date in both operand orders, ordering in both orders, same-day comparison semantics, plain-datetime behavior parity (equality, hash, arithmetic with datetime/timedelta,str/repr/strftime), and pickle degradation to plaindatetime.Python-side
tests/test_conversion.py::test_datetime_date_coercionexercises the same through a real CLR boundary (ConversionTest.DateTimeField).End-to-end against Lean (master + Improve error messages for datetime vs date operations in Python algorithms Lean#9659, this branch's
Python.Runtime.dllinjected over the test output, mirroring the Lean Python CI workflows): Python unit tests 16045 passed / 0 failed / 9 skipped; Python regression algorithms 324/324 passed.Full embed suite: 978 passed, 0 failed, 8 skipped. Full Python test suite: 457 passed, 1 pre-existing environment-related failure (
test_explicit_assembly_load, fails identically on master).Operations where both operands are pure Python objects (e.g.
datetime.now() - date.today()) still raise, as pythonnet is not involved; a companion Lean PR improves those error messages: Improve error messages for datetime vs date operations in Python algorithms Lean#9659. No merge-order dependency between the two.Checklist
Check all those that are applicable and complete.
AUTHORSCHANGELOG