-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AttributeError: 'super' object has no attribute ... #549
Comments
You must import I see I have to change something in the documentation, this should be mentioned in PyObjC's introduction but it isn't. Background: PyObjC's proxy for Cocoa classes dynamically looks for methods as needed, both for performance reasons and because Cocoa classes can change at runtime without a way to cheaply detect this. Because of this the I've written a PEP that tries to fix this PEP 447, but now think this is too complex and that a better solution is possible. I haven't gotten around to fully flesh out a proposal for that though (and in any case, such a proposal would end up in Python 3.13 at the earliest). |
Can |
this still fails with pyobjc 9.1.1 and import AppKit
import objc
print(objc.__version__)
class Sub(AppKit.NSObject):
def init(self):
print("init")
self = objc.super().init()
return self
s = Sub.alloc().init()
|
Yes, you can. |
That's because you really have to use import AppKit
import objc
from objc import super
print(objc.__version__)
class Sub(AppKit.NSObject):
def init(self):
print("init")
self = super().init()
return self
s = Sub.alloc().init() |
thanks for the explanation! |
In my case, this was baffling to discover, because it looked like it worked correctly locally in local & alias builds for me, but then failed in release builds. I wonder if it would be possible to at least have a linter for this somehow, if a clearer error when using the wrong |
I'm afraid not. I've considered changing the type of the |
A linter is possible, but not something I can work on in the short term. |
There now is some documentation. Leaving the issue open for now to look into linter possibilities. |
Hmmm... Because of the way calls to argument less super is implemented in CPython it might be possible to detect this at runtime. In particular: there will be a Something that could be good enough (in if "__classcell__" in class_dict:
if "__module__" in class_dict:
mod = sys.modules[class_dict["__module__"]]
if not hasattr(mod, "super") or mod.super is not objc.super:
warnings.warn("Objective-C subclass uses super() but super is not objc.super", ...) Probably with configuring the used warning to always report. The code above is completely untested at this time. |
import objc
NSObject = objc.lookUpClass("NSObject")
class MyObject(NSObject):
def init(self):
self = super().init()
if self is None:
return None
self.a = 42
return self $ python3 a.py
/a.py:5: ObjCSuperWarning: Objective-C subclass uses super(), but super is not objc.super
class MyObject(NSObject): |
This adds a new warning ``objc.ObjCSuperWarning`` and uses that to warning about class definitions that use argumentless super in Objective-C subclasses without binding ``super`` to ``objc.super``.
Changeset d6e9b63 warns about using the wrong That's not entirely ideal because the warning will mostly be invisible when using a tool like py2app to create a standalone application, but makes it a lot easier to detect the issue (and this also found a recent bug in the PyObjC testsuite itself...) |
super! thanks |
Describe the bug
Calling
super()
in a subclasses of NSObject (like a NSView) triggers an error.Platform information
this worked fine in pyobjc 9.0.1
thanks!!
The text was updated successfully, but these errors were encountered: