-
Notifications
You must be signed in to change notification settings - Fork 51
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
add __new__
NS classes
#275
Comments
Original comment by Georg Seifert (Bitbucket: Schriftgestalt, GitHub: Schriftgestalt). I just tried to implement this and it doesn't seem to work. This did:
And immutable classes needs a ` |
Original comment by Ronald Oussoren (Bitbucket: ronaldoussoren, GitHub: ronaldoussoren). It is not as easy as this, I do want to add more There’s also classes that either cannot be created using the usual API but only through factory methods or other APIs. Those should not have |
I'm thinking about a way to do this cleanly, and not just for new. In particular, I'm thinking about a way to add PEP8-compliant "aliases" to ObjC classes using the metadata system. Doing this will be a lot of work, but would result in Cocoa classes that are much nicer to use from Python. The hard parts will be designing a system where subclassing still feels natural (the easiest solution is to force subclassers to use the regular "ugly" names, but that's not very nice), and devising a naming scheme for the PEP8 names (one that is predictable and can be scripted). |
I've started looking into this and have some ideas, but also a problem. First the basic idea (not fully fleshed out, hence vague):
This seems easy enough to implement, and can later be used as the base for creating nicer alternatives for other methods using the same pattern (with loads of handwaving for subclassing). There might be problem here though: value, error = NSAttributedString(url=..., options=..., documentAttributes=..., error=None) I'm currently inclined to accept this weirdness. A different possible problem: There's a number of init selectors with unnamed selector parts, e.g. Current plan is to work on this over the summer with inclusion in PyObjC 10, but this depends a lot on how much free time I'll have over the summer (and how much work there is in adapting to changes in macOS 15). |
A slightly more serious problem: Longer term I'd prefer to provide wrappers for all methods with a completion handler as async methods that can be awaited for. For example: https://developer.apple.com/documentation/vision/vncoremlrequest/2890152-initwithmodel?language=objc Not sure yet how to nicely convert this. Likely by having An additional problem here: both First stab at this issue should ignore the completionHandler/awaitable issue and just use completionHandler arguments. |
I have some code to calculate signatures for Next step is to generate two sets of output:
(1) allows for playing with the implementation, while (2) is easier for reviewing the interface. Once I have a first stab at an implementation for this I'll start a branch. UPDATE: My in progress script reports on about 3200 'init*' methods. Some of which are duplicates, but this does mean reviewing the generated interfaces won't be trivial. Also: a number of classes, like |
Small steps.... The following is partial documentation for .. class:: NSURL
.. method:: __new__(*, absoluteURLWithDataRepresentation, relativeToURL)
Equivalent to ``NSURL.alloc().initAbsoluteURLWithDataRepresentation_relativeToURL_(absoluteURLWithDataRepresentation, relativeToURL)``
.. method:: __new__(*, byResolvingBookmarkData, options, relativeToURL, bookmarkDataIsStale, error=None)
Equivalent to ``NSURL.alloc().initByResolvingBookmarkData_options_relativeToURL_bookmarkDataIsStale_error_(byResolvingBookmarkData, options, relativeToURL, bookmarkDataIsStale, error)``
.. method:: __new__(*, dataRepresentation, relativeToURL)
Equivalent to ``NSURL.alloc().initWithDataRepresentation_relativeToURL_(dataRepresentation, relativeToURL)``
.. method:: __new__(*, fileURLWithFileSystemRepresentation, isDirectory, relativeToURL)
Equivalent to ``NSURL.alloc().initFileURLWithFileSystemRepresentation_isDirectory_relativeToURL_(fileURLWithFileSystemRepresentation, isDirectory, relativeToURL)``
.. method:: __new__(*, fileURLWithPath)
Equivalent to ``NSURL.alloc().initFileURLWithPath_(fileURLWithPath)``
.. method:: __new__(*, fileURLWithPath, isDirectory)
Equivalent to ``NSURL.alloc().initFileURLWithPath_isDirectory_(fileURLWithPath, isDirectory)``
.. method:: __new__(*, fileURLWithPath, isDirectory, relativeToURL)
Equivalent to ``NSURL.alloc().initFileURLWithPath_isDirectory_relativeToURL_(fileURLWithPath, isDirectory, relativeToURL)``
.. method:: __new__(*, fileURLWithPath, relativeToURL)
Equivalent to ``NSURL.alloc().initFileURLWithPath_relativeToURL_(fileURLWithPath, relativeToURL)``
.. method:: __new__(*, scheme, host, path)
Equivalent to ``NSURL.alloc().initWithScheme_host_path_(scheme, host, path)``
.. method:: __new__(*, string)
Equivalent to ``NSURL.alloc().initWithString_(string)``
.. method:: __new__(*, string, relativeToURL)
Equivalent to ``NSURL.alloc().initWithString_relativeToURL_(string, relativeToURL)``
.. class:: NSURLAuthenticationChallenge
.. method:: __new__(*, authenticationChallenge, sender)
Equivalent to ``NSURLAuthenticationChallenge.alloc().initWithAuthenticationChallenge_sender_(authenticationChallenge, sender)``
.. method:: __new__(*, protectionSpace, proposedCredential, previousFailureCount, failureResponse, error, sender)
Equivalent to ``NSURLAuthenticationChallenge.alloc().initWithProtectionSpace_proposedCredential_previousFailureCount_failureResponse_error_sender_(protectionSpace, proposedCredential, previousFailureCount, failureResponse, error, sender)`` The logic is not yet ideal, see the inconsistency in naming for the first variant for .. class:: VNVector
.. method:: __new__(*, XComponent, yComponent)
Equivalent to ``VNVector.alloc().initWithXComponent_yComponent_(XComponent, yComponent)``
.. method:: __new__(*, r, theta)
Equivalent to ``VNVector.alloc().initWithR_theta_(r, theta)``
.. method:: __new__(*, vectorHead, tail)
Equivalent to ``VNVector.alloc().initWithVectorHead_tail_(vectorHead, tail)`` I've also not yet looked into consistency with the manual |
For python subclasses the keyword arguments are automatically calculated, the following now works in the branch: class MyObject(NSObject):
def initWithX_y_(self, x, y):
self = super().init()
self.x = x
self.y = y
return self
o = MyObject(x=4, y=5)
print(o.x, o.y) This still needs some work to sync up with the final algorithm to calculate keyword arguments (see note about VNVector in an earlier comment). |
The branch is now basically finished:
The only thing left to do is update the framework bindings with metadata for setting up the accepted keyword arguments for system classes. That will be done after the merge into the master branch. From the documentation:
|
This merges the implemention of a generic __new__ into the master branch, as part of the implementation for #275
Two steps left to do for this issue:
|
I'm closing this issue because the required changes have been done in the master branch and will be in the next release (waiting for some minor issue with completely regenerated metadata). |
Original report by Georg Seifert (Bitbucket: Schriftgestalt, GitHub: Schriftgestalt).
I found a comment in the Foundation `_
_init__.py:`
(https://bitbucket.org/ronaldoussoren/pyobjc/src/default/pyobjc-framework-Cocoa/Lib/Foundation/__init__.py#lines-110)`__new__` should be easy.
Add this method
And use it like this:
The text was updated successfully, but these errors were encountered: