Skip to content

Add item access support for non-identifier subject/surface/transform names - #709

Open
alexhuth wants to merge 1 commit into
mainfrom
claude/issue-699-ai75xq
Open

Add item access support for non-identifier subject/surface/transform names#709
alexhuth wants to merge 1 commit into
mainfrom
claude/issue-699-ai75xq

Conversation

@alexhuth

Copy link
Copy Markdown
Contributor

Summary

This PR adds __getitem__ support throughout the database access hierarchy, enabling users to access subjects, surfaces, and transforms by name using bracket notation (e.g., db['S1-test']). This is essential for names that are not valid Python identifiers and cannot be accessed via attribute notation.

Key Changes

  • Database class: Added __getitem__, __contains__, and __iter__ methods to support item access for subjects. Improved __getattr__ to provide helpful error messages directing users to use item access for non-identifier names.

  • SubjectDB.surfaces (Surfaces class): Added __getitem__ method with informative KeyError messages listing available surfaces. Updated __getattr__ to avoid recursion issues with dunder attributes.

  • SubjectDB.transforms (Transforms class): Added __getitem__ method, __dir__ method, and improved __getattr__ to support both item and attribute access patterns.

  • XfmSet class: Added __getitem__ method for accessing transform types (e.g., 'coord', 'magnet'), __dir__ method, and improved __getattr__ with better error handling.

  • Error handling: All __getitem__ implementations now raise KeyError (not AttributeError) with helpful messages listing available options. Dunder attributes are explicitly excluded from filestore lookups to prevent recursion during object construction, copying, or pickling.

  • Documentation: Updated docs/database.rst with examples and explanation of item access for non-identifier names, including usage of in operator and iteration.

  • Tests: Added comprehensive test suite (cortex/tests/test_database.py) covering item access, attribute access, error messages, dunder attribute handling, and warning behavior.

Notable Implementation Details

  • Dunder attributes (__*__) are explicitly checked and raise AttributeError before attempting filestore lookups, preventing infinite recursion during object operations.
  • Error messages consistently list available options to help users discover valid names.
  • The implementation maintains backward compatibility with existing attribute-based access while adding the new item-based access pattern.

https://claude.ai/code/session_01XvzkAD2Fu6irNYq4uKyfRT

cortex.db.<subject> only works when the subject name happens to be a valid
python identifier, so a subject named e.g. "S1-test" was unreachable through
the tab interface. Add item access alongside attribute access at every level:

    cortex.db['S1-test'].surfaces['flat-orig'].get()
    cortex.db['S1-test'].transforms['full-head']['coord']

Database also gains __contains__ and __iter__, XfmDB gains __getattr__/__dir__
so attribute access works there too, and the bare `raise AttributeError` calls
now carry messages listing what is actually available. Dunder attributes are no
longer looked up in the filestore, which otherwise recursed through `subjects`
while the object was being copied or pickled.

Closes #699

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01XvzkAD2Fu6irNYq4uKyfRT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants