fix(python): drop typing.Type dependency for Python 3.6 target#3005
Merged
Conversation
typing.Type was only added to the standard library in 3.5.3/3.6.1, so it does not exist on Python 3.6.0. The --python-version 3.6 preset enables type hints and unconditionally emitted `Type[T]` (imported from typing) as the parameter type hint for the c parameter of the generated to_class/to_enum/is_type helpers, breaking generated code on 3.6.0. Add a typingType PythonFeatures flag (false for 3.5/3.6, true for 3.7+) and only emit the Type[T] annotation when it is set, leaving all other type hints for 3.6 intact and 3.7+ output unchanged. 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
--lang python --python-version 3.6generated code that imports and usestyping.Type:typing.Typewas only added to the standard library in Python 3.5.3 / 3.6.1— it does not exist in Python 3.6.0, the very first 3.6 release. This makes
quicktype's "3.6" output incompatible with that micro-version, even though
the user asked for 3.6 compatibility.
Root cause
The
3.6preset inpythonOptions.features(packages/quicktype-core/src/language/Python/language.ts)sets
typeHints: true. With type hints enabled,JSONPythonRendererunconditionally emitted
Type[T](imported fromtyping) as the parametertype hint for the
c/tparameter of the generatedto_class,to_enum,and
is_typehelper functions — regardless of Python version.Fix
Added a new
typingTypeflag toPythonFeatures, set tofalsefor the3.5/3.6presets andtruefor3.7+ (wheretyping.Typehas long beenavailable).
JSONPythonRenderernow only emits theType[T]annotation whentypingTypeis enabled; when it's disabled, thec/tparameter is leftwithout a type hint. All other type hints for Python 3.6 are unaffected, and
3.7+ output is unchanged.
Test coverage
Added
test/unit/python-typing-type.test.ts, which generates code for bothPython 3.6 and 3.7 from a schema/JSON pair exercising
to_class,to_enum,and
is_type, and asserts:typing.Type.Type[T]as before (noregression for versions where
typing.Typeis safe to use).The existing end-to-end Python fixture suite (
test/languages.ts) alreadyparametrizes on
python-version(3.5/3.6/3.7/3.9) viaquickTestRendererOptions, compiling generated code withmypyandexecuting it — this continues to pass with the changed 3.6 output, verifying
the generated code stays syntactically valid and mypy-clean.
Verification performed locally
npm run build— passes.node dist/index.js --lang python --python-version 3.6 sample.jsonno longer imports/uses
Type;--python-version 3.7output is unchanged.npx vitest run test/unit/python-typing-type.test.ts— passes (2/2).QUICKTEST=true FIXTURE=python script/test— 61/61 tests pass, includingthe
python-version: 3.5/3.6/3.7/3.9combinations.biome checkon changed files — clean.Fixes #1728
🤖 Generated with Claude Code