Skip to content

Commit 5522601

Browse files
authored
関数の微修正 (#459)
* update * 不具合修正 * mypy修正 * 型ヒントの修正 * version up
1 parent 84fb968 commit 5522601

File tree

4 files changed

+34
-8
lines changed

4 files changed

+34
-8
lines changed

annofabapi/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.60.0"
1+
__version__ = "0.60.3"

annofabapi/utils.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,17 +162,23 @@ def can_put_annotation(task: Task, my_account_id: str) -> bool:
162162
return len(task["histories_by_phase"]) == 0 or task["account_id"] == my_account_id
163163

164164

165-
def get_message(internationalization_message: Dict[str, Any], lang: str = "en-US") -> Optional[str]:
165+
def get_message_for_i18n(internationalization_message: Dict[str, Any], lang: str = "en-US") -> str:
166166
"""
167167
アノテーション仕様で使われている`InternalizationMessage`クラスの値から、指定された言語のメッセージを取得する。
168168
169169
Args:
170170
internationalization_message: 多言語化されたメッセージ
171-
lang: 取得したいメッセージに対応する言語
171+
lang: 取得したいメッセージに対応する言語コード。`en-US`または`ja-JP`のみサポートしています。
172172
173173
Returns:
174-
指定した言語に対応するメッセージ。見つからない場合はNoneを返します。
175-
"""
174+
指定した言語に対応するメッセージ。
176175
177-
messages = internationalization_message["messages"]
178-
return more_itertools.first_true(messages, pred=lambda e: e["lang"] == lang)
176+
Raises:
177+
ValueError: 引数langに対応するメッセージが見つからない場合
178+
"""
179+
messages: List[Dict[str, str]] = internationalization_message["messages"]
180+
result = more_itertools.first_true(messages, pred=lambda e: e["lang"] == lang)
181+
if result is not None:
182+
return result["message"]
183+
else:
184+
raise ValueError(f"lang='{lang}'であるメッセージは見つかりませんでした。")

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "annofabapi"
3-
version = "0.60.0"
3+
version = "0.60.3"
44
description = "Python Clinet Library of AnnoFab WebAPI (https://annofab.com/docs/api/)"
55
authors = ["yuji38kwmt"]
66
license = "MIT"

tests/test_local_utils.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import pytest
2+
13
from annofabapi.models import TaskPhase
24
from annofabapi.utils import (
5+
get_message_for_i18n,
36
get_number_of_rejections,
47
get_task_history_index_skipped_acceptance,
58
get_task_history_index_skipped_inspection,
@@ -612,3 +615,20 @@ def test_get_task_history_index_skipped_inspection_検査1回_教師付で提出
612615
actual = get_task_history_index_skipped_inspection(task_history_list)
613616
expected = []
614617
assert all([a == b for a, b in zip(actual, expected)])
618+
619+
620+
def test_get_message_for_i18n():
621+
i18n_message = {
622+
"messages": [{"lang": "ja-JP", "message": "自動車"}, {"lang": "en-US", "message": "car"}],
623+
"default_lang": "ja-JP",
624+
}
625+
assert get_message_for_i18n(i18n_message) == "car"
626+
assert get_message_for_i18n(i18n_message, lang="ja-JP") == "自動車"
627+
628+
i18n_message2 = {
629+
"messages": [{"lang": "ja-JP", "message": "自動車"}],
630+
"default_lang": "ja-JP",
631+
}
632+
633+
with pytest.raises(ValueError):
634+
get_message_for_i18n(i18n_message2, lang="en-US")

0 commit comments

Comments
 (0)