From 0337b1022da70680874907e12293f319c6c4961e Mon Sep 17 00:00:00 2001 From: mikaeloduh Date: Mon, 7 Jul 2025 12:34:13 +0800 Subject: [PATCH] fix: handle typing_extensions import properly on Python 3.11+ The SDK unconditionally imported `typing_extensions.NotRequired`, but `typing_extensions` is declared as a dependency only for Python < 3.11 in pyproject.toml. On Python 3.11+ this led to a ModuleNotFoundError on a clean install. This commit replaces the unconditional import with a version-guarded --- src/claude_code_sdk/types.py | 6 +++++- tests/test_types.py | 11 +++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/claude_code_sdk/types.py b/src/claude_code_sdk/types.py index bd3c726..8cb662a 100644 --- a/src/claude_code_sdk/types.py +++ b/src/claude_code_sdk/types.py @@ -1,10 +1,14 @@ """Type definitions for Claude SDK.""" +import sys from dataclasses import dataclass, field from pathlib import Path from typing import Any, Literal, TypedDict -from typing_extensions import NotRequired # For Python < 3.11 compatibility +if sys.version_info >= (3, 11): + from typing import NotRequired +else: + from typing_extensions import NotRequired # Permission modes PermissionMode = Literal["default", "acceptEdits", "bypassPermissions"] diff --git a/tests/test_types.py b/tests/test_types.py index 6046292..5d53375 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -1,5 +1,8 @@ """Tests for Claude SDK type definitions.""" +import sys +import importlib +import pytest from claude_code_sdk import ( AssistantMessage, ClaudeCodeOptions, @@ -56,6 +59,14 @@ class TestMessageTypes: assert msg.total_cost_usd == 0.01 assert msg.session_id == "session-123" + def test_import_types_module(self): + """ + Smoke test to ensure claude_code_sdk.types imports without errors + and defines NotRequired. + """ + module = importlib.reload(importlib.import_module("claude_code_sdk.types")) + assert hasattr(module, "NotRequired"), "claude_code_sdk.types should define NotRequired" + class TestOptions: """Test Options configuration."""