首页 > 解决方案 > 如何让 mypy 记住 hasattr?

问题描述

我正在检查hasattr对象是否具有属性。如果它存在,我分配它。但是,mypy 仍然抱怨has no attribute。我如何帮助 mypy 记住该属性存在?

MVCE

另存为example.py

from typing import Any


class MakeNoiseMixin:
    def make_noise(self):
        if isinstance(self, Cat) or hasattr(self, "cat"):
            cat = self if isinstance(self, Cat) else self.cat
            cat.meow()
        else:
            print("zZ")


class Cat(MakeNoiseMixin):
    def meow(self):
        print("meow!")


class Dog(MakeNoiseMixin):
    ...


class Human(MakeNoiseMixin):
    def __init__(self, cat):
        self.cat = cat


felix = Cat()
felix.make_noise()

tom = Dog()
tom.make_noise()

felix_owner = Human(felix)
felix_owner.make_noise()

跑:

$ python example.py 
meow!
zZ
meow!

$ example.py --check-untyped-defs
example.py:4: error: "MakeNoiseMixin" has no attribute "cat"

标签: pythonmypypython-typing

解决方案


这不是正在寻求的答案,而是将其作为一种方法放在这里。期待看到其他答案。

Fwiw,定义变量注释不会导致新的表列,但即使它是一个不是模型字段实例的类级别变量,Django 也会知道得更好。

from typing import Any

class MakeNoiseMixin:
    cat: Any
    def make_noise(self):
        if isinstance(self, Cat) or hasattr(self, 'cat'):
            cat = self if isinstance(self, Cat) else self.cat
            cat.meow()
        else:
            print("zZ")


class Cat(MakeNoiseMixin):
    def meow(self):
        print("meow!")


felix = Cat()
felix.make_noise()
$ mypy example.py --check-untyped-defs
Success: no issues found in 1 source file

推荐阅读