question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

FileLink should accept PathLike objects

See original GitHub issue

Starting with Python 3.6, the os.PathLike interface was introduced so that functions which operate on paths would accept not only str or bytes, but also pathlib.PurePath for example.

IPython.display.FileLink does not accept PathLike objects yet:

[In]: IPython.display.FileLink(pathlib.Path(r'C:\Windows\System32\drivers\etc\hosts'))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
C:\mnt\Anaconda\5.1.0\lib\site-packages\IPython\core\formatters.py in __call__(self, obj)
    343             method = get_real_method(obj, self.print_method)
    344             if method is not None:
--> 345                 return method()
    346             return None
    347         else:

C:\mnt\Anaconda\5.1.0\lib\site-packages\IPython\lib\display.py in _repr_html_(self)
    355                     "incorrect path." % self.path)
    356 
--> 357         return self._format_path()
    358 
    359     def __repr__(self):

C:\mnt\Anaconda\5.1.0\lib\site-packages\IPython\lib\display.py in _format_path(self)
    341 
    342     def _format_path(self):
--> 343         fp = ''.join([self.url_prefix,self.path])
    344         return ''.join([self.result_html_prefix,
    345                         self.html_link_str % (fp, self.path),

TypeError: sequence item 1: expected str instance, WindowsPath found

This could be implemented by calling os.fspath on the parameter, but care should be taken because the function did not exist before Python 3.6.

Also for what it’s worth pathlib objects have a method named as_uri().

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
ciupicricommented, Mar 27, 2018

I was think of something like:

try:
    from os import fsencode
except ImportError:
    fsencode = lamda path: path  # do nothing just like in previous versions

And call fsencode where it’s needed. Another possibility would be to make fsencode = str in the fallback, as you suggest.

0reactions
takluyvercommented, Mar 28, 2018

Closed b y #11060.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeError: expected str, bytes or os.PathLike object, not tuple ...
I'm building an application with Django. Whenever I try using os.path.join() in the 'DIRS' list of the TEMPLATES setting I get TypeError: ...
Read more >
Python | os.link() method - GeeksforGeeks
src: A path-like object representing the file system path. This is the source file path for which the hard link will be created...
Read more >
11.2. os.path — Common pathname manipulations
All of these functions accept either only bytes or only string objects as their parameters. The result is an object of the same...
Read more >
Please read!No, idea how to do this without using any - Chegg
PathLike object, not _io.StringIO) The customers.txt file link is: ... that the function does not use 'for': FAILURE: Function should not use 'for'...
Read more >
External CSS Stylesheets – How to Link CSS to HTML and ...
In this case you would need to write a path like "css/styles.css" . The type attribute. <link rel=" ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found