Skip to content
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

Partial method enhancement #462

Merged
merged 14 commits into from
May 3, 2024

Conversation

qqfunc
Copy link
Contributor

@qqfunc qqfunc commented Apr 30, 2024

The partial method system was changed as follows:

  1. Add the "__" keyword mechanism Add mechanism for invoking methods with duplicated argument names. #148
  2. Change to check the order of keyword arguments strictly Partial method argument order #453
  3. Improve the error message Improve the error message of partial methods #461

PR Checklist:

  • All new features have been tested
  • All new features have been documented
  • I have read the CONTRIBUTING.md file
  • I will abide by the code of conduct

Copy link
Member

@freakboy3742 freakboy3742 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks pretty good. I've flagged a couple of testing issues, some minor implementation cleanups, and some updates to the release notes.

There's also a need for a documentation update to describe the __1 syntax.


+(NSUInteger) overloaded:(NSUInteger)arg duplicateArg:(NSUInteger)arg1 duplicateArg:(NSUInteger)arg2
{
return arg1 + arg2;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't confirm if arg1 and arg2 are being handled in the right order, as the addition is commutative. Something like 2*arg1 + 3*arg2 (or even arg1 - arg2) would let us confirm that arg1 and arg2 are being handled in the right order.

Example = ObjCClass("Example")

self.assertEqual(Example.overloaded(0, extraArg1=0, extraArg2=0), 1)
self.assertEqual(Example.overloaded(0, extraArg2=0, extraArg1=0), 2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test confirms we're calling the right method, but doesn't verify that the arguments are passed in as expected. Ideally the result should incorporate the values from the arguments.

f"Known keywords are:\n"
+ "\n".join(repr(keywords) for keywords in self.methods)
)
f"Invalid selector {specified_sel!r}. Available selectors are: "
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A minor cosmetic detail, but the quotes added by !r aren't needed here, because the identifier can't ever have spaces or anything else that might be ambiguous.

+(void) dummyForException:(NSUInteger)arg
{
// Dummy method for testing exception
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no need to have these extra methods just to demonstrate we can't call them correctly. The test will work just as well by calling Example.overloaded(0, notAnArgument=10). It would be arguably better to use overloaded:... , because there are more options, and some of them are ambiguous.

tests/test_core.py Show resolved Hide resolved

args = []
rest = frozenset()
rest = (*order,)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is technically correct, but isn't intuitive to read. I'd suggest creating the original order as a tuple comprehension; then this can be just rest = order, and the version in the else branch is rest = ("",) + order

changes/148.feature.rst Outdated Show resolved Hide resolved
changes/453.feature.rst Outdated Show resolved Hide resolved
@@ -0,0 +1 @@
Updated the exception message when calling Objective-C methods with invalid keyword arguments to include selector names.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message has been improved when an Objective-C selector cannot be found matching the provided arguments.

src/rubicon/objc/api.py Outdated Show resolved Hide resolved
@qqfunc
Copy link
Contributor Author

qqfunc commented May 2, 2024

Thanks for the reviews. I have made some changes as you say, and added a description to the documentation.

Copy link
Member

@freakboy3742 freakboy3742 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for those fixes. I've made a couple of additional tweaks to the documentation and change notes; but otherwise, this looks great!

Thanks for these fixes - the ambiguous argument problem has been one of those small annoying feature requests that I've wanted to fix for a while, but it's never been quite annoying enough to get to the top of my todo list.

``__1`` and ``__2`` are added to avoid argument name collisions. Since ``__``
and any part after it are ignored, it is possible to use any suffix starting
with it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

performSelector isn't a great example here, as it can be easily confused with the fact that we're discussing how to access selectors. Going "meta" in the tutorial is a potential source of confusion.

There's also the following paragraph, which contradicts this one; and the need for API documentation in addition to the tutorial.

@freakboy3742 freakboy3742 merged commit a6bdd62 into beeware:main May 3, 2024
26 checks passed
@qqfunc
Copy link
Contributor Author

qqfunc commented May 3, 2024

Thank you!

@qqfunc qqfunc deleted the partial-method-enhancement branch May 3, 2024 01:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants