アプリケーションでmypyのテストを開始しましたが、instances of class
で作業しようとすると成功しません。Mypy:インスタンスのリスト?呼び出し可能?
ButtonGenerator.__new__
はBaseButton
(URLButton
)のリストまたは単にBaseButton
(URLButton
)インスタンスを受け入れるべきです。私もCallable
の一覧を試みた print(ButtonGenerator(URLButton('https://stackoverflow.com')))
:
buttons.py
class BaseButton(object):
def __init__(self):
pass
class ButtonGenerator(BaseButton):
def __new__(self, buttons: Union[List[BaseButton], BaseButton]) -> Union[List[Dict[str, str]], Dict[str, str]]:
if (isinstance(buttons, list)):
if (not all(isinstance(button, BaseButton) for button in buttons)):
raise ValueError('Invalid Buttons instances.')
return [button.button_dict for button in buttons]
return buttons.button_dict
class URLButton(BaseButton):
def __init__(self, url, title=None):
self.button_dict = {
'type': 'web_url',
'url': url
}
if (title):
self.button_dict['title'] = title
エラー
messenger/buttons.py:17: error: "BaseButton" has no attribute "button_dict"
messenger/buttons.py:19: error: "BaseButton" has no attribute "button_dict"
例。それはその暗黙の型以外を返すようにしようとした場合