Skip to content

Conversation

@leavesster
Copy link
Contributor

Summary

  • Remove redundant __init__ methods from PrimitiveFieldSchema, VarFieldSchema, SecretFieldSchema, ArrayFieldSchema, ObjectFieldSchema
  • Subclasses now inherit the base FieldSchema.__init__() implementation

Problem

6 classes had nearly identical __init__ implementations:

def __init__(self, **kwargs):
    for key, value in kwargs.items():
        object.__setattr__(self, key, value)

Solution

Keep only one implementation in the base class FieldSchema. Subclasses that need special handling (like ArrayFieldSchema) call super().__init__() first.

Test Plan

  • All schema tests pass (test_schema.py)
  • Behavior unchanged

The __init__ methods in PrimitiveFieldSchema, VarFieldSchema, and
SecretFieldSchema were identical copies of the base class implementation.
Now they inherit from FieldSchema without overriding __init__.

ArrayFieldSchema and ObjectFieldSchema still have custom __init__ for
post-processing, but now call super().__init__(**kwargs) instead of
duplicating the loop.
Copilot AI review requested due to automatic review settings January 31, 2026 09:27
@coderabbitai
Copy link

coderabbitai bot commented Jan 31, 2026

总体说明

PrimitiveFieldSchemaVarFieldSchemaSecretFieldSchema 中移除显式 __init__ 样板代码,改为依赖数据类自动生成的构造器;同时更新 ArrayFieldSchemaObjectFieldSchema 的初始化逻辑,添加属性规范化处理。

变更

变更内容 摘要
Schema类重构
oocana/oocana/schema.py
简化 PrimitiveFieldSchemaVarFieldSchemaSecretFieldSchema 的构造器实现;ArrayFieldSchemaObjectFieldSchema 改为调用 super().__init__(**kwargs) 并强化属性规范化逻辑,包括将非 FieldSchema 值自动转换为 FieldSchema 对象。

代码审查工作量

🎯 3 (中等) | ⏱️ ~20 分钟

🚥 Pre-merge checks | ✅ 2
✅ Passed checks (2 passed)
Check name Status Explanation
Description check ✅ Passed 描述与变更集密切相关,详细说明了问题、解决方案和测试计划,与代码改动一致。
Title check ✅ Passed 标题完全符合指定格式要求,并准确总结了主要改动:从schema子类中移除重复的__init__方法。

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

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

This PR refactors the schema class hierarchy by removing duplicate __init__ methods from subclasses that don't require special initialization logic, consolidating on the base class implementation.

Changes:

  • Removed redundant __init__ methods from PrimitiveFieldSchema, VarFieldSchema, and SecretFieldSchema subclasses that had identical implementations to the base class
  • Updated ArrayFieldSchema and ObjectFieldSchema to call super().__init__(**kwargs) instead of duplicating the base initialization logic
  • All classes now properly inherit from FieldSchema.__init__() which uses object.__setattr__() to handle frozen dataclass constraints

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
oocana/oocana/schema.py (1)

66-79: ⚠️ Potential issue | 🟠 Major

移除 __init__ 后存在潜在的破坏性变更——缺少声明的字段会导致 TypeError。

当 dataclass 子类不定义自己的 __init__ 时,Python 会生成一个仅接受声明字段的 __init__,且不会调用父类的自定义 __init__。这会导致行为变化:

  • 之前:这些类接受任意 kwargs(通过父类 FieldSchema 的自定义 __init__object.__setattr__
  • 现在PrimitiveFieldSchemaVarFieldSchemaSecretFieldSchema 仅接受声明的字段(typecontentMediaType

如果 FieldSchema.generate_schema(dict) 传入包含额外字段(如 JSON Schema 的 descriptiondefaulttitle 等)的字典,将抛出 TypeError: unexpected keyword argument

此外,这与 ArrayFieldSchemaObjectFieldSchema 的行为不一致——后两者仍通过自定义 __init__ 调用 super().__init__(**kwargs) 接受任意 kwargs。这个设计模式在 data.py 中也有体现(见 StoreKey 类及其注释说明需要自定义 __init__ 来兼容额外字段)。

@leavesster leavesster changed the title refactor: remove duplicate __init__ methods from schema subclasses refactor(oocana): remove duplicate __init__ methods from schema subclasses Jan 31, 2026
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