【问题标题】:mypy error: Value of type variable "_AnyPath" of "copyfile" cannot be "Union[str, Path]"mypy 错误:“copyfile”的类型变量“_AnyPath”的值不能是“Union [str, Path]”
【发布时间】:2019-06-27 22:55:22
【问题描述】:

这曾经适用于旧版本(0.6xx?)的 mypy:

import pathlib
import shutil
from typing import Union

def f(x: Union[str, pathlib.Path]):
    shutil.copyfile("bla", x)

但不是在它抱怨的 mypy 0.710 中:

error: Value of type variable "_AnyPath" of "copyfile" cannot be "Union[str, Path]"

应该如何解决?

【问题讨论】:

标签: python mypy


【解决方案1】:

这似乎是唯一的方法:

import os
import shutil
from typing import TypeVar

_AnyPath = TypeVar("_AnyPath", str, os.PathLike)

def f(x: _AnyPath):
    shutil.copyfile("bla", x)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-14
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-25
    相关资源
    最近更新 更多