Skip to content

Add descriptions to restriction AST type schema#756

Merged
yuji38kwmt merged 2 commits into
mainfrom
feat/restriction-ast-type-schema-description
May 12, 2026
Merged

Add descriptions to restriction AST type schema#756
yuji38kwmt merged 2 commits into
mainfrom
feat/restriction-ast-type-schema-description

Conversation

@yuji38kwmt
Copy link
Copy Markdown
Collaborator

概要

  • RestrictionAstType の JSON Schema に各 AST type の説明を出力するように変更
  • enum 定義に oneOf を追加し、const ごとの description を含めるようにした
  • schema 出力のテストを追加

動作確認

  • make format
  • make lint
  • pytest tests/util/test_attribute_restrictions.py

@kci-pr-agent
Copy link
Copy Markdown

kci-pr-agent Bot commented May 10, 2026

Title

Add descriptions to restriction AST type schema


Description

  • RestrictionAstTypeに説明追加

  • JSON SchemaにoneOfとdescription追加

  • __get_pydantic_json_schema__を実装

  • テストでoneOfとdescription検証追加


Changes walkthrough 📝

Relevant files
Enhancement
attribute_restrictions.py
RestrictionAstTypeにスキーマ説明を追加                                                         

annofabapi/util/attribute_restrictions.py

  • Pydantic v2用スキーマ生成ハンドラをインポート
  • Enumにdescriptionプロパティを追加
  • __get_pydantic_json_schema__でoneOfを生成
  • AST種別ごとの説明マッピングを定義
  • +41/-1   
    Tests
    test_attribute_restrictions.py
    テストでoneOfとdescriptionを検証                                                                 

    tests/util/test_attribute_restrictions.py

  • model_json_schemaテストを拡張
  • RestrictionAstTypeのoneOf項目を検証
  • スキーマdescriptionアサーションを追加
  • +12/-0   

    Need help?
  • Type /help how to ... in the comments thread for any questions about PR-Agent usage.
  • Check out the documentation for more information.
  • @kci-pr-agent
    Copy link
    Copy Markdown

    kci-pr-agent Bot commented May 10, 2026

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 PR contains tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Unused Import

    不使用のインポートが追加されています。BaseModelConfigDictFieldfield_serializermodel_validatorなどがこのファイル内で使用されていないため、不要なインポートを削除してください。

    from pydantic import BaseModel, ConfigDict, Field, GetJsonSchemaHandler, field_serializer, model_validator
    from pydantic.json_schema import JsonSchemaValue
    from pydantic_core import CoreSchema
    JSON Schema 記述不足

    RestrictionAstType の JSON Schema にトップレベルの description が設定されていません。テストが "属性制約ASTの種別です。" を期待しているため、__get_pydantic_json_schema__ メソッド内で適切な description を追加してください。

    def __get_pydantic_json_schema__(cls, core_schema: CoreSchema, handler: GetJsonSchemaHandler) -> JsonSchemaValue:
        """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

    Comment on lines +88 to +98
    """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
    Copy link
    Copy Markdown

    Choose a reason for hiding this comment

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

    Suggestion: JSON Schema のルートに enum 全体の説明が含まれていないため、json_schema["description"] を追加して明示的に説明文を設定してください。テストで期待される "属性制約ASTの種別です。" を指定するとマッチします。 [possible issue, importance: 9]

    Suggested change
    """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

    Comment on lines +82 to +84
    def description(self) -> str:
    """AST種別の説明文を返します。"""
    return _RESTRICTION_AST_TYPE_DESCRIPTIONS[self]
    Copy link
    Copy Markdown

    Choose a reason for hiding this comment

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

    Suggestion: マッピングに漏れがあると KeyError になる可能性があるため、dict.get を使ってデフォルト値を返すようにガードしてください。 [general, importance: 3]

    Suggested change
    def description(self) -> str:
    """AST種別の説明文を返します。"""
    return _RESTRICTION_AST_TYPE_DESCRIPTIONS[self]
    @property
    def description(self) -> str:
    """AST種別の説明文を返します。"""
    return _RESTRICTION_AST_TYPE_DESCRIPTIONS.get(self, "")

    Copy link
    Copy Markdown

    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

    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 が含まれることを検証するテストを追加

    Comment on lines +101 to +105
    _RESTRICTION_AST_TYPE_DESCRIPTIONS: dict[RestrictionAstType, str] = {
    RestrictionAstType.CHECKED: "チェックボックス属性がチェックされていることを表すAST種別です。",
    RestrictionAstType.UNCHECKED: "チェックボックス属性がチェックされていないことを表すAST種別です。",
    RestrictionAstType.IS_EMPTY: "属性値が空であることを表すAST種別です。",
    RestrictionAstType.IS_NOT_EMPTY: "属性値が空でないことを表すAST種別です。必須制約に相当します。",
    @yuji38kwmt yuji38kwmt merged commit ea61dbc into main May 12, 2026
    9 checks passed
    @yuji38kwmt yuji38kwmt deleted the feat/restriction-ast-type-schema-description branch May 12, 2026 14:58
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Labels

    None yet

    Projects

    None yet

    Development

    Successfully merging this pull request may close these issues.

    2 participants