mypy:不兼容的类型[class],预期类
当将类对象作为变量转换给函数时,我对mypy有问题:
from PIL import Image
def save_img(img: Image.Image) -> None:
img.save("file.png")
img = Image.open("any_image.png")
save_img(img) # mypy-error save_img has incompatible type "Type[Image]"; expected "Image"
如果我更改save_img
像follwoing一样,我会创建一个不同的mypy错误:
def save_img(img: Type[Image.Image]) -> None:
img.save("file.png") # mypy-error: missing positional argument 'self'
我如何正确注释类型功能定义?
I've got a problem with mypy when handing a class-object as variable to a function:
from PIL import Image
def save_img(img: Image.Image) -> None:
img.save("file.png")
img = Image.open("any_image.png")
save_img(img) # mypy-error save_img has incompatible type "Type[Image]"; expected "Image"
If I change save_img
like the follwoing, I create a different mypy error:
def save_img(img: Type[Image.Image]) -> None:
img.save("file.png") # mypy-error: missing positional argument 'self'
How do I correctly annotate the type in the function definition?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论