如何用泛型TypeVar替代扩展的Union类型注解实现类生成?
问题原因与修复方案
你的重构代码里,CreatedDetails没有被定义为泛型类——只是在__init__参数里用了CreatorRecord这个TypeVar,但类本身没有和这个泛型参数绑定。类型检查器无法把creator的类型和具体的Creator子类关联起来,自然识别不了foo、bar这类子类独有的属性。
正确的重构步骤
- 把
CreatedDetails改成泛型类,继承Generic[CreatorRecord],让类和泛型参数绑定 - 在Creator子类的
create方法中,用Self类型标注返回值,确保返回的CreatedDetails实例能关联当前Creator子类的类型
修复后的完整代码
from __future__ import annotations from typing import TypeVar, Generic from typing_extensions import Self # Python 3.11+ 可直接用 typing.Self # 用bound限定类型范围,确保只能传入Creator或其子类 CreatorRecord = TypeVar("CreatorRecord", bound="Creator") class CreatedDetails(Generic[CreatorRecord]): def __init__(self, creator: CreatorRecord, new: bool) -> None: self.creator: CreatorRecord = creator self.new = new class Creator: def create(self) -> CreatedDetails[Self]: return CreatedDetails(creator=self, new=True) class Creator1(Creator): def __init__(self) -> None: self.foo = "foo" def create(self) -> CreatedDetails[Self]: return CreatedDetails(creator=self, new=True) class Creator2(Creator): def __init__(self) -> None: self.bar = "bar" def create(self) -> CreatedDetails[Self]: return CreatedDetails(creator=self, new=False) # 测试代码 creator1 = Creator1() created = creator1.create() created.creator.foo # 类型检查器能正确识别foo属性 created.new creator2 = Creator2() created2 = creator2.create() created2.creator.bar # 类型检查器能正确识别bar属性
关键修改说明
- 泛型类绑定:
CreatedDetails(Generic[CreatorRecord])让类的实例和具体的Creator子类类型关联,类型检查器能跟踪creator的实际类型 - Self类型标注:
CreatedDetails[Self]确保每个Creator子类的create方法返回的实例,其creator属性是当前子类的类型,而非父类Creator - TypeVar的bound参数:
bound="Creator"限定CreatorRecord只能是Creator或其子类,避免传入无关类型,增强类型安全性
这样重构后,新增Creator子类时,只需要继承Creator并实现自身逻辑,不需要修改任何Union注解,类型检查器就能自动识别creator的属性。
内容的提问来源于stack exchange,提问作者Jossy
相关产品推荐
相关产品推荐

