(original) (raw)
I like \_\_fspath\_\_ because it looks like os.fsencode() and os.fsdecode().
Please no builtin function, we have enough of them, but make sure that the \_\_fspath\_\_ is accepted in all functions expecting a filename.
If you consider that a function would make your change simpler, I suggest to add os.fspath():
if isinstance(obj, str): return obj
try: return obj.\_\_fspath\_\_
except AttributeError: raise TypeError(...)
Victor