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
git.types.PathLike is partially unknown for Pylance/mypy #1473
I am using GitPython in my project and I use Pylance/mypy with the strict type checking settings. This setting is pretty important for my project's sanity.
As I've been using methods from GitPython that make uses of paths, I've been getting the following errors:
As you can notice, what raises this error is PathLike[Unknown], defined in git.types.PathLike. (Source file & lines)
After further investigation, I've found out that this is because os.PathLike is considered as a generic class (Like typing.List, typing.Dict and so on)
The fix is fairly simple, as it is to simply allow str for PathLike, which gives this result: os.PathLike[str]
(awesome! This issue's no longer a thing with this fix!)
Thanks for bringing this up. I think for correctness, the type should be os.PathLike[str|bytes] if this doesn't pose a problem. A PR for that would definitely be welcome, thanks for offering.
Hi,
I'll try to make a PR by tomorrow to fix this issue, I won't be available
for the next few days after then so hopefully you need anything else from
me!
On Wed, Jul 27, 2022, 02:17 Sebastian Thiel ***@***.***> wrote:
Thanks for bringing this up. I think for correctness, the type should be
os.PathLike[str|bytes] if this doesn't pose a problem. A PR for that
would definitely be welcome, thanks for offering.
—
Reply to this email directly, view it on GitHub
<#1473 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AOSDPZZQOSIQFLKRLFBSPA3VWB527ANCNFSM54UGC3MA>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
So, after digging thing a bit deeper, my error I had in #1474 was because I was trying to convert git.repo.Repo.working_tree_dir (With the "corrected" type hint) to a pathlib.Path object (Which should implement os.PathLike too), but pathlib.Path refuses the corrected type hint with bytes because pathlib.Path does not support bytes when creating a new instance of it.
My wonder here is why, actually, should I add bytes?
I sure would have not questioned if it was all good, but as for right now, I have wonder since pathlib.Path does not accept it...
Thanks for the follow-up. bytes are the correct choice on unix platforms which represent paths as bytes, no encoding can be assumed for that reason which would be the case for paths that are str, which in python is an interpreter controlled encoding that can change depending on many factors. It appears that Python made its choice and given the history of encoding errors I have witnessed with GitPython in the past decade I wonder what they are thinking. Now there seems to be no way around it being broken.
Accepting that, then not adding bytes would be the way to go and leaving the PR as is, right? If so, please feel free to remove bytes once again and it should be OK to merge. Thank you.
Activity
Byron commentedon Jul 27, 2022
Thanks for bringing this up. I think for correctness, the type should be
os.PathLike[str|bytes]if this doesn't pose a problem. A PR for that would definitely be welcome, thanks for offering.madebylydia commentedon Jul 28, 2022
madebylydia commentedon Aug 4, 2022
So, after digging thing a bit deeper, my error I had in #1474 was because I was trying to convert
git.repo.Repo.working_tree_dir(With the "corrected" type hint) to apathlib.Pathobject (Which should implementos.PathLiketoo), butpathlib.Pathrefuses the corrected type hint withbytesbecausepathlib.Pathdoes not support bytes when creating a new instance of it.My wonder here is why, actually, should I add bytes?
I sure would have not questioned if it was all good, but as for right now, I have wonder since
pathlib.Pathdoes not accept it...Byron commentedon Aug 7, 2022
Thanks for the follow-up.
bytesare the correct choice on unix platforms which represent paths as bytes, no encoding can be assumed for that reason which would be the case for paths that arestr, which in python is an interpreter controlled encoding that can change depending on many factors. It appears that Python made its choice and given the history of encoding errors I have witnessed with GitPython in the past decade I wonder what they are thinking. Now there seems to be no way around it being broken.Accepting that, then not adding bytes would be the way to go and leaving the PR as is, right? If so, please feel free to remove
bytesonce again and it should be OK to merge. Thank you.