Skip to content

Commit 658f3d3

Browse files
authored
util/annotation_specs.py: ValueErrorの条件を追加 (#716)
* アノテーション仕様が複数見つかったときの対応 * add * lint * add
1 parent 3e7c33c commit 658f3d3

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

annofabapi/util/annotation_specs.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from typing import Any, Literal, Optional, Union
22

33
import more_itertools
4-
from more_itertools import first_true
54

65
from annofabapi.models import Lang
76

@@ -70,21 +69,24 @@ def get_choice(choices: list[dict[str, Any]], *, choice_id: Optional[str] = None
7069
choice_name: 選択肢名(英語)
7170
7271
Raises:
73-
ValueError: 'choice_id'か'choice_name'の指定方法が間違っている。または引数に合致する選択肢情報が見つからない。
72+
ValueError: 'choice_id'か'choice_name'の指定方法が間違っている。または引数に合致する選択肢情報が見つからない。または複数見つかった。
7473
7574
"""
7675
if choice_id is not None and choice_name is not None:
7776
raise ValueError("'choice_id'か'choice_name'のどちらかはNoneにしてください。")
7877

7978
if choice_id is not None:
80-
result = first_true(choices, pred=lambda e: e["choice_id"] == choice_id)
79+
result = [e for e in choices if e["choice_id"] == choice_id]
8180
elif choice_name is not None:
82-
result = first_true(choices, pred=lambda e: get_english_message(e["name"]) == choice_name)
81+
result = [e for e in choices if get_english_message(e["name"]) == choice_name]
8382
else:
8483
raise ValueError("'choice_id'か'choice_name'のどちらかはNone以外にしてください。")
85-
if result is None:
84+
85+
if len(result) == 0:
8686
raise ValueError(f"選択肢情報が見つかりませんでした。 :: choice_id='{choice_id}', choice_name='{choice_name}'")
87-
return result
87+
if len(result) > 1:
88+
raise ValueError(f"選択肢情報が複数({len(result)}件)見つかりました。 :: choice_id='{choice_id}', choice_name='{choice_name}'")
89+
return result[0]
8890

8991

9092
def get_attribute(additionals: list[dict[str, Any]], *, attribute_id: Optional[str] = None, attribute_name: Optional[str] = None) -> dict[str, Any]:
@@ -96,20 +98,23 @@ def get_attribute(additionals: list[dict[str, Any]], *, attribute_id: Optional[s
9698
attribute_name: 属性名(英語)
9799
98100
Raises:
99-
ValueError: 'attribute_id'か'attribute_name'の指定方法が間違っている。または引数に合致する属性情報が見つからない。
101+
ValueError: 'attribute_id'か'attribute_name'の指定方法が間違っている。または引数に合致する属性情報が見つからない。または複数見つかった。
100102
"""
101103
if attribute_id is not None and attribute_name is not None:
102104
raise ValueError("'attribute_id'か'attribute_name'のどちらかはNoneにしてください。")
103105

104106
if attribute_id is not None:
105-
result = first_true(additionals, pred=lambda e: e["additional_data_definition_id"] == attribute_id)
107+
result = [e for e in additionals if e["additional_data_definition_id"] == attribute_id]
106108
elif attribute_name is not None:
107-
result = first_true(additionals, pred=lambda e: get_english_message(e["name"]) == attribute_name)
109+
result = [e for e in additionals if get_english_message(e["name"]) == attribute_name]
108110
else:
109111
raise ValueError("'attribute_id'か'attribute_name'のどちらかはNone以外にしてください。")
110-
if result is None:
112+
113+
if len(result) == 0:
111114
raise ValueError(f"属性情報が見つかりませんでした。 :: attribute_id='{attribute_id}', attribute_name='{attribute_name}'")
112-
return result
115+
if len(result) > 1:
116+
raise ValueError(f"属性情報が複数({len(result)}件)見つかりました。 :: attribute_id='{attribute_id}', attribute_name='{attribute_name}'")
117+
return result[0]
113118

114119

115120
def get_label(labels: list[dict[str, Any]], *, label_id: Optional[str] = None, label_name: Optional[str] = None) -> dict[str, Any]:
@@ -121,21 +126,24 @@ def get_label(labels: list[dict[str, Any]], *, label_id: Optional[str] = None, l
121126
label_name: ラベル名(英語)
122127
123128
Raises:
124-
ValueError: 'label_id'か'label_name'の指定方法が間違っている。または引数に合致するラベル情報が見つからない。
129+
ValueError: 'label_id'か'label_name'の指定方法が間違っている。または引数に合致するラベル情報が見つからない。または複数見つかった。
125130
126131
"""
127132
if label_id is not None and label_name is not None:
128133
raise ValueError("'label_id'か'label_name'のどちらかはNoneにしてください。")
129134

130135
if label_id is not None:
131-
result = first_true(labels, pred=lambda e: e["label_id"] == label_id)
136+
result = [e for e in labels if e["label_id"] == label_id]
132137
elif label_name is not None:
133-
result = first_true(labels, pred=lambda e: get_english_message(e["label_name"]) == label_name)
138+
result = [e for e in labels if get_english_message(e["label_name"]) == label_name]
134139
else:
135140
raise ValueError("'label_id'か'label_name'のどちらかはNone以外にしてください。")
136-
if result is None:
141+
142+
if len(result) == 0:
137143
raise ValueError(f"ラベル情報が見つかりませんでした。 :: label_id='{label_id}', label_name='{label_name}'")
138-
return result
144+
if len(result) > 1:
145+
raise ValueError(f"ラベル情報が複数({len(result)}件)見つかりました。 :: label_id='{label_id}', label_name='{label_name}'")
146+
return result[0]
139147

140148

141149
class AnnotationSpecsAccessor:

0 commit comments

Comments
 (0)