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

shutil.which cannot return PathLike, and fails with cmd: PathLike on Windows Python < 3.12 #13580

Merged
merged 2 commits into from
Mar 5, 2025

Conversation

Avasam
Copy link
Collaborator

@Avasam Avasam commented Mar 4, 2025

As found on Python's Discord: https://discord.com/channels/267624335836053506/891788761371906108/1346539455267213372

Linux:

>>> import shutil
>>> from pathlib import Path
>>> which(Path("bash"))
'/usr/bin/bash'

Python 3.10, Windows:

>>> import shutil
>>> from pathlib import Path
>>> shutil.which(Path("python"))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Avasam\AppData\Local\Programs\Python\Python310\lib\shutil.py", line 1492, in which
    if any(cmd.lower().endswith(ext.lower()) for ext in pathext):
  File "C:\Users\Avasam\AppData\Local\Programs\Python\Python310\lib\shutil.py", line 1492, in <genexpr>
    if any(cmd.lower().endswith(ext.lower()) for ext in pathext):
AttributeError: 'WindowsPath' object has no attribute 'lower'. Did you mean: 'owner'?

Python 3.12 & 3.13, Windows:

>>> import shutil
>>> from pathlib import Path
>>> shutil.which(Path("python"))
'E:\\Program Files\\uv\\cache\\archive-v0\\9TtCCyEjyO6hv113bBovk\\Scripts\\python.EXE'

Oh and all cases I found, folders return None

Relevant CPython PR (I think) that fixed Path on Windows: python/cpython#103179

This comment has been minimized.

Copy link
Contributor

github-actions bot commented Mar 4, 2025

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@srittau srittau merged commit 58a4c08 into python:main Mar 5, 2025
55 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants