-
Notifications
You must be signed in to change notification settings - Fork 0
pythonic getter implementation #335
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
… ... * added `get_by_key()` for string queries * `__getitem__()` now only works with integer/slice indexing
…ist-like classes' `get` once and for all
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR implements pythonic getter patterns for MMIF objects, addressing issue #295. The changes separate list-like positional access from dictionary-like ID-based access, making the API more intuitive and consistent with Python conventions.
Key Changes
- DataList collections (
views,documents,annotations) now only support integer/slice indexing (list-like behavior) - High-level containers (Mmif, View) provide string-based ID access via bracket notation
- Added
get()method to MmifObject base class for safe access with default values across all MMIF objects - Deprecated the
get()method on DataList with a clear migration path
Reviewed Changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/test_serialize.py | Updated tests to use new access patterns; added comprehensive test coverage for integer indexing, slicing, and string access behavior; updated error handling test |
| tests/mmif_examples.py | Added helper function to load test data from local git repository or URL for offline development |
| mmif/serialize/model.py | Refactored DataList.getitem() to only accept int/slice; deprecated DataList.get(); added MmifObject.get() for safe access; enhanced documentation |
| mmif/serialize/view.py | Updated View.getitem() to use _items dict directly; added comprehensive documentation with examples |
| mmif/serialize/mmif.py | Refactored Mmif.getitem() for clearer error handling and to use _items dict directly; enhanced documentation |
| mmif/serialize/annotation.py | Enhanced documentation for get() methods in Annotation and Document classes; fixed default parameter forwarding in Document.get() |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| result = subprocess.run( | ||
| ['git', 'show', git_ref], | ||
| cwd=localmmif, | ||
| capture_output=True, | ||
| text=True, | ||
| check=True | ||
| ) |
Copilot
AI
Nov 18, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using subprocess.run() with shell=False is good for security, but the cwd parameter is set from an environment variable. Consider validating that localmmif is a valid directory path to prevent potential security issues:
if localmmif:
if not os.path.isdir(localmmif):
raise ValueError(f"LOCALMMIF path is not a valid directory: {localmmif}")
# ... rest of codeThis prevents potential issues if the environment variable is set to an invalid or malicious path.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot open a new pull request to apply changes based on this feedback Use pathlib, not os.path
| err_str = 'custom error as a single long string' | ||
| aview.metadata.error = err_str | ||
| aview.metadata.error = ErrorDict({'message': err_str}) | ||
| print(aview.metadata.error) |
Copilot
AI
Nov 18, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Debug print statement should be removed before merging. This appears to be leftover debugging code that should not be in the final test suite.
| print(aview.metadata.error) |
| return False | ||
|
|
||
| def __getitem__(self, key) -> Union['MmifObject', str, datetime]: | ||
| def __getitem__(self, key) -> Any: |
Copilot
AI
Nov 18, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method raises AttributeError - should raise a LookupError (KeyError or IndexError) instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot open a new pull request to apply changes based on this feedback, Implement KeyError
keighrim
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
go fix them. Also check the failed test at https://github.com/clamsproject/mmif-python/actions/runs/19481626098
| return False | ||
|
|
||
| def __getitem__(self, key) -> Union['MmifObject', str, datetime]: | ||
| def __getitem__(self, key) -> Any: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot open a new pull request to apply changes based on this feedback, Implement KeyError
| result = subprocess.run( | ||
| ['git', 'show', git_ref], | ||
| cwd=localmmif, | ||
| capture_output=True, | ||
| text=True, | ||
| check=True | ||
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot open a new pull request to apply changes based on this feedback Use pathlib, not os.path
…dation Co-authored-by: keighrim <9062727+keighrim@users.noreply.github.com>
[WIP] Update address feedback on Pythonic getter implementation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop #335 +/- ##
===========================================
+ Coverage 87.98% 88.19% +0.20%
===========================================
Files 13 13
Lines 1673 1677 +4
===========================================
+ Hits 1472 1479 +7
+ Misses 201 198 -3
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
fixes #295. See the issue for discussion.