Skip to content

Commit 05a130d

Browse files
authored
Merge pull request pvieito#48 from philipturner/patch-6
Fix compiler failure
2 parents 933624f + 3da8778 commit 05a130d

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

PythonKit/Python.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1711,6 +1711,7 @@ public struct PythonClass {
17111711
self.init(name, superclasses: superclasses, members: members.dictionary)
17121712
}
17131713

1714+
@_disfavoredOverload
17141715
public init(_ name: String, superclasses: [PythonObject] = [], members: [String: PythonObject] = [:]) {
17151716
var trueSuperclasses = superclasses
17161717
if !trueSuperclasses.contains(Python.object) {

Tests/PythonKitTests/PythonFunctionTests.swift

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,27 @@ class PythonFunctionTests: XCTestCase {
8484
XCTAssertEqual(printOutput, "Class Dynamically Created!")
8585
}
8686

87+
// There is a build error where passing a simple `PythonClass.Members`
88+
// literal makes the literal's type ambiguous. It is confused with
89+
// `[String: PythonObject]`. To fix this error, we add a
90+
// `@_disfavoredOverload` attribute to the more specific initializer.
91+
func testPythonClassInitializer() {
92+
guard canUsePythonFunction else {
93+
return
94+
}
95+
96+
let MyClass = PythonClass(
97+
"MyClass",
98+
superclasses: [Python.object],
99+
members: [
100+
"memberName": "memberValue",
101+
]
102+
).pythonObject
103+
104+
let memberValue = MyClass().memberName
105+
XCTAssertEqual(String(memberValue), "memberValue")
106+
}
107+
87108
func testPythonClassInheritance() {
88109
guard canUsePythonFunction else {
89110
return
@@ -106,7 +127,7 @@ class PythonFunctionTests: XCTestCase {
106127
helloOutput = String(message)
107128

108129
// Conventional `super` syntax causes problems; use this instead.
109-
Python.Exception.__init__(self, message)
130+
Python.Exception.__init__(`self`, message)
110131
return Python.None
111132
},
112133

@@ -130,7 +151,7 @@ class PythonFunctionTests: XCTestCase {
130151
`self`.int_param = params[2]
131152

132153
// Conventional `super` syntax causes problems; use this instead.
133-
HelloException.__init__(self, message)
154+
HelloException.__init__(`self`, message)
134155
return Python.None
135156
},
136157

0 commit comments

Comments
 (0)