Python Mixin - Unresolved Attribute Reference [PyCharm]

前端 未结 3 1691
借酒劲吻你
借酒劲吻你 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 01:55

    I see few options.

    1) Type annotations (i think this is cleanest solution):

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

    2) Default None (@ikamen option):

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

    3) Suppress unresolved reference error for class or for specific line (i think this is more dirty way than first two):

    # noinspection PyUnresolvedReferences
    class Mixin:
        def complex_operation(self):
            return self.foo.capitalize()
    
    class Mixin:
        def complex_operation(self):
            # noinspection PyUnresolvedReferences
            return self.foo.capitalize()
    

提交回复
热议问题