Skip to content

fix: preserve SDK imports on supported Python versions#427

Open
DivyamTalwar wants to merge 1 commit into
morphik-org:mainfrom
DivyamTalwar:fix/sdk-python-min-version-imports
Open

fix: preserve SDK imports on supported Python versions#427
DivyamTalwar wants to merge 1 commit into
morphik-org:mainfrom
DivyamTalwar:fix/sdk-python-min-version-imports

Conversation

@DivyamTalwar

Copy link
Copy Markdown

Summary

The Python SDK declares requires-python = ">=3.8", but a few runtime-evaluated annotations used newer Python syntax. On Python 3.9, importing morphik failed immediately with TypeError: unsupported operand type(s) for |: 'type' and 'type' before users could create a client.

This replaces those annotations with typing equivalents (Union, Tuple, and List) and adds a short SDK changelog note. There is no API behavior change.

Changes

  • Replaced FinalChunkResult.content: str | PILImage with Union[str, PILImage].
  • Replaced sync/async migration helper return annotations from tuple[str, Document] to Tuple[str, Document].
  • Replaced live-test schema annotations from list[str] to List[str] so those modules collect on older supported interpreters.
  • Added an Unreleased Python SDK changelog entry for the compatibility fix.

Why this matters

Users on Python versions allowed by the SDK metadata should be able to import the package. This fixes a reproduced import-time compatibility regression for Python 3.9 and removes the same newer annotation forms from the touched SDK/test surface.

Tests

Commands run:

  • uv run --python 3.9 --with pytest --with httpx --with pyjwt --with 'pydantic>=2.10.3' --with 'pillow>=10.4.0' --project sdks/python python - <<'PY' ... import morphik ... PY — failed before the fix with TypeError: unsupported operand type(s) for |: 'type' and 'type'.
  • uv run --python 3.9 --project sdks/python python -c "import morphik; from morphik.sync import Morphik; from morphik.async_ import AsyncMorphik; print('py39 import ok')" — passed.
  • uv run --python 3.9 --project sdks/python pytest sdks/python/morphik/tests/test_scoped_ops_unit.py::test_sync_client_accepts_plain_http_uri_without_auth -q — passed.
  • SKIP_LIVE_TESTS=1 uv run --python 3.9 --project sdks/python pytest sdks/python/morphik/tests/test_sync.py sdks/python/morphik/tests/test_async.py --collect-only -q — passed, 26 tests collected.
  • .venv/bin/python -m pytest sdks/python/morphik/tests/test_scoped_ops_unit.py -q — passed, 49 tests.
  • SKIP_LIVE_TESTS=1 .venv/bin/python -m pytest sdks/python/morphik/tests -q — passed, 74 passed / 26 skipped.
  • .venv/bin/python -m py_compile sdks/python/morphik/_internal.py sdks/python/morphik/sync.py sdks/python/morphik/async_.py sdks/python/morphik/tests/test_sync.py sdks/python/morphik/tests/test_async.py — passed.
  • Python 3.8 grammar parse across sdks/python/morphik/**/*.py using ast.parse(..., feature_version=(3, 8)) — passed.
  • SDK annotation compatibility scan for PEP 604 unions and PEP 585 builtin generic annotations under sdks/python/morphik — passed.
  • .venv/bin/markdown-it sdks/python/CHANGELOG.md >/tmp/sdk-changelog-pr12.html — passed.
  • git diff --check — passed.

Checks not completed:

  • uv run --python 3.8 --project sdks/python ... — not completed because the local cpython-3.8.20-macos-aarch64 download remained silent for multiple minutes and was interrupted. The Python 3.9 runtime failure is reproduced/fixed, and source-level Python 3.8 grammar/annotation scans pass.
  • uv run ty check ... — failed on existing SDK typing diagnostics unrelated to this diff (_prepare_file_for_upload return type, CompletionResponse required usage, sync/async get_info return type, and migration status literal).
  • uv run ruff check ... — not run successfully because ruff is not installed in the local project environment.

Risk

Risk level: low.

This is an annotation-only compatibility fix plus a changelog entry. It does not change request payloads, parsing behavior, public APIs, dependencies, auth, or network behavior. Rollback is straightforward if maintainers choose to raise the SDK Python floor instead.

Issue

No existing issue. This was discovered during repository analysis.

This does not address or close issue #155, which concerns a hosted documentation redirect page that is not present in this repository.

Notes for maintainers

Follow-up worth considering: add a small SDK import/test-collection CI job for the declared minimum supported Python version, so future annotation syntax regressions are caught automatically.

Constraint: The Python SDK metadata declares >=3.8, while Python 3.9 import failed on runtime-evaluated newer annotation syntax.
Rejected: Raise the SDK Python floor to >=3.10 | This keeps the existing published support contract and only changes annotation syntax.
Confidence: high
Scope-risk: narrow
Directive: Add a minimum-version SDK import or collection CI job before relying on future syntax changes.
Tested: Python 3.9 import smoke; Python 3.9 SDK live-test collection with SKIP_LIVE_TESTS=1; SDK tests with live tests skipped (74 passed, 26 skipped); py_compile on changed Python files; Python 3.8 grammar parse and annotation compatibility scan; changelog markdown render; git diff --check.
Not-tested: Python 3.8 runtime import because uv interpreter download did not complete locally; ty has existing unrelated SDK diagnostics; ruff is unavailable locally.
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.

1 participant