Skip to content

feat(stdlib): add pathlib module bindings#276

Merged
dbrattli merged 4 commits intomainfrom
repo-assist/bindings-pathlib-2026-04-26-507e311dc6c26dda
Apr 26, 2026
Merged

feat(stdlib): add pathlib module bindings#276
dbrattli merged 4 commits intomainfrom
repo-assist/bindings-pathlib-2026-04-26-507e311dc6c26dda

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

🤖 This is an automated pull request from Repo Assist, an AI assistant for this repository.

Summary

Adds F# bindings for Python's [pathlib]((docs.python.org/redacted) module — the modern, object-oriented path-handling library introduced in Python 3.4 and now preferred over os.path.

The core class is pathlib.Path, which Fable.Python users can now use directly from F#.

What's included

src/stdlib/Pathlib.fs

Construction

  • Path(string) — single-segment constructor
  • Path(string[]) — multi-segment constructor (varargs)
  • Path.cwd() / Path.home() — static factory methods

Properties (all pure, no I/O)

  • name, stem, suffix, suffixes, parent, parents, parts, root, anchor, drive

Path arithmetic

  • / operator (Path / string and Path / Path)
  • joinpath(parts...) — varargs join

Pure transformations

  • with_name, with_stem (3.9+), with_suffix
  • as_posix, as_uri
  • is_absolute, is_relative_to (3.9+, string and Path overloads)

I/O predicates

  • exists, is_file, is_dir, is_symlink, is_mount

File I/O

  • read_text() / read_text(encoding), read_bytes()
  • write_text(data) / write_text(data, encoding), write_bytes(data)

Directory and file operations

  • mkdir(), mkdir(mode, parents, exist_ok), mkdir_p() (creates all parents)
  • rmdir(), unlink(), unlink(missing_ok)
  • rename(target), replace(target) — Path and string overloads
  • resolve(), relative_to(other) — Path and string overloads
  • expanduser()
  • iterdir(), glob(pattern), rglob(pattern)

test/TestPathlib.fs — 36 tests

Covers: construction, properties, path arithmetic, transformations, predicates, file round-trips (write_text/read_text, write_bytes/read_bytes), directory operations (mkdir_p, rmdir, iterdir), and glob.

Trade-offs

  • Only Path is bound (not PurePath, PurePosixPath, PureWindowsPath, PosixPath, WindowsPath). Those can be added later if needed; Path covers the vast majority of use cases.
  • open() is intentionally omitted — F# idiomatic I/O via read_text/write_text/read_bytes/write_bytes is cleaner.

Usage example

open Fable.Python.Pathlib

let dir = Path.cwd () / "data"
dir.mkdir_p ()
let file = dir / "hello.txt"
file.write_text "Hello, pathlib!" |> ignore
printfn "%s" (file.read_text ())

Note

🔒 Integrity filter blocked 10 items

The following items were blocked because they don't meet the GitHub integrity level.

To allow these resources, lower min-integrity in your GitHub frontmatter:

tools:
  github:
    min-integrity: approved  # merged | approved | unapproved | none

Generated by Repo Assist · ● 3M ·

Adds F# bindings for Python's pathlib.Path class:
- Path construction (single string and multi-segment)
- Properties: name, stem, suffix, suffixes, parent, parents, parts, root, anchor, drive
- Path arithmetic: / operator (string and Path), joinpath
- Pure transformations: with_name, with_stem, with_suffix, as_posix, as_uri
- I/O predicates: exists, is_file, is_dir, is_symlink, is_mount, is_absolute
- I/O operations: resolve, relative_to, read_text, read_bytes, write_text, write_bytes
- Directory operations: mkdir, mkdir_p, rmdir, iterdir, glob, rglob
- File operations: unlink, rename, replace, expanduser
- Static factories: Path.cwd, Path.home
- is_relative_to (3.9+)

Includes 36 tests covering construction, properties, path arithmetic,
transformations, predicates, file round-trips, and directory operations.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@dbrattli dbrattli changed the title [Repo Assist] feat(stdlib): add pathlib module bindings feat(stdlib): add pathlib module bindings Apr 26, 2026
Fable emits primary-ctor parameters as Python kwargs, but
pathlib.Path.__init__ does not accept `path=` as a keyword, breaking
27 tests with `TypeError: PurePath.__init__() got an unexpected
keyword argument 'path'`. Switch to a no-arg primary ctor and declare
the string and varargs ctors via [<Emit>] so emission stays
positional. Also relax suffixes/parents/parts to `seq<_>` to match the
underlying tuple/list/_PathParents return types, and wrap the byte
fixture in builtins.bytes so write_bytes receives a real bytes object
instead of a fable.UInt8Array.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@dbrattli dbrattli marked this pull request as ready for review April 26, 2026 11:00
dbrattli and others added 2 commits April 26, 2026 13:00
# Conflicts:
#	src/Fable.Python.fsproj
#	test/Fable.Python.Test.fsproj
Lost during the merge conflict resolution.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@dbrattli dbrattli merged commit 0639c9a into main Apr 26, 2026
2 checks passed
@dbrattli dbrattli deleted the repo-assist/bindings-pathlib-2026-04-26-507e311dc6c26dda branch April 26, 2026 11:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant