refactor(oocana): remove duplicate __init__ methods from schema subclasses#456
refactor(oocana): remove duplicate __init__ methods from schema subclasses#456leavesster wants to merge 1 commit intomainfrom
Conversation
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.
总体说明从 变更
代码审查工作量🎯 3 (中等) | ⏱️ ~20 分钟 🚥 Pre-merge checks | ✅ 2✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Comment |
There was a problem hiding this comment.
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 fromPrimitiveFieldSchema,VarFieldSchema, andSecretFieldSchemasubclasses that had identical implementations to the base class - Updated
ArrayFieldSchemaandObjectFieldSchemato callsuper().__init__(**kwargs)instead of duplicating the base initialization logic - All classes now properly inherit from
FieldSchema.__init__()which usesobject.__setattr__()to handle frozen dataclass constraints
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
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__)- 现在:
PrimitiveFieldSchema、VarFieldSchema、SecretFieldSchema仅接受声明的字段(type、contentMediaType)如果
FieldSchema.generate_schema(dict)传入包含额外字段(如 JSON Schema 的description、default、title等)的字典,将抛出TypeError: unexpected keyword argument。此外,这与
ArrayFieldSchema和ObjectFieldSchema的行为不一致——后两者仍通过自定义__init__调用super().__init__(**kwargs)接受任意 kwargs。这个设计模式在data.py中也有体现(见StoreKey类及其注释说明需要自定义__init__来兼容额外字段)。
Summary
__init__methods fromPrimitiveFieldSchema,VarFieldSchema,SecretFieldSchema,ArrayFieldSchema,ObjectFieldSchemaFieldSchema.__init__()implementationProblem
6 classes had nearly identical
__init__implementations:Solution
Keep only one implementation in the base class
FieldSchema. Subclasses that need special handling (likeArrayFieldSchema) callsuper().__init__()first.Test Plan
test_schema.py)