mypy没有__ sub __'的超载变体

发布于 2025-02-12 06:47:04 字数 988 浏览 0 评论 0原文

我有这种情况:

def get_date_plus_n(d1 :Optional[date] , n:int) -> Optional[date] :
    if d1 :
       return d1 + n
    else :
       return None

def get_date_minus_n(d1 :Optional[date] , n:int) -> Optional[date] :
    if d1 :
       return d1 - n
    else :
       return None


def total(n:int) -> Optional[date]:
    if get_date_plus_n(n) and get_date_minus_n() :
        return get_date_plus_n(n)-  get_date_minus_n(n)
    else :
        return None

但是我会得到此错误运行total():

error: No overload variant of "__sub__" of "date" matches argument type "None"
note: Possible overload variants:
note:     def __sub__(self, timedelta) -> date
note:     def __sub__(self, datetime) -> NoReturn
note:     def __sub__(self, date) -> timedelta
note: Right operand is of type "Optional[date]"

idk如何处理此错误。在函数总数()中,我验证了先前方法的结果在应用 sub 方法之前是否有效,只是为了避免错误。但是显然,米皮不喜欢那样。

I have this case :

def get_date_plus_n(d1 :Optional[date] , n:int) -> Optional[date] :
    if d1 :
       return d1 + n
    else :
       return None

def get_date_minus_n(d1 :Optional[date] , n:int) -> Optional[date] :
    if d1 :
       return d1 - n
    else :
       return None


def total(n:int) -> Optional[date]:
    if get_date_plus_n(n) and get_date_minus_n() :
        return get_date_plus_n(n)-  get_date_minus_n(n)
    else :
        return None

But I'm getting this error running total():

error: No overload variant of "__sub__" of "date" matches argument type "None"
note: Possible overload variants:
note:     def __sub__(self, timedelta) -> date
note:     def __sub__(self, datetime) -> NoReturn
note:     def __sub__(self, date) -> timedelta
note: Right operand is of type "Optional[date]"

idk how to handle this error. In the function total() I verified if the result of the previous methods are valid before applying the sub method, just to avoid errors. But apparently, mipy does not like that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文