Skip to content

Commit 1217c7a

Browse files
dbrattliclaude
andcommitted
fix(pathlib): make Path constructor emit positional args
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>
1 parent c93d43d commit 1217c7a

2 files changed

Lines changed: 20 additions & 7 deletions

File tree

src/stdlib/Pathlib.fs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,20 @@ open Fable.Core
88

99
/// Represents a filesystem path on the current OS (POSIX or Windows).
1010
/// Paths are immutable; operations return new Path instances.
11+
///
12+
/// `Path()` represents the current directory (`.`). For multi-segment construction
13+
/// use the `/` operator (`Path "a" / "b" / "c"`) or `joinpath`.
1114
/// See https://docs.python.org/3/library/pathlib.html#pathlib.Path
1215
[<Import("Path", "pathlib")>]
13-
type Path(path: string) =
16+
type Path() =
17+
/// Construct a Path from a single string segment.
18+
[<Emit("Path($0)")>]
19+
new(path: string) = Path()
20+
1421
/// Construct a Path by joining multiple path segments.
15-
/// Equivalent to Path(parts[0]) / parts[1] / …
22+
/// Equivalent to ``Path(parts[0]) / parts[1] / …``.
1623
[<Emit("Path($0...)")>]
17-
new([<ParamArray>] paths: string[]) = Path("")
24+
new([<ParamArray>] paths: string[]) = Path()
1825

1926
// -------------------------------------------------------------------------
2027
// Properties
@@ -34,19 +41,19 @@ type Path(path: string) =
3441

3542
/// All file extensions of the final component (e.g. [".tar"; ".gz"]).
3643
/// See https://docs.python.org/3/library/pathlib.html#pathlib.PurePath.suffixes
37-
member _.suffixes: ResizeArray<string> = nativeOnly
44+
member _.suffixes: string seq = nativeOnly
3845

3946
/// The logical parent of the path (directory containing this path).
4047
/// See https://docs.python.org/3/library/pathlib.html#pathlib.PurePath.parent
4148
member _.parent: Path = nativeOnly
4249

4350
/// Immutable sequence of the logical ancestors of the path.
4451
/// See https://docs.python.org/3/library/pathlib.html#pathlib.PurePath.parents
45-
member _.parents: ResizeArray<Path> = nativeOnly
52+
member _.parents: Path seq = nativeOnly
4653

4754
/// The path's components as a tuple of strings.
4855
/// See https://docs.python.org/3/library/pathlib.html#pathlib.PurePath.parts
49-
member _.parts: ResizeArray<string> = nativeOnly
56+
member _.parts: string seq = nativeOnly
5057

5158
/// The root component of the path (e.g. "/" on POSIX), or "".
5259
/// See https://docs.python.org/3/library/pathlib.html#pathlib.PurePath.root

test/TestPathlib.fs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module Fable.Python.Tests.Pathlib
22

33
open Fable.Python.Testing
44
open Fable.Python.Pathlib
5+
open Fable.Python.Builtins
56

67
// ============================================================================
78
// Construction
@@ -12,6 +13,11 @@ let ``test Path construction from string`` () =
1213
let p = Path "/tmp"
1314
p.str () |> equal "/tmp"
1415

16+
[<Fact>]
17+
let ``test Path construction from multiple segments`` () =
18+
let p = Path("/foo", "bar", "baz.txt")
19+
p.str () |> equal "/foo/bar/baz.txt"
20+
1521
[<Fact>]
1622
let ``test Path cwd returns absolute path`` () =
1723
let p = Path.cwd ()
@@ -201,7 +207,7 @@ let ``test Path write_text and read_text`` () =
201207
[<Fact>]
202208
let ``test Path write_bytes and read_bytes`` () =
203209
let tmp = Path.cwd () / "__pathlib_test_bytes__.bin"
204-
let data: byte[] = [| 0uy; 1uy; 2uy; 255uy |]
210+
let data = builtins.bytes [| 0uy; 1uy; 2uy; 255uy |]
205211
tmp.write_bytes data |> ignore
206212
let result = tmp.read_bytes ()
207213
tmp.unlink (missing_ok = true)

0 commit comments

Comments
 (0)