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

Pub/Sub: staticmethod check #8091

Merged
merged 6 commits into from
May 23, 2019
Merged

Pub/Sub: staticmethod check #8091

merged 6 commits into from
May 23, 2019

Conversation

anguillanneuf
Copy link
Contributor

The GAPIC wrapper (code snippet below with comments) in pubsub/google/cloud/pubsub_v1/_gapic falsely recognizes static methods as instance methods.

    def wrap(wrapped_fx):
        """Wrap a GAPIC method; preserve its name and docstring."""
        # If this is a static or class method, then we need to *not*
        # send self as the first argument.
        #
        # Similarly, for instance methods, we need to send self.api rather
        # than self, since that is where the actual methods were declared.
        instance_method = True

        # If this is a bound method it's a classmethod. 
        self = getattr(wrapped_fx, "__self__", None) # <-- this would return None for staticmethod
        if issubclass(type(self), type):             # <-- this would return False for staticmethod
            instance_method = False                  # <-- resulting in instance_method = True for staticmethod

        # Okay, we have figured out what kind of method this is; send
        # down the correct wrapper function.
        if instance_method:
            fx = lambda self, *a, **kw: wrapped_fx(self.api, *a, **kw)  # noqa
            return functools.wraps(wrapped_fx)(fx)

        fx = lambda *a, **kw: wrapped_fx(*a, **kw)  # noqa
        return staticmethod(functools.wraps(wrapped_fx)(fx))

The PR adds a check for static methods and aims to make the code more readable.

@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label May 22, 2019
Copy link
Contributor

@plamut plamut left a comment

Choose a reason for hiding this comment

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

On the surface the fix looks good, the straightforward isinstance checks make the logic indeed more readable. 👍 Gave two suggestions for improvement.

I noticed, though, that we currently do not have any unit tests for the _gapic.add_methods() decorator, which could have prevented the discovered issue with static methods. Since already touching this part of the code, it represents a good chance to fill this gap and improve test coverage.

pubsub/google/cloud/pubsub_v1/_gapic.py Outdated Show resolved Hide resolved
pubsub/google/cloud/pubsub_v1/_gapic.py Outdated Show resolved Hide resolved
@plamut plamut added the needs work This is a pull request that needs a little love. label May 22, 2019
@anguillanneuf anguillanneuf requested a review from plamut May 22, 2019 17:17
Copy link
Contributor

@plamut plamut left a comment

Choose a reason for hiding this comment

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

The code LGTM now.

What about the tests, are they in the making?

@anguillanneuf
Copy link
Contributor Author

anguillanneuf commented May 22, 2019

Let me see if the test can prove that the code really works. I will add them next. Thanks for the quick turnaround!

@anguillanneuf anguillanneuf requested a review from plamut May 22, 2019 23:55
Copy link
Contributor

@plamut plamut left a comment

Choose a reason for hiding this comment

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

The tests looks good!

Just have two naming/comment suggestions, but otherwise lean towards merging this.

pubsub/tests/unit/pubsub_v1/test__gapic.py Outdated Show resolved Hide resolved
pubsub/tests/unit/pubsub_v1/test__gapic.py Outdated Show resolved Hide resolved
@sduskis sduskis added the api: pubsub Issues related to the Pub/Sub API. label May 23, 2019
@anguillanneuf anguillanneuf added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label May 23, 2019
@yoshi-kokoro yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label May 23, 2019
@anguillanneuf anguillanneuf added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label May 23, 2019
@yoshi-kokoro yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label May 23, 2019
@anguillanneuf anguillanneuf requested a review from plamut May 23, 2019 17:38
@plamut plamut removed the needs work This is a pull request that needs a little love. label May 23, 2019
Copy link
Contributor

@plamut plamut left a comment

Choose a reason for hiding this comment

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

Looks good now, merging. 👍

@anguillanneuf
Copy link
Contributor Author

I don't have permission to merge. If you do, please do! :)

@plamut plamut merged commit 967242c into master May 23, 2019
@anguillanneuf anguillanneuf deleted the decorator branch May 23, 2019 17:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: pubsub Issues related to the Pub/Sub API. cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants