instructor icon indicating copy to clipboard operation
instructor copied to clipboard

`ValidationInfo.context` may be `None`, Pylance reports "get" is not a known attribute of "None"

Open johnwlockwood opened this issue 7 months ago • 2 comments

https://github.com/567-labs/instructor/blob/121f901653f92e6d718531cc2a02151d56ad5ca3/examples/reranker/run.py#L24

"get" is not a known attribute of "None" PylancereportOptionalMemberAccess

johnwlockwood avatar Jun 03 '25 21:06 johnwlockwood

@claude investigate and fix if possible,

jxnl avatar Jun 10 '25 21:06 jxnl

Claude finished @jxnl's task in 1m 53s —— View jobclaude/issue-1588-20250610_213255Create PR ➔


ValidationInfo.context type issue - Fixed ✅

I've successfully investigated and fixed the Pylance type checking issue where ValidationInfo.context may be None but the code calls .get() on it.

Tasks:

  • [x] Read the problematic file (examples/reranker/run.py:24)
  • [x] Understand ValidationInfo.context typing
  • [x] Implement type-safe fix
  • [x] Test and verify the solution
  • [x] Commit changes

Analysis:

Found the issue in examples/reranker/run.py:24. The code directly calls .get() on info.context without checking if it's None:

context = info.context
chunks = context.get("chunks", [])  # Error: context might be None

Fix Applied:

Added a null check before accessing context methods, following the pattern used in test files:

context = info.context
if context:
    chunks = context.get("chunks", [])
    if v not in [chunk["id"] for chunk in chunks]:
        # validation logic...

This ensures the validator only performs chunk ID validation when context is available, eliminating the Pylance type error while maintaining the same functionality.

Changes Committed:

  • File: examples/reranker/run.py
  • Change: Added if context: check before calling context.get()
  • Commit: 29b6c11

claude[bot] avatar Jun 10 '25 21:06 claude[bot]