Python Mixin - Unresolved Attribute Reference [PyCharm]

前端 未结 3 1689
借酒劲吻你
借酒劲吻你 2021-01-04 01:34

I am using a mixin to separate a range of functionality to a different class. This Mixin is only supposed to be mixable with the only child class:

class Mixi         


        
3条回答
  •  执笔经年
    2021-01-04 02:03

    Declare the necessary fields in the Mixin like:

    class Mixin:
        foo:str
    
        def complex_operation(self):
            return self.foo.capitalize() 
    

    This way the mixin actually declares the fields a class must have to be able to use this mixin. Type hint will create warnings if extending class will put incompatible type into declared field.

    edit: Replaced foo = None with foo:str as suggested by @valex

提交回复
热议问题