Skip to content
This repository was archived by the owner on Apr 21, 2023. It is now read-only.

Commit b0a21e7

Browse files
authored
Merge pull request pvieito#51 from philipturner/patch-8
Add further support for count computed property
2 parents d383a1b + 1b1cc1d commit b0a21e7

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

PythonKit/Python.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,10 @@ public struct ThrowingPythonObject {
394394
}
395395
return (elt0, elt1, elt2, elt3)
396396
}
397+
398+
public var count: Int? {
399+
base.checking.count
400+
}
397401
}
398402

399403

@@ -507,6 +511,10 @@ public struct CheckingPythonObject {
507511
}
508512
return (elt0, elt1, elt2, elt3)
509513
}
514+
515+
public var count: Int? {
516+
Int(Python.len(base))
517+
}
510518
}
511519

512520
//===----------------------------------------------------------------------===//
@@ -1402,7 +1410,7 @@ extension PythonObject : Sequence {
14021410

14031411
extension PythonObject {
14041412
public var count: Int {
1405-
Int(Python.len(self))!
1413+
checking.count!
14061414
}
14071415
}
14081416

Tests/PythonKitTests/PythonRuntimeTests.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ class PythonRuntimeTests: XCTestCase {
2020
XCTAssertEqual(1.5, polymorphicList[3])
2121
XCTAssertEqual(1.5, polymorphicList[-1])
2222

23+
XCTAssertEqual(4, polymorphicList.count as Int)
24+
XCTAssertEqual(4, polymorphicList.checking.count!)
25+
XCTAssertEqual(4, polymorphicList.throwing.count!)
26+
2327
polymorphicList[2] = 2
2428
XCTAssertEqual(2, polymorphicList[2])
2529
}
@@ -30,6 +34,10 @@ class PythonRuntimeTests: XCTestCase {
3034
XCTAssertEqual(2, Python.len(dict))
3135
XCTAssertEqual(1, dict["a"])
3236
XCTAssertEqual(0.5, dict[1])
37+
38+
XCTAssertEqual(2, dict.count as Int)
39+
XCTAssertEqual(2, dict.checking.count!)
40+
XCTAssertEqual(2, dict.throwing.count!)
3341

3442
dict["b"] = "c"
3543
XCTAssertEqual("c", dict["b"])

0 commit comments

Comments
 (0)