Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

convert shutil._PathReturn to a type parameter? #13508

Open
dpinol opened this issue Feb 16, 2025 · 2 comments
Open

convert shutil._PathReturn to a type parameter? #13508

dpinol opened this issue Feb 16, 2025 · 2 comments
Labels
stubs: improvement Improve/refactor existing annotations, other stubs issues

Comments

@dpinol
Copy link

dpinol commented Feb 16, 2025

Since shutil._PathReturn is defined as Any, it may hide type errors to mypy-like tools. E.g in the following code, mypy will not detect that f function is invoked with an str argument instead of a Path.

import shutil
from pathlib import Path


def f(p: Path) -> None:
    pass


def cp(target: str | Path) -> None:
    p = shutil.copy("/src", target)
    return f(p)
@srittau srittau added the stubs: improvement Improve/refactor existing annotations, other stubs issues label Feb 17, 2025
@srittau
Copy link
Collaborator

srittau commented Feb 17, 2025

Unfortunately, changing this to a union in the general case would mean that users would need to add isinstance checks to the returned value in many cases. That said, looking at the implementation of copy(), I think we could do better for some cases: For example, passing str or bytes as dst will always return the same type. We could reflect that and keep the Any return type only when passing in a PathLike.

The best solution would be to fix the functions in the standard library to return consistent types, but that's out of scope for typeshed.

@dpinol
Copy link
Author

dpinol commented Feb 17, 2025

I understand, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stubs: improvement Improve/refactor existing annotations, other stubs issues
Projects
None yet
Development

No branches or pull requests

2 participants