New submission from conchylicultor <etiennefg....@gmail.com>:
I'm writing a subclass of `pathlib.Path` with custom `__fspath__`. To prevent bad usages (users should use `os.fspath(path)` instead of `str(path)` to access the underlying path), I would like to overwrite `str`: ``` class MyPath(pathlib.PosixPath): def __fspath__(self) -> str: # Custom fspath def __str__(self) -> str: raise TypeError( f'Please do not use `str()` directly:' ' * For display: Use, f-string, `.format` or `repr`' ' * To access the path string: Use `os.fspath`, as per PEP 519' ) def __format__(self, format_spec) -> str: return format(super().__str__(), format_spec) ``` However, this is breaking pathlib internal, as `str(self)` is used at many places. It would be nice if all internal `str()` calls where replaced by some `self._str()`. I also think it would be more semantically correct if some of the `__str__` call where replaced by `__fspath__`, like: https://github.com/python/cpython/blob/b30934e9afb0af3f8e2e5f0992445be775b3c630/Lib/pathlib.py#L748 ---------- components: Library (Lib) messages: 378710 nosy: conchylicultor priority: normal severity: normal status: open title: pathlib.Path should not call str() internally to allow inheritance versions: Python 3.10 _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue42052> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com