praw-dev / praw

PRAW, an acronym for "Python Reddit API Wrapper", is a python package that allows for simple access to Reddit's API.

Home Page:http://praw.readthedocs.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Title-only submissions

HaluanUskoa opened this issue · comments

Describe the Bug

In this section of /praw/models/reddit/subreddit.py, the following code appears:

 if (bool(selftext) or selftext == "") == bool(url):
     msg = "Either 'selftext' or 'url' must be provided."
     raise TypeError(msg)

Should this be adjusted? It's possible to submit a title-only post without any selftext or url through the site's UI. Currently, it seems like it isn't possible to do that through PRAW since it needs one or the other of these parameters.

If these three lines were removed outright, the result would be that PRAW will always prioritize selftext over url. A few lines down from the above code, here, the following check is implemented:

     if selftext is not None:
            data.update(kind="self")
            if inline_media:
                body = selftext.format(
                    **{
                        placeholder: self._upload_inline_media(media)
                        for placeholder, media in inline_media.items()
                    }
                )
                converted = self._convert_to_fancypants(body)
                data.update(richtext_json=dumps(converted))
            else:
                data.update(text=selftext)
        else:
            data.update(kind="link", url=url)

Which effectively ignores url if both selftext and url are provided. The docs could be updated to reflect this, or an error could be raised at this point alerting the requester that both cannot be provided.

Desired Result

The ability to submit with only the title parameter, and neither of selftext or url.

Code to reproduce the bug

reddit.subreddit('test').submit('Title')

My code does not include sensitive credentials

  • Yes, I have removed sensitive credentials from my code.

Relevant Logs

Either 'selftext' or 'url' must be provided.

This code has previously worked as intended

I'm not sure, I haven't used this code before.

Operating System/Environment

Windows 10

Python Version

3.12.0

PRAW Version

7.7.1

Links, references, and/or additional comments?

No response

I believe the underlying endpoint requires it to be provided. You can still post without a selftext but you have to specify selftext="".

@LilSpazJoekp You're right, that worked. I hadn't tried providing an empty string explicitly, but was just trying to submit it without the parameter entirely. Thanks!