Add descriptions to restriction AST type schema#756
Conversation
TitleAdd descriptions to restriction AST type schema Description
Changes walkthrough 📝
|
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
| """AST種別ごとの説明を含むJSON Schemaを返します。""" | ||
| json_schema = handler(core_schema) | ||
| json_schema["oneOf"] = [ | ||
| { | ||
| "const": ast_type.value, | ||
| "title": ast_type.name, | ||
| "description": ast_type.description, | ||
| } | ||
| for ast_type in cls | ||
| ] | ||
| return json_schema |
There was a problem hiding this comment.
Suggestion: JSON Schema のルートに enum 全体の説明が含まれていないため、json_schema["description"] を追加して明示的に説明文を設定してください。テストで期待される "属性制約ASTの種別です。" を指定するとマッチします。 [possible issue, importance: 9]
| """AST種別ごとの説明を含むJSON Schemaを返します。""" | |
| json_schema = handler(core_schema) | |
| json_schema["oneOf"] = [ | |
| { | |
| "const": ast_type.value, | |
| "title": ast_type.name, | |
| "description": ast_type.description, | |
| } | |
| for ast_type in cls | |
| ] | |
| return json_schema | |
| def __get_pydantic_json_schema__(cls, core_schema: CoreSchema, handler: GetJsonSchemaHandler) -> JsonSchemaValue: | |
| """AST種別ごとの説明を含むJSON Schemaを返します。""" | |
| json_schema = handler(core_schema) | |
| json_schema["description"] = "属性制約ASTの種別です。" | |
| json_schema["oneOf"] = [ | |
| { | |
| "const": ast_type.value, | |
| "title": ast_type.name, | |
| "description": ast_type.description, | |
| } | |
| for ast_type in cls | |
| ] | |
| return json_schema |
| def description(self) -> str: | ||
| """AST種別の説明文を返します。""" | ||
| return _RESTRICTION_AST_TYPE_DESCRIPTIONS[self] |
There was a problem hiding this comment.
Suggestion: マッピングに漏れがあると KeyError になる可能性があるため、dict.get を使ってデフォルト値を返すようにガードしてください。 [general, importance: 3]
| def description(self) -> str: | |
| """AST種別の説明文を返します。""" | |
| return _RESTRICTION_AST_TYPE_DESCRIPTIONS[self] | |
| @property | |
| def description(self) -> str: | |
| """AST種別の説明文を返します。""" | |
| return _RESTRICTION_AST_TYPE_DESCRIPTIONS.get(self, "") |
There was a problem hiding this comment.
Pull request overview
RestrictionAstType(属性制約ASTの種別)に対して、JSON Schema 出力時に各 AST type ごとの説明文が含まれるよう拡張し、スキーマ出力のテストも追加するPRです。
Changes:
RestrictionAstTypeの JSON Schema にoneOfを追加し、constごとにdescriptionを出力- AST type の説明文を参照できるよう
RestrictionAstType.descriptionを追加 RestrictionAst.model_json_schema()の$defs["RestrictionAstType"]を検証するテストを追加
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| annofabapi/util/attribute_restrictions.py | RestrictionAstType の JSON Schema をカスタマイズして、AST type ごとの説明を oneOf で出力 |
| tests/util/test_attribute_restrictions.py | $defs["RestrictionAstType"] に description / oneOf が含まれることを検証するテストを追加 |
| _RESTRICTION_AST_TYPE_DESCRIPTIONS: dict[RestrictionAstType, str] = { | ||
| RestrictionAstType.CHECKED: "チェックボックス属性がチェックされていることを表すAST種別です。", | ||
| RestrictionAstType.UNCHECKED: "チェックボックス属性がチェックされていないことを表すAST種別です。", | ||
| RestrictionAstType.IS_EMPTY: "属性値が空であることを表すAST種別です。", | ||
| RestrictionAstType.IS_NOT_EMPTY: "属性値が空でないことを表すAST種別です。必須制約に相当します。", |
概要
RestrictionAstTypeの JSON Schema に各 AST type の説明を出力するように変更oneOfを追加し、constごとのdescriptionを含めるようにした動作確認
make formatmake lintpytest tests/util/test_attribute_restrictions.py