简体   繁体   English

如何在 python class 中仅使一个方法通用

[英]How to make only a method generic in a python class

I have the following base class to represent fields:我有以下基数 class 来表示字段:

T = TypeVar("T")


@dataclass
class BaseClass(Generic[T]):
    def validate(self, value: T):
        raise NotImplementedError

I also have an enum to represent the available implementations of this class:我还有一个枚举来表示这个 class 的可用实现:

class Types(Enum):
    A = auto()
    B = auto()

    @staticmethod
    def from_instance(instance: BaseClass) -> "Types":
        if isinstance(instance, ClassA):
            return Types.A
        if isinstance(instance, ClassB):
            return Types.B
        raise ValueError("Not supported")

Now, from these class, I have several implementations:现在,从这些 class,我有几个实现:

@dataclass
class ClassA(BaseClass[str]):
    def validate(self, value: str):
        pass


@dataclass
class ClassB(BaseClass[int]):
    def validate(self, value: int):
        pass

After this setup, I have another class to store a list of BaseClass :完成此设置后,我有另一个 class 来存储BaseClass列表:

@dataclass
class Container:
    instances: List[BaseClass]

    def get_by_type(self, type: Types) -> List[BaseClass]:
        return [instance for instance in self.instances if type == Types.from_instance(instance)]

At the end I have the following code and the following error:最后我有以下代码和以下错误:

def function(fields_from_class_a: List[ClassA]):
    print(fields_from_class_a)


container = Container(instances=[ClassA(), ClassB()])
fields = container.get_by_type(Types.A)


# throws error:
#  Argument 1 to "function" has incompatible type "List[BaseClass[Any]]"; expected "List[ClassA]"
function(fields)

So my question is, can I modify the code in such a way that the method get_by_type is correctly typed?所以我的问题是,我可以修改代码以正确输入方法get_by_type吗?

ClassA is only mapped at runtime to Types.A in Types.from_instance() . ClassA仅在运行时映射到Types.A Types.from_instance()中的 Types.A。

You would have to use BaseClass[Literal[Types.A]] instead of ClassA :您将不得不使用BaseClass[Literal[Types.A]]而不是ClassA

# def function(fields_from_class_a: List[ClassA]):                     # Change this
def function(fields_from_class_a: List[BaseClass[Literal[Types.A]]]):  # to this

And type Container.get_by_type() as such:并输入Container.get_by_type()这样的:

TypesT = TypeVar("TypesT", Literal[Types.A], Literal[Types.B])   # Add this

# def get_by_type(self, type: Types) -> List[BaseClass]:         # Change this
def get_by_type(self, type: TypesT) -> List[BaseClass[TypesT]]:  # to this

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使类通用并正确推断其方法的返回类型 - How to make a class generic and correctly infer the return type of its method 如何在Python中创建一个泛型方法来执行多个管道shell命令? - How to make a generic method in Python to execute multiple piped shell commands? 如何使用函数式编程在 python 中创建通用方法? - How can I use functional programming to make a generic method in python? Python:如何使 class 方法与父 class 的实例方法通信 - Python: how to make a class method communicate with an instance method of a parent class 如何为Python类添加方法? - How to make an add method to a Python class? 我该如何创建一个类,该类从python中的另一个类继承方法,但又仅更改某个方法的某些部分? - How can I make a class that inherits methods from another class in python but also change only some part of a certain method? 我怎样才能使方法只能从 class 中使用 - How can i make a method available only from within the class Python:将类方法设为全局 - Python: make class method global 如何在 python 中的 class 线程安全中进行方法调用? - How to make method call in a class thread-safe in python? 如何在python中使类字段[list]只读? - How to make a class field [list] read-only in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM