Fix contracts of `pathlib.Path` and `os` fs actions

Merged
JA
2mo ago
+522-114

Fix filesystem call contracts for Path and os; fixes #993.

In detail:

  • Add Path.replace() and keyword target= support; both Path methods return the destination Path, preserving relative spelling.
  • os.rename() and os.replace() still return None.
  • Support Path.unlink(missing_ok=True) and Path.stat(follow_symlinks=True) keyword arguments.
  • Validate Path.mkdir() modes consistently with os.mkdir().

Side node: This adds dedicated effects RenamePath, ReplacePath, RenameOs and ReplaceOs. We arguably want to decouple effect-caller Path.rename -> RenamePath from each effect. E.g. here we would only need two effects, e.g. DropResult and IntoPath.


Summary by cubic

Fixes the filesystem call contracts for pathlib.Path and os to match CPython, addressing #993.

  • Path.rename() and the new Path.replace() return the destination Path, preserving relative spelling; os.rename() and os.replace() still return None. Invalid targets raise the same TypeError wording as the os functions.
  • Adds target= keyword support, Path.unlink(missing_ok=True) (suppresses FileNotFoundError only when requested; other exceptions propagate), and Path.stat(follow_symlinks=True).
  • Path.mkdir() now validates mode as a C integer like os.mkdir() and accepts __index__ objects.
  • Path method arity errors now count the bound receiver, matching CPython.