Skip to content

Conversation

@keighrim
Copy link
Member

fixes #295. See the issue for discussion.

Copy link

Copilot AI left a 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.

Comment on lines 36 to 42
result = subprocess.run(
['git', 'show', git_ref],
cwd=localmmif,
capture_output=True,
text=True,
check=True
)
Copy link

Copilot AI Nov 18, 2025

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 code

This prevents potential issues if the environment variable is set to an invalid or malicious path.

Copilot uses AI. Check for mistakes.
Copy link
Member Author

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)
Copy link

Copilot AI Nov 18, 2025

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.

Suggested change
print(aview.metadata.error)

Copilot uses AI. Check for mistakes.
return False

def __getitem__(self, key) -> Union['MmifObject', str, datetime]:
def __getitem__(self, key) -> Any:
Copy link

Copilot AI Nov 18, 2025

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.

Copilot uses AI. Check for mistakes.
Copy link
Member Author

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

Copy link
Member Author

@keighrim keighrim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return False

def __getitem__(self, key) -> Union['MmifObject', str, datetime]:
def __getitem__(self, key) -> Any:
Copy link
Member Author

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

Comment on lines 36 to 42
result = subprocess.run(
['git', 'show', git_ref],
cwd=localmmif,
capture_output=True,
text=True,
check=True
)
Copy link
Member Author

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

Copy link
Contributor

Copilot AI commented Nov 18, 2025

@keighrim I've opened a new pull request, #336, to work on those changes. Once the pull request is ready, I'll request review from you.

Copilot AI and others added 3 commits November 18, 2025 23:21
…dation

Co-authored-by: keighrim <9062727+keighrim@users.noreply.github.com>
[WIP] Update address feedback on Pythonic getter implementation
@codecov
Copy link

codecov bot commented Nov 19, 2025

Codecov Report

❌ Patch coverage is 92.10526% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 88.19%. Comparing base (b08fd6e) to head (e32ec86).
⚠️ Report is 10 commits behind head on develop.

Files with missing lines Patch % Lines
mmif/serialize/mmif.py 94.73% 1 Missing ⚠️
mmif/serialize/model.py 93.75% 1 Missing ⚠️
mmif/serialize/view.py 50.00% 1 Missing ⚠️
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     
Flag Coverage Δ
unittests 88.19% <92.10%> (+0.20%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@keighrim keighrim merged commit 9c0d3ba into develop Nov 19, 2025
6 checks passed
@keighrim keighrim deleted the 295-pythonic-getters branch November 19, 2025 01:28
@github-project-automation github-project-automation bot moved this from Todo to Done in infra Nov 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

make .get() and indexing ([]) more pythonic

2 participants