0

I have some code that calls a function from the Tweepy API per the docs:

api = tweepy.API(auth)
...
api.create_friendship(tweet.user.id)

Alternatively I've tried with the overload:

api.create_friendship(tweet.user.username)

Either implementation yields the error:

create_friendship() takes 1 positional argument but 2 were given

Doing some research this error can be avoided by passing in an instance of self into the function, which looks like this:

@payload('user')
def create_friendship(self, **kwargs):
    """ :reference: https://developer.twitter.com/en/docs/twitter-api/v1/accounts-and-users/follow-search-get-users/api-reference/post-friendships-create
    """
    return self.request(
        'POST', 'friendships/create', endpoint_parameters=(
            'screen_name', 'user_id', 'follow'
        ), **kwargs
    )

However I'm just running a simple script that isn't part of a class so I don't have any scope related to self. Is there an easy way around this?

Using python 3.9

user2961759
  • 137
  • 1
  • 8
  • You're calling `api.create_friendship`, which implies that it is, in fact, part of a class. Does the `payload` decorator automatically insert the function into a class somewhere else? Does it work if you include the `self` parameter? It's possible you're trying to solve a problem you don't have. – Kemp Mar 26 '21 at 15:19
  • `self` in this context is not an argument you'd pass from your script, it's a syntactic sugar largely used for managing inheritance. [Relevant SO answer](https://stackoverflow.com/a/287293/269970) – esqew Mar 26 '21 at 15:21
  • Can you share the contents of `tweet.user.username` and `tweet.user.id` that cause this error to be thrown? – esqew Mar 26 '21 at 15:21

1 Answers1

4

The signature and the documentation comment should have clued you in:

def create_friendship(self, **kwargs):
    """ :reference: https://developer.twitter.com/en/docs/twitter-api/v1/accounts-and-users/follow-search-get-users/api-reference/post-friendships-create
    """

From this, you should be able to infer that the create_friendship method takes one (implicit) positional argument (the instance of the API class), and some number of keyword arguments. I would assume the expected keyword arguments are screen_name=, user_id=, follow=, as described on the documentation page whose link appears in the doc comment.

This means that the function should be probably invoked as

api.create_friendship(user_id=tweet.user.id)
user3840170
  • 26,597
  • 4
  • 30
  • 62