Skip to content

fix(python): drop typing.Type dependency for Python 3.6 target#3005

Merged
schani merged 1 commit into
masterfrom
agent/fix-issue-1728
Jul 21, 2026
Merged

fix(python): drop typing.Type dependency for Python 3.6 target#3005
schani merged 1 commit into
masterfrom
agent/fix-issue-1728

Conversation

@schani

@schani schani commented Jul 20, 2026

Copy link
Copy Markdown
Member

Bug

--lang python --python-version 3.6 generated code that imports and uses
typing.Type:

from typing import Any, TypeVar, Type, cast
...
def to_class(c: Type[T], x: Any) -> dict:

typing.Type was 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.6 preset in pythonOptions.features (packages/quicktype-core/src/language/Python/language.ts)
sets typeHints: true. With type hints enabled, JSONPythonRenderer
unconditionally emitted Type[T] (imported from typing) as the parameter
type hint for the c/t parameter of the generated to_class, to_enum,
and is_type helper functions — regardless of Python version.

Fix

Added a new typingType flag to PythonFeatures, set to false for the
3.5/3.6 presets and true for 3.7+ (where typing.Type has long been
available). JSONPythonRenderer now only emits the Type[T] annotation when
typingType is enabled; when it's disabled, the c/t parameter is left
without 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 both
Python 3.6 and 3.7 from a schema/JSON pair exercising to_class, to_enum,
and is_type, and asserts:

  • Python 3.6 output never imports or references typing.Type.
  • Python 3.7 output still imports and uses Type[T] as before (no
    regression for versions where typing.Type is safe to use).

The existing end-to-end Python fixture suite (test/languages.ts) already
parametrizes on python-version (3.5/3.6/3.7/3.9) via
quickTestRendererOptions, compiling generated code with mypy and
executing 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.
  • Repro re-run: node dist/index.js --lang python --python-version 3.6 sample.json
    no longer imports/uses Type; --python-version 3.7 output 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, including
    the python-version: 3.5/3.6/3.7/3.9 combinations.
  • biome check on changed files — clean.
  • CI will run the full fixture matrix and unit suite.

Fixes #1728

🤖 Generated with Claude Code

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>
@schani
schani merged commit 524b3fe into master Jul 21, 2026
28 checks passed
@schani
schani deleted the agent/fix-issue-1728 branch July 21, 2026 13:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python3.6 generated code has "typing" import which may not work on all 3.6 microversions

1 participant