Skip to content

Use pathlib instead of os.path in paths.py - #12650

Closed
davbrito wants to merge 32 commits into
ipython:mainfrom
davbrito:pathlib-path.py
Closed

davbrito wants to merge 32 commits into
ipython:mainfrom
davbrito:pathlib-path.py

Conversation

@davbrito

Copy link
Copy Markdown

Replacing os.path by pathlib in IPython/path.py. See: #12515

@Carreau
Carreau force-pushed the master branch 2 times, most recently from aa92b4c to 3c2c185 Compare October 19, 2020 20:45
@davbrito

davbrito commented Oct 19, 2020 •

Copy link
Copy Markdown
Author

I am having problem passing this test, and others similar.

def test_get_ipython_dir_2():
    """test_get_ipython_dir_2, Testcase to see if we can call get_ipython_dir without Exceptions."""
    with patch_get_home_dir('someplace'), \
            patch.object(paths, 'get_xdg_dir', return_value=None), \
            patch.object(paths, '_writable_dir', return_value=True), \
            patch('os.name', "posix"), \
            modified_env({'IPYTHON_DIR': None,
                          'IPYTHONDIR': None,
                          'XDG_CONFIG_HOME': None
                         }):
        ipdir = paths.get_ipython_dir()

    nt.assert_equal(ipdir, os.path.join("someplace", ".ipython"))

The problem is this line: patch('os.name', "posix"). Because pathlib thinks it is running into a unix system then tries to use PosixPath.
Therefore it gets this error:

======================================================================
ERROR: test_get_ipython_dir_2, Testcase to see if we can call get_ipython_dir without Exceptions.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\David\Envs\ipy\lib\site-packages\nose\case.py", line 198, in runTest
    self.test(*self.arg)
  File "c:\users\david\proyectos\ipython\IPython\core\tests\test_paths.py", line 67, in test_get_ipython_dir_2
    ipdir = paths.get_ipython_dir()
  File "c:\users\david\proyectos\ipython\IPython\paths.py", line 45, in get_ipython_dir
    ipdir = Path(home_dir, ipdir_def)
  File "c:\python\python38\lib\pathlib.py", line 1035, in __new__
    raise NotImplementedError("cannot instantiate %r on your system"
NotImplementedError: cannot instantiate 'PosixPath' on your system

@Carreau what should I do?

jpavlav and others added 26 commits October 26, 2020 16:02
* Added pathlib in a few places where it makes sense to.
Using the %matplotlib magics would not always work without this fix: 
For `%matplotlib qt5` to work, `flush_figures` should not be in `get_ipython().events.callbacks['post_execute']`. Yet without this fix:
```python
Python 3.7.6 (default, Jan  8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)]
Type "copyright", "credits" or "license" for more information.

IPython 7.18.1 -- An enhanced Interactive Python.

In [1]: %matplotlib inline

In [2]: %matplotlib inline

In [3]: %matplotlib inline

In [4]: %matplotlib inline

In [5]: get_ipython().events.callbacks['post_execute']
Out[5]: 
[<bound method AutoreloadMagics.post_execute_hook of <autoreload.AutoreloadMagics object at 0x00000199BFE73888>>,
 <function ipykernel.pylab.backend_inline.flush_figures()>,
 <function matplotlib.pyplot.install_repl_displayhook.<locals>.post_execute()>,
 <function ipykernel.pylab.backend_inline.flush_figures()>,
 <function ipykernel.pylab.backend_inline.flush_figures()>,
 <function ipykernel.pylab.backend_inline.flush_figures()>,
 <function ipykernel.pylab.backend_inline.flush_figures()>]

In [6]: %matplotlib qt5

In [7]: get_ipython().events.callbacks['post_execute']
Out[7]: 
[<bound method AutoreloadMagics.post_execute_hook of <autoreload.AutoreloadMagics object at 0x00000199BFE73888>>,
 <function matplotlib.pyplot.install_repl_displayhook.<locals>.post_execute()>,
 <function ipykernel.pylab.backend_inline.flush_figures()>,
 <function ipykernel.pylab.backend_inline.flush_figures()>,
 <function ipykernel.pylab.backend_inline.flush_figures()>,
 <function ipykernel.pylab.backend_inline.flush_figures()>]

In [8]: 
```
- Remove easily replaceable distutils imports with setuptools

- Sort the imports alphabetically

- Remove trailing whitespace
Travis is getting too slow to start, so migrating away some small steps to regain some CI time.
rushabh-v and others added 6 commits October 26, 2020 16:02
Travis seem to now have a lower limit on open-source builds; so reduce
dependency on Travis to get faster turnaround.

Will also limit the number of failures to 10, so that if there are many
test failing we don't spend too many cycles.
- fix utils.path.compress_user to accept path-like objects

- create a PathObject traitlet for ipython_dir

- fix test_paths.py: It's still pending to fix
some tests that fail because
of what is IMO a design error.
I just commented them out for now.
See: issue #12651
@davbrito

Copy link
Copy Markdown
Author

@Carreau this pull request is ready for review

Comment thread .gitignore Resolved
Comment thread IPython/core/traitlets.py
def from_string(self, s):
if self.allow_none and s == "None":
return None
return Path(s)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

All of this could also be suggested as a path to traitlets itself.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

You mean that I open an issue at the trailets repo?

@Carreau

Carreau commented Oct 27, 2020

Copy link
Copy Markdown
Member

There seem to be a number of commented test with TODO and the rebasing went wrong, I'll try to fix some of this.

@davbrito

Copy link
Copy Markdown
Author

There seem to be a number of commented test with TODO and the rebasing went wrong, I'll try to fix some of this.

I talked about those tests at #12651

@davbrito davbrito closed this by deleting the head repository Dec 30, 2022
@Carreau Carreau added this to the no action milestone Aug 28, 2023
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.