Some py.path -> pathlib conversions#7619
Conversation
nicoddemus
left a comment
There was a problem hiding this comment.
Awesome work @bluetech! I've left a few comments, let me know what you think! 👍
dd12f05 to
7ddf479
Compare
| ) | ||
| p = Path(entry.path) | ||
|
|
||
| parents = p.parents |
nicoddemus
left a comment
There was a problem hiding this comment.
Great work! Left two minor comments, feel free to address them or not, up to you! 👍
src/_pytest/config/findpaths.py
Outdated
| return path.parent | ||
|
|
||
| def safe_exists(path: Path) -> bool: | ||
| # On Python<=3.7, this can throw on paths that contain characters |
There was a problem hiding this comment.
Perhaps we should use sys.version_info here as guard, so safe_exists can be dropped once only Python 3.8+ is supported?
if sys.version_info[:2] <= (3, 7):
def safe_exists(path: Path) -> bool:
# On Python<=3.7, this can throw on paths that contain characters
# unrepresentable at the OS level.
try:
return path.exists()
except OSError:
return False
else:
def safe_exists(path: Path) -> bool:
return path.exists()There was a problem hiding this comment.
Good idea, will change it.
There was a problem hiding this comment.
BTW. The reason this can happen at all is pretty funky.
pytest determines the rootdir by looking at all non-option arguments and finding the common ancestor etc.
But because this code runs before plugins are loaded, any plugin argument not prefixed by a -, such as test*bla in pytest --tx test*bla (as opposed to if the --tx=test*bla form was used) gets interpreted as a possible path. If it is actually a valid path then you'd have one seriously confused user...
I looked into fixing it but seems not really possible.
There was a problem hiding this comment.
Yeah it is not simple due to how options are parsed. 😕
An equivalent for these py.path.local functions is needed for some upcoming py.path -> pathlib conversions.
Didn't call it absolute or absolute_path to avoid conflicts with possible variable names. Didn't call it abspath to avoid confusion with os.path.abspath.
The first commit adds analogues of
py.path.local'sbestrelpathandcommonfunctions to_pytest.pathlib.The second commit converts
_pytest.config.findpathsto use pathlib.The third commit coverts some random places to pathlib.