diff --git a/.github/workflows/dxray.yml b/.github/workflows/dxray.yml index d356b47..782db03 100644 --- a/.github/workflows/dxray.yml +++ b/.github/workflows/dxray.yml @@ -35,9 +35,15 @@ jobs: runs-on: ${{ matrix.os }} strategy: fail-fast: false + # Every Python on Linux; Windows and macOS on the oldest and newest. Ten jobs, so macOS runners aren't queued for hours. matrix: - os: [ubuntu-22.04, windows-latest, macos-latest] + os: [ubuntu-22.04] python: ["3.9", "3.10", "3.11", "3.12", "3.13", "3.14"] + include: + - { os: windows-latest, python: "3.9" } + - { os: windows-latest, python: "3.14" } + - { os: macos-latest, python: "3.9" } + - { os: macos-latest, python: "3.14" } steps: - uses: actions/checkout@v4 - uses: actions/setup-python@v5 diff --git a/python/dxray/README.md b/python/dxray/README.md index 41eb901..2b1a949 100644 --- a/python/dxray/README.md +++ b/python/dxray/README.md @@ -33,7 +33,7 @@ pip install dxray ``` Works with pandas DataFrames (and polars or pyarrow tables, dicts, lists) and with CSV, Excel, Parquet and JSON files. -Python 3.9+. Only needs pandas and numpy. +Python 3.9+. Only needs pandas and numpy (`dx.ask()` needs Python 3.10+, like the Anthropic SDK). ## Why dxray diff --git a/python/dxray/pyproject.toml b/python/dxray/pyproject.toml index 6f7e45b..0827be3 100644 --- a/python/dxray/pyproject.toml +++ b/python/dxray/pyproject.toml @@ -34,8 +34,9 @@ classifiers = [ dependencies = ["pandas>=1.5", "numpy>=1.23"] [project.optional-dependencies] -ai = ["anthropic>=2.13"] -all = ["anthropic>=2.13", "pyarrow>=12", "openpyxl>=3.1"] +# The Anthropic SDK needs Python 3.10+: on 3.9 everything but dx.ask() still installs and works. +ai = ["anthropic>=2.13; python_version >= '3.10'"] +all = ["anthropic>=2.13; python_version >= '3.10'", "pyarrow>=12", "openpyxl>=3.1"] [project.urls] Homepage = "https://github.com/fcopensource/veyraeditor/tree/main/python/dxray" diff --git a/python/dxray/src/dxray/_ask.py b/python/dxray/src/dxray/_ask.py index 54a000d..f031755 100644 --- a/python/dxray/src/dxray/_ask.py +++ b/python/dxray/src/dxray/_ask.py @@ -55,6 +55,9 @@ def ask(data: Any, question: str, *, target: str | None = None, model: str = DEF try: import anthropic except ImportError: + import sys + if sys.version_info < (3, 10): + raise AskError("dx.ask() needs Python 3.10 or newer (the Anthropic SDK does). Everything else in dxray works on your Python.") from None raise AskError('dx.ask() needs the Anthropic SDK. Install it with: pip install "dxray[ai]"') from None df, name = _frame(data) context = for_llm(df, target=target, report=scan(df, target, name=name)) diff --git a/python/dxray/tests/test_clean_llm_ask.py b/python/dxray/tests/test_clean_llm_ask.py index 4f3b467..fe74d14 100644 --- a/python/dxray/tests/test_clean_llm_ask.py +++ b/python/dxray/tests/test_clean_llm_ask.py @@ -1,5 +1,6 @@ """dx.clean(), dx.for_llm(), dx.ask() (with a fake Claude client) and the dxray command.""" +import importlib.util import json import subprocess import sys @@ -11,6 +12,9 @@ import dxray as dx from dxray.cli import main +# The Anthropic SDK needs Python 3.10+, so on 3.9 these are skipped (and test_ask_without_the_sdk covers that case). +needs_sdk = pytest.mark.skipif(importlib.util.find_spec("anthropic") is None, reason="the Anthropic SDK isn't installed") + # ------------------------------------------------------------------ clean @@ -83,6 +87,7 @@ def __init__(self, text="Revenue grew 122% over the year.", stop="end_turn"): self.beta = SimpleNamespace(messages=beta_messages) +@needs_sdk def test_ask_sends_the_description_and_returns_the_answer(orders): client = FakeClient() answer = dx.ask(orders, "How did revenue change?", target="revenue", client=client) @@ -96,6 +101,7 @@ def test_ask_sends_the_description_and_returns_the_answer(orders): assert call["output_config"] == {"effort": "medium"} +@needs_sdk def test_ask_refusal_and_bad_question(orders): with pytest.raises(dx.AskError, match="declined"): dx.ask(orders, "something", client=FakeClient(stop="refusal")) @@ -103,6 +109,7 @@ def test_ask_refusal_and_bad_question(orders): dx.ask(orders, " ", client=FakeClient()) +@needs_sdk def test_ask_without_a_key_explains(orders, monkeypatch): for v in ("ANTHROPIC_API_KEY", "ANTHROPIC_AUTH_TOKEN", "ANTHROPIC_PROFILE"): monkeypatch.delenv(v, raising=False) @@ -134,3 +141,18 @@ def test_module_entry_point(orders, tmp_path): orders.head(200).to_csv(p, index=False) r = subprocess.run([sys.executable, "-m", "dxray", str(p), "--markdown"], capture_output=True, text=True, encoding="utf-8") assert r.returncode == 0 and r.stdout.startswith("# o.csv") + + +def test_ask_without_the_sdk(monkeypatch): + """No Anthropic SDK (not installed, or Python 3.9): a clear message, not a crash.""" + monkeypatch.setitem(sys.modules, "anthropic", None) # makes `import anthropic` fail + with pytest.raises(dx.AskError) as caught: + dx.ask(pd.DataFrame({"a": [1, 2, 3]}), "anything?") + message = str(caught.value) + if sys.version_info < (3, 10): + assert "Python 3.10" in message + else: + assert 'pip install "dxray[ai]"' in message + monkeypatch.setattr(sys, "version_info", (3, 9, 18)) + with pytest.raises(dx.AskError, match="Python 3.10"): + dx.ask(pd.DataFrame({"a": [1, 2, 3]}), "anything?")