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

RF: Provide functions to augment old Path.mkdir, Path.resolve methods #3050

Merged
merged 2 commits into from
Sep 28, 2019

Conversation

effigies
Copy link
Member

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)

  • Replace Path.mkdir monkeypatch with nipype.utils.filemanip.path_mkdir
  • Replace Path.resolve monkeypatch with nipype.utils.filemanip.path_resolve

Acknowledgment

  • (Mandatory) I acknowledge that this contribution will be available under the Apache 2 license.

@codecov
Copy link

codecov bot commented Sep 25, 2019

Codecov Report

Merging #3050 into master will decrease coverage by 0.41%.
The diff coverage is 90.47%.

Impacted file tree graph

@@            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
Flag Coverage Δ
#smoketests 48.92% <28.57%> (-1.43%) ⬇️
#unittests 64.19% <90.47%> (-0.04%) ⬇️
Impacted Files Coverage Δ
nipype/pipeline/engine/utils.py 77.26% <100%> (-3.51%) ⬇️
nipype/interfaces/base/traits_extension.py 95.81% <100%> (ø) ⬆️
nipype/utils/filemanip.py 78.41% <88.88%> (-1.86%) ⬇️
nipype/utils/tmpdirs.py 50% <0%> (-30.77%) ⬇️
nipype/workflows/fmri/fsl/preprocess.py 72.67% <0%> (-13.39%) ⬇️
nipype/__init__.py 59.57% <0%> (-8.52%) ⬇️
nipype/utils/config.py 61.82% <0%> (-4.31%) ⬇️
nipype/workflows/fmri/fsl/estimate.py 61.97% <0%> (-2.82%) ⬇️
nipype/interfaces/fsl/model.py 77.91% <0%> (-2.71%) ⬇️
... and 9 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 56a0335...9eefdcd. Read the comment docs.

@effigies
Copy link
Member Author

A review would be appreciated. This fixes one of the two issues in #3046.

except TypeError: # PY35
pass

path = path.absolute()
Copy link
Contributor

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.

Copy link
Member Author

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?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nono, just confirming.

Copy link
Member Author

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)
Copy link
Contributor

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'

Copy link
Member Author

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.

Copy link
Contributor

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!

@effigies effigies merged commit 1d1883b into nipy:master Sep 28, 2019
@effigies effigies deleted the fix/pathlib_patch branch September 28, 2019 14:13
@effigies effigies added this to the 1.3.0 milestone Oct 3, 2019
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