Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion .github/workflows/dxray.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion python/dxray/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 3 additions & 2 deletions python/dxray/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 3 additions & 0 deletions python/dxray/src/dxray/_ask.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
22 changes: 22 additions & 0 deletions python/dxray/tests/test_clean_llm_ask.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -96,13 +101,15 @@ 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"))
with pytest.raises(ValueError):
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)
Expand Down Expand Up @@ -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?")
Loading