Native DateOnly to Python datetime.date#801
Open
buvinghausen wants to merge 4 commits intotonybaloney:mainfrom
Open
Native DateOnly to Python datetime.date#801buvinghausen wants to merge 4 commits intotonybaloney:mainfrom
buvinghausen wants to merge 4 commits intotonybaloney:mainfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds bidirectional conversion between C#'s DateOnly and Python's datetime.date, using ordinal-based conversion (avoiding string parsing or intermediate DateTime allocations). The implementation follows the established patterns for type conversion throughout the CSnakes codebase.
Changes:
- Adds
DateOnly↔datetime.dateconversion via ordinal representation (PyObjectTypeConverter.Date.cs), withPyObject.From(DateOnly)factory overloads andAs<DateOnly>()dispatch in the runtime - Adds source generation support:
DateTypetype spec, parser mapping for"date"/"datetime.date", C# type reflection toDateOnly, and result conversion code generation - Adds comprehensive tests: unit tests for round-trip conversion, source generation snapshot tests, type reflection tests, and integration tests
Reviewed changes
Copilot reviewed 15 out of 15 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
src/CSnakes.Runtime/PyObjectTypeConverter.Date.cs |
New partial class with ConvertFromDateOnly and ConvertToDateOnly using ordinal-based conversion |
src/CSnakes.Runtime/Python/PyObject.cs |
Added From(DateOnly), From(DateOnly?) overloads and As<DateOnly>() dispatch |
src/CSnakes.Runtime/Python/PyObjectImporters.cs |
Added Date importer class implementing IPyObjectImporter<DateOnly> |
src/CSnakes.SourceGeneration/Parser/Types/PythonTypeSpec.cs |
Added DateType singleton record |
src/CSnakes.SourceGeneration/Parser/PythonParser.TypeDef.cs |
Maps "date" and "datetime.date" to DateType |
src/CSnakes.SourceGeneration/Reflection/TypeReflection.cs |
Maps DateType to DateOnly |
src/CSnakes.SourceGeneration/ResultConversionCodeGenerator.cs |
Wires DateType to the scalar Date code generator |
src/CSnakes.Runtime/PublicAPI/net8.0/PublicAPI.Unshipped.txt |
Records new public API surface (net8.0) |
src/CSnakes.Runtime/PublicAPI/net9.0/PublicAPI.Unshipped.txt |
Records new public API surface (net9.0) |
src/CSnakes.Runtime.Tests/Converter/DateConverterTest.cs |
Round-trip unit tests for DateOnly conversion |
src/CSnakes.Tests/TypeReflectionTests.cs |
Adds date/datetime.date → DateOnly test cases |
src/CSnakes.Tests/PythonTypeSpecTests.cs |
Adds DateTypeTests and singleton assertion |
src/CSnakes.Tests/PythonStaticGeneratorTests/FormatClassFromMethods.test_date.approved.txt |
Snapshot approval for generated code |
src/Integration.Tests/python/test_date.py |
Python test module for date round-trip and creation |
src/Integration.Tests/DateTests.cs |
Integration tests for date conversion |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Make optional work Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
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.
Add
DateOnly↔datetime.dateinteropAdds bidirectional conversion between C#'s
DateOnlyand Python'sdatetime.date.Conversion approach
The conversion uses the ordinal day representation to avoid any string parsing, locale issues, or intermediate
DateTimeallocations — making it the cleanest and fastest round-trip.date.DayNumber + 1is passed to Python'sdate.fromordinal()date.toordinal()result minus 1 is passed toDateOnly.FromDayNumber()The offset of 1 exists because C#'s
DayNumberis zero-based (day 0 = January 1, 0001) while Python's ordinal is one-based (ordinal 1 = January 1, 0001).Changes
Runtime (
CSnakes.Runtime)PyObjectTypeConverter.Date.cs— new partial class implementingConvertFromDateOnlyandConvertToDateOnlyvia the ordinal approachPyObject.cs— addedPyObject.From(DateOnly)andPyObject.From(DateOnly?)factory overloads;As<DateOnly>()dispatch added to the type-switchPyObjectImporters.cs— added[Experimental("PRTEXP001")] public sealed class Date : IPyObjectImporter<DateOnly>PublicAPI.Unshipped.txt(net8.0 & net9.0) — new public surface area recordedSource generation (
CSnakes.SourceGeneration)PythonTypeSpec.cs— addedDateTypesingleton record (base("date"))PythonParser.TypeDef.cs— parser maps"date"and"datetime.date"toDateTypeTypeReflection.cs—DateTypemaps to the C# type nameDateOnlyResultConversionCodeGenerator.cs—DateTypecase wired to theDatescalar generatorTests
CSnakes.Runtime.Tests/Converter/DateConverterTest.cs— round-trip unit tests via bothobj.As<DateOnly>()andobj.ImportAs<DateOnly, PyObjectImporters.Date>()coveringMinValue,MaxValue, Unix epoch, and a known dateCSnakes.Tests/TypeReflectionTests.cs— added("date", "DateOnly")and("datetime.date", "DateOnly")to the type reflection theory dataCSnakes.Tests/PythonTypeSpecTests.cs— addedDateTypeTestsandUuidTypeTestsinner test classes; both singletons added toSingletons_AreExpectedTypesCSnakes.Tests/PythonStaticGeneratorTests/FormatClassFromMethods.test_date.approved.txt— snapshot approval confirmingDateOnlyandglobal::CSnakes.Runtime.Python.PyObjectImporters.Dateappear correctly in generated codeIntegration.Tests/python/test_date.py— Python test module withtest_date_roundtripandtest_create_dateIntegration.Tests/DateTests.cs— integration tests: roundtrip for a known date,DateOnly.MinValue, and construction from year/month/day componentsExample