没有“foo"的重载变体“动物"匹配参数类型“str" [英] No overload variant of "foo" of "Animal" matches argument type "str"

查看:51
本文介绍了没有“foo"的重载变体“动物"匹配参数类型“str"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件 t.py,它有一个类 Animal 和一个子类 Cat.两者都有方法 foo,它根据布尔值 inplace 的值具有不同的返回类型.

I have a file t.py which has a class Animal and a subclass Cat. Both have the method foo, which has different return types based on the value of the boolean inplace.

这是文件的完整代码:

# t.py

from __future__ import annotations

from typing import TypeVar, Optional, overload, Literal 

CatOrDog = TypeVar("CatOrDog", bound="Animal")


class Animal:
    @overload
    def foo(self: CatOrDog, inplace: Literal[False], bar) -> CatOrDog:
        ...

    @overload
    def foo(self: CatOrDog, inplace: Literal[True], bar) -> None:
        ...

    def foo(
        self: CatOrDog, inplace: bool = False, bar=None
    ) -> Optional[CatOrDog]:
        ...

    def ffill(self) -> Optional[CatOrDog]:
        return self.foo(bar="a")


class Cat(Animal):
    @overload
    def foo(self, inplace: Literal[False], bar) -> Cat:
        ...

    @overload
    def foo(self, inplace: Literal[True], bar) -> None:
        ...

    def foo(self, inplace: bool = False, bar=None) -> Optional[Cat]:
        ...

如果我在它上面运行 mypy,我得到

If I run mypy on it, I get

$ mypy t.py 
t.py:23: error: No overload variant of "foo" of "Animal" matches argument type "str"
t.py:23: note: Possible overload variants:
t.py:23: note:     def foo(self, inplace: Literal[False], bar: Any) -> Animal
t.py:23: note:     def foo(self, inplace: Literal[True], bar: Any) -> None
Found 1 error in 1 file (checked 1 source file)

如何正确重载foo,以便我可以调用self.foo(bar=a")?我试过设置 bar: Any,但这不起作用.

How can I properly overload foo, so that I can call self.foo(bar="a")? I have tried setting bar: Any, but that doesn't work.

推荐答案

您需要允许重载之一的默认参数,并在 ffill 中为 self 设置正确的类型 方法.

You need to allow default arguments for one of the overloads, and also set the correct type for self in the ffill method.

作为:

from __future__ import annotations

from typing import TypeVar, Optional, overload, Literal 

CatOrDog = TypeVar("CatOrDog", bound="Animal")


class Animal:
    @overload
    def foo(self: CatOrDog, inplace: Literal[False]=..., bar=...) -> CatOrDog:
        ...

    @overload
    def foo(self: CatOrDog, inplace: Literal[True], bar=...) -> None:
        ...

    def foo(
        self: CatOrDog, inplace: bool = False, bar=None
    ) -> Optional[CatOrDog]:
        ...

    def ffill(self: CatOrDog) -> Optional[CatOrDog]:
        
        return self.foo(bar="a")


class Cat(Animal):
    @overload
    def foo(self, inplace: Literal[False]=..., bar=...) -> Cat:
        ...

    @overload
    def foo(self, inplace: Literal[True], bar=...) -> None:
        ...

    def foo(self, inplace: bool = False, bar=None) -> Optional[Cat]:
        ...

参见 https://mypy-play.net/?mypy=latest&python=3.9&gist=49da369f6343543769eed2060fa61639

这篇关于没有“foo"的重载变体“动物"匹配参数类型“str"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆