Skip to content

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

Closed
@petsuter

Description

@petsuter

I have this code which was accepted in a older version (0.6xx?) of mypy:

import pathlib
import shutil
from typing import Union

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

But is not accepted by mypy 0.710:

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

How should I fix my type annotations?
I tried this:

import os
import shutil
from typing import TypeVar

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

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

And that seems to work.


I asked this on StackOverflow but it was suggested to open a "bug/missing feature issue" here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions