python - mypy 错误:“copyfile”的类型变量“_AnyPath”的值不能是“Union [str, Path]”
问题描述
这曾经适用于 mypy 的旧版本(0.6xx?):
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]"
应该如何修复?
解决方案
这似乎是唯一的方法:
import os
import shutil
from typing import TypeVar
_AnyPath = TypeVar("_AnyPath", str, os.PathLike)
def f(x: _AnyPath):
shutil.copyfile("bla", x)
推荐阅读
- flutter - TextField 的 obscureText 选项问题
- swift - 在 NSOutlineView 源列表中重新定位显示/隐藏按钮
- three.js - 我无法在threejs中加载GLB 3D模型
- arrays - 将txt导入二维数组
- javascript - 在 webhook 中获取插槽填充答案作为变量
- python - “fc1 = tf.reshape(conv2, [-1, weights['wd1'].get_shape().as_list()[0]])”在做什么?
- node.js - Node/electron: child_process.exec 即使成功也报错
- javascript - 如何在输入焦点时 div 元素应按其大小缩放(增长)?
- java - 线程“主”java.lang.ClassCastException 中的异常:com.sun.speech.freetts.en.us.cmu_time_awb.AlanVoiceDirectory 类
- python - 从给定的字典列表创建嵌套输出如下