classification
Title: os.fspath() bypasses __fspath__ for str subclasses
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: contrebasse, serhiy.storchaka
Priority: normal Keywords:

Created on 2018-05-25 10:00 by contrebasse, last changed 2018-05-25 14:00 by serhiy.storchaka. This issue is now closed.

Messages (2)
msg317666 - (view) Author: Joseph Martinot-Lagarde (contrebasse) Date: 2018-05-25 10:00
os.fspath() returns its argument if it is a str. That means that it bypasses __fspath__ for str subclasses.

This is the case for the library path.py for example.

This is a corner case that was discovered while trying to fix https://github.com/matplotlib/matplotlib/issues/11306

Minimal example:

```
import os

class MyPath(str):
    def __fspath__(self):
        print("Returns a pure string")
        return str(self)

os.fspath(MyPath())  # Prints nothing
```
msg317674 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-05-25 14:00
It works as documented.

https://docs.python.org/3/library/os.html#os.fspath
https://www.python.org/dev/peps/pep-0519/
History
Date User Action Args
2018-05-25 14:00:08serhiy.storchakasetstatus: open -> closed

nosy: + serhiy.storchaka
messages: + msg317674

resolution: not a bug
stage: resolved
2018-05-25 10:00:01contrebassecreate