Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 带绑定TypeVar的mypy工厂方法_Python_Mypy - Fatal编程技术网

Python 带绑定TypeVar的mypy工厂方法

Python 带绑定TypeVar的mypy工厂方法,python,mypy,Python,Mypy,我使用以下代码大致定义并使用了factory函数: import typing as t from pydantic import BaseModel M = t.TypeVar("M", bound=t.Union[t.Dict, BaseModel]) def foo(factory: t.Type[M]) -> M: ... return factory(**{"key": "value"}) cla

我使用以下代码大致定义并使用了factory函数:

import typing as t
from pydantic import BaseModel

M = t.TypeVar("M", bound=t.Union[t.Dict, BaseModel])

def foo(factory: t.Type[M]) -> M:
    ...
    return factory(**{"key": "value"})


class MyModel(BaseModel):
    key: str

foo(MyModel)
我从以下代码接收到错误
不兼容的返回值类型(得到“Union[Dict[Any,Any],BaseModel]”,预期为“M”)


使mypy接受此代码的正确方法是什么?

分别指定变体:

M=t.TypeVar(“M”,t.Dict,BaseModel)