分解工会的最佳方法
目标是坚持完全静态打字。 “解决”工会/可选类型并满足Mypy的最佳方法是什么? 例如:
from typing import Optional
def foo() -> bool:
item: Optional[str] = find() # here find is some arbitrary search which returns a string in case something was found or None if nothing found.
if(item == None):
return False # Nothing found in the first place, could not execute bar()
else:
return bar(item) # here mypy is unsatisfied and tells me, that type Optional[str] is not applicable to type str.
def bar(str) -> bool:
.....
那么,将可选的[str]“铸造”到str的最佳方法是什么?这是一种特定的毕pythonian方法吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,在发布问题后,我在“相关”问题中找到了答案。
因此,这取决于:
正确的代码块将是:
Okay, after Posting the question I found an answer in the "Related" questions.
So this is taken from:
Use attribute from Optional[Union[str, int]] parameter depending on its type
The correct code block would be: