2025-11-12 00:28:07 +08:00

66 lines
1.7 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
"""
正确的dataclass继承测试
"""
from dataclasses import dataclass, field
from datetime import datetime
from enum import Enum
from typing import List, Optional, Dict, Any
# 定义枚举
class PlatformType(Enum):
XIAOHONGSHU = "xiaohongshu"
DOUYIN = "douyin"
# 正确的基类定义 - 所有必需参数在前
@dataclass
class BaseContent:
title: str
description: str
tags: List[str] = field(default_factory=list)
visibility: str = "public"
# 正确的子类定义 - 只有必需参数
@dataclass
class ImageNote(BaseContent):
images: List[str] = field(default_factory=list)
@dataclass
class VideoNote(BaseContent):
video_path: str
images: List[str] = field(default_factory=list)
def test_correct_design():
print('🚀 开始正确的dataclass设计测试')
print('=' * 50)
# 测试枚举
print(f'✅ 平台枚举: {PlatformType.XIAOHONGSHU.value}')
# 测试图文笔记
note = ImageNote(
title="测试笔记标题",
description="测试笔记描述",
images=["img1.jpg", "img2.jpg"],
tags=["测试", "笔记"]
)
print(f'✅ 图文笔记: {note.title}, {len(note.images)}张图片')
# 测试视频内容
video = VideoNote(
title="测试视频标题",
description="测试视频描述",
video_path="test.mp4",
images=["video_cover.jpg"],
tags=["测试", "视频"]
)
print(f'✅ 视频内容: {video.title}, 路径: {video.video_path}')
print('=' * 50)
print('🎉 正确的dataclass设计测试成功')
print('💡 关键:子类只能添加默认值参数,不能添加必需参数')
return True
if __name__ == "__main__":
test_correct_design()