You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
convert shutil._PathReturn to a type parameter? #13508
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.
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.
Given that the return type would be StrPath (ie: str | PathLike[str]) and that we can return a str if dst: str and str | _T otherwise, I'd argue that's a fine union to return.
Most cases of using a StrPath whether you actually have a str or a PathLike won't matter too much. If you do explicitly need a str after potentially passing a PathLike or explicitely need the same PathLike you passed, then you should really check the return here.
And once again, in most cases, you can coerce the type (using str() or Path())
Activity
srittau commentedon Feb 17, 2025
Unfortunately, changing this to a union in the general case would mean that users would need to add
isinstancechecks to the returned value in many cases. That said, looking at the implementation ofcopy(), I think we could do better for some cases: For example, passingstrorbytesasdstwill always return the same type. We could reflect that and keep theAnyreturn type only when passing in aPathLike.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 commentedon Feb 17, 2025
I understand, thanks
Avasam commentedon Apr 2, 2025
Given that the return type would be
StrPath(ie:str | PathLike[str]) and that we can return astrifdst: strandstr | _Totherwise, I'd argue that's a fine union to return.Most cases of using a
StrPathwhether you actually have astror aPathLikewon't matter too much. If you do explicitly need astrafter potentially passing aPathLikeor explicitely need the samePathLikeyou passed, then you should really check the return here.And once again, in most cases, you can coerce the type (using
str()orPath())(this argument may not hold as well for bytespath though, what's a common
PathLike[bytes]?)