Skip to content

Commit 72c5e82

Browse files
authored
Merge pull request #336 from clamsproject/copilot/sub-pr-335
[WIP] Update address feedback on Pythonic getter implementation
2 parents 15dd3df + ad8d4e2 commit 72c5e82

3 files changed

Lines changed: 10 additions & 9 deletions

File tree

mmif/serialize/mmif.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,4 @@ def __getitem__(self, item: str) -> Union[Document, View, Annotation, MmifMetada
868868
ret = self.views._items.get(item)
869869
if ret is None:
870870
raise KeyError(f"Object with ID {item} not found in the MMIF object. ")
871-
else:
872-
return ret
873-
else:
874-
return ret
871+
return ret

mmif/serialize/model.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,14 +337,14 @@ def __contains__(self, key: str) -> bool:
337337
try:
338338
self.__getitem__(key)
339339
return True
340-
except (TypeError, AttributeError, KeyError):
340+
except (TypeError, KeyError):
341341
return False
342342

343343
def __getitem__(self, key) -> Any:
344344
if key in self._named_attributes():
345345
value = self.__dict__[key]
346346
elif self._unnamed_attributes is None:
347-
raise AttributeError(f"Additional properties are disallowed by {self.__class__}: {key}")
347+
raise KeyError(f"Additional properties are disallowed by {self.__class__}: {key}")
348348
else:
349349
value = self._unnamed_attributes[key]
350350
if key not in self._required_attributes and self.is_empty(value):

tests/mmif_examples.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import itertools
22
import os
33
import subprocess
4+
from pathlib import Path
45
from string import Template
56
from urllib import request
67

@@ -20,8 +21,11 @@ def _load_from_url_or_git(url):
2021
If LOCALMMIF env var is set, use git show to load from local repo.
2122
LOCALMMIF should be the path to the local mmif repository.
2223
"""
23-
localmmif = os.environ.get('LOCALMMIF')
24-
if localmmif:
24+
localmmif_str = os.environ.get('LOCALMMIF')
25+
if localmmif_str:
26+
localmmif = Path(localmmif_str)
27+
if not localmmif.is_dir():
28+
raise ValueError(f"LOCALMMIF path is not a valid directory: {localmmif}")
2529
# Extract the version/branch and file path from the URL
2630
# URL format: https://raw.githubusercontent.com/clamsproject/mmif/{version}/{filepath}
2731
url_prefix = "https://raw.githubusercontent.com/clamsproject/mmif/"
@@ -35,7 +39,7 @@ def _load_from_url_or_git(url):
3539
try:
3640
result = subprocess.run(
3741
['git', 'show', git_ref],
38-
cwd=localmmif,
42+
cwd=str(localmmif),
3943
capture_output=True,
4044
text=True,
4145
check=True

0 commit comments

Comments
 (0)