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

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

这曾用于旧版本(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]"

应该如何修复?

这似乎是唯一的方法:

import os
import shutil
from typing import TypeVar

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

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