chore: modernize typing for Python 3.10+ minimum#356
Merged
Abhijeet Prasad (AbhiPrasad) merged 10 commits intomainfrom Apr 27, 2026
Merged
chore: modernize typing for Python 3.10+ minimum#356Abhijeet Prasad (AbhiPrasad) merged 10 commits intomainfrom
Abhijeet Prasad (AbhiPrasad) merged 10 commits intomainfrom
Conversation
The SDK requires Python >=3.10, so the `sys.version_info < (3, 9)` guard on `test_to_thread_preserves_context` is always False and can be removed. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Now that min Python is 3.10, use `int | str` union syntax directly in the type annotation instead of leaving it untyped with a comment. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…r.submit Future[T] generic typing has been stable since Python 3.9; with min Python 3.10 we no longer need to return Any as a workaround. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Both have been in stdlib typing since Python 3.8. With min Python 3.10, no need for the typing_extensions backport. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
With min Python 3.10, the union pipe syntax is available at runtime. Migrates type aliases and annotations in framework.py, logger.py, util.py, prompt.py, functions/stream.py, and langchain/callbacks.py. Keeps Union imports where used as a runtime sentinel for get_origin() checks (serializable_data_class.py, devserver/schemas.py). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Modernizes type annotations across merge_row_batch.py, trace.py, span_cache.py, otel/context.py, langsmith_wrapper.py, and test_serializable_data_class.py. Removes unused Optional imports. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Migrates legacy typing container imports (List, Dict, AsyncGenerator, Generator, Callable, Mapping, Sequence, Iterable, Iterator, Awaitable) to their modern equivalents: lowercase builtins (list, dict) and collections.abc for abstract types. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The regex-based Optional→|None replacement incorrectly placed | None inside Callable's return type instead of outside the whole Callable, changing the type semantics: - `Optional[Callable[[], Awaitable[X]]]` (callable is nullable) was wrongly converted to `Callable[[], Awaitable[X] | None]` (return type is nullable) Fixed to: `Callable[[], Awaitable[X]] | None` Also moves Callable from typing to collections.abc in langsmith_wrapper.py for consistency with other files. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
On Python 3.10, stdlib typing.TypedDict does not support inheriting from both Generic and TypedDict simultaneously. The typing_extensions backport handles this correctly, so TypedDict must stay in typing_extensions for types/_eval.py. Also applies ruff format/check fixes (import sorting, line collapsing). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Abhijeet Prasad (AbhiPrasad)
approved these changes
Apr 27, 2026
On Python 3.10-3.12, the `X | Y` syntax creates a `types.UnionType` which is distinct from `typing.Union`. The `from_dict_deep` method in `SerializableDataClass` only checked for `typing.Union` via `get_origin`, so it failed to deserialize union-typed fields when the annotation used pipe syntax (e.g. `PromptBlockData`). Also fixes a missed `cast(List, ...)` → `cast(list, ...)` in langchain test_callbacks.py that caused a pylint E0602. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.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.
Summary
requires-python = ">=3.10.0"Union[X, Y]withX | Ysyntax (PEP 604)Optional[X]withX | Nonetyping.List,typing.Dict, etc. with builtinlist,dictandcollections.abcequivalentsProtocolandTypedDictfromtyping_extensionsto stdlibtyping(where compatible)prettify_xact()andTracedThreadPoolExecutor.submit()sys.version_info < (3, 9)skipif guardserializable_data_class.from_dict_deepto handletypes.UnionType(the runtime type produced byX | Yon Python 3.10–3.12, which differs fromtyping.Union)Intentionally kept:
Unionimport inserializable_data_class.pyanddevserver/schemas.py— used as a runtime sentinel forget_origin()checksTypedDictfromtyping_extensionsintypes/_eval.py— stdlibtyping.TypedDicton Python 3.10 does not supportGeneric + TypedDictmultiple inheritanceNotRequiredfromtyping_extensions— only available in stdlibtypingsince 3.11_generated_types.py— auto-generated, not hand-editedOptional[str]in docstrings — documentation text, not type annotationsTest plan
make test-corepasses (442 passed)nox -s test_typespasses (pyright + mypy + pytest)make fixuppasses (ruff format, ruff check, codespell, stale cassettes)🤖 Generated with Claude Code