-
Notifications
You must be signed in to change notification settings - Fork 530
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
RF: Provide functions to augment old Path.mkdir, Path.resolve methods #3050
Conversation
Codecov Report
@@ Coverage Diff @@
## master #3050 +/- ##
==========================================
- Coverage 67.03% 66.61% -0.42%
==========================================
Files 343 343
Lines 44077 44065 -12
Branches 5551 5549 -2
==========================================
- Hits 29545 29355 -190
- Misses 13775 13917 +142
- Partials 757 793 +36
Continue to review full report at Codecov.
|
d729a11
to
6d4e821
Compare
6d4e821
to
9eefdcd
Compare
A review would be appreciated. This fixes one of the two issues in #3046. |
except TypeError: # PY35 | ||
pass | ||
|
||
path = path.absolute() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this method documented? I had to go to the code to find it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$ pydoc pathlib.Path.absolute | cat
Help on function absolute in pathlib.Path:
pathlib.Path.absolute = absolute(self)
Return an absolute version of this path. This function works
even if the path doesn't point to anything.
No normalization is done, i.e. all '.' and '..' will be kept along.
Use resolve() to get the canonical path to a file.
Yeah, it looks like it's not on the web docs. Is that a problem?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nono, just confirming.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool. I did check that it exists in py35.
def _resolve_with_filenotfound(path, **kwargs): | ||
""" Raise FileNotFoundError instead of OSError """ | ||
try: | ||
return path.resolve(**kwargs) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure this is intended (PY35):
Python 3.5.5 | packaged by conda-forge | (default, Jul 23 2018, 23:45:43)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.6.1 -- An enhanced Interactive Python. Type '?' for help.
In [1]: %load_ext autoreload
...: %autoreload 2
In [2]: from nipype.utils import filemanip as fm
190927-11:57:03,978 nipype.utils INFO:
Running nipype version 1.3.0-dev+g9eefdcdb1 (latest: 1.2.3)
In [3]: from pathlib import Path
In [4]: fm._resolve_with_filenotfound(Path('/some/made/out/path'), strict=False)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-4-b819f7473f4f> in <module>
----> 1 fm._resolve_with_filenotfound(Path('/some/made/out/path'), strict=False)
~/workspace/nipype/nipype/utils/filemanip.py in _resolve_with_filenotfound(path, **kwargs)
64 """ Raise FileNotFoundError instead of OSError """
65 try:
---> 66 return path.resolve(**kwargs)
67 except OSError as e:
68 if isinstance(e, FileNotFoundError):
TypeError: resolve() got an unexpected keyword argument 'strict'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is. It's a helper function to promote OSErrors to FileNotFoundErrors, and lets all others get caught higher.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gotcha, yes, I see the TypeError
in path_resolve
. Thanks!
Summary
In #3046, we see that pytest using our monkey-patched
Path
objects. This seems generally unsafe, and evidently possible for us to screw up, so I've switched to functions that try to use the original methods, with fallback implementations when they fail. These are marked with#PY*
tags so we can drop them when we drop support for those versions.I've left the
Path.write_text
method for now, just because the logic there is simpler and we'll be dropping Python 3.4 very soon.Alternative to #3048.
List of changes proposed in this PR (pull-request)
Path.mkdir
monkeypatch withnipype.utils.filemanip.path_mkdir
Path.resolve
monkeypatch withnipype.utils.filemanip.path_resolve
Acknowledgment