fix(python): use datetime.date/time for date/time formats#2990
Merged
Conversation
The Python generator collapsed JSON Schema string formats "date" and "time" onto the same "date-time" transformed-string kind, so all three were emitted as datetime.datetime and serialized with .isoformat(), producing a full ISO datetime string for date-only and time-only values. This violated the input schema on round-trip. Keep "date" and "time" as distinct transformed-string kinds through the Python stringTypeMapping, and teach PythonRenderer/ JSONPythonRenderer to emit datetime.date/datetime.time (with dedicated from_date/from_time parsing helpers) instead of always using datetime.datetime. Test coverage: strengthened test/fixtures/python/main.py to assert that generated instances have the correct runtime types (datetime.date/datetime.time/datetime.datetime) for schemas with date/time/date-time properties; this is exercised by the existing test/inputs/schema/date-time.schema fixture, run via `QUICKTEST=true FIXTURE=schema-python script/test`. Co-Authored-By: gpt-5.6-sol via pi <noreply@openai.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.
Bug
The Python generator mapped JSON Schema string formats
dateandtimeonto the same transformed-string kind asdate-time. As a result, a schema property withformat: "date"orformat: "time"was typed asdatetime.datetimeand serialized with.isoformat(), so a date-only value like"2024-01-01"round-tripped as a full ISO datetime string (e.g."2024-01-01T00:00:00"), which violates the input schema.Repro:
{ "$schema": "http://json-schema.org/draft-07/schema#", "$ref": "#/definitions/Example", "definitions": { "Example": { "type": "object", "additionalProperties": false, "properties": { "d": { "type": "string", "format": "date" }, "t": { "type": "string", "format": "time" }, "dt": { "type": "string", "format": "date-time" } }, "required": ["d","t","dt"] } } }Before the fix,
d,t, anddtwere all typeddatetime. After the fix,disdatetime.date,tisdatetime.time, anddtremainsdatetime.datetime.Root cause
packages/quicktype-core/src/language/Python/language.tsfolded thedateandtimeTransformedStringTypeKinds intodate-timeinstringTypeMapping, so every downstream Python renderer switch only ever saw a singledate-timekind.Fix
language.ts: keepdate,time, anddate-timeas distinct kinds instringTypeMappinginstead of collapsing them.PythonRenderer.ts: emitdatetime.date/datetime.time/datetime.datetimetype names (via a new module-levelimport datetimehelper) instead of always usingdatetime.datetime.JSONPythonRenderer.ts: addfrom_date/from_timeparsing helpers (usingdateutil.parser.parse(...).date()/.time(), with a format-shape assertion) alongside the existingfrom_datetime, and route thedate/time/date-timetransformer cases to the correct converter for both parsing and.isoformat()serialization.Test coverage
test/fixtures/python/main.py(the Python fixture driver used by end-to-end schema fixture tests) was strengthened to assert the correct runtime types (datetime.date,datetime.time,datetime.datetime) whenever the round-tripped object hasdate/time/date-timeproperties. This is exercised by the existingtest/inputs/schema/date-time.schemafixture (which already hasdate,time, anddate-timeproperties plus union-array cases), run via the standardschema-pythonfixture suite — no fixture wiring changes were needed since the schema/inputs already existed, they just weren't checking runtime types before.Verification (local)
npm run buildpasses.npm run test:unit: 163/163 tests pass.QUICKTEST=true FIXTURE=schema-python script/test: all 67 tests pass, includingdate-time.schemawith the strengthened runtime-type assertions.datetime.date/datetime.time/datetime.datetimeas expected, and a manual round-trip in Python confirmsto_dict()now serializesdas"2024-01-01"andtas a time-only ISO string instead of a full datetime string.Fixes #1974
🤖 Generated with Claude Code