praw-dev / praw

PRAW, an acronym for "Python Reddit API Wrapper", is a python package that allows for simple access to Reddit's API.
http://praw.readthedocs.io/
BSD 2-Clause "Simplified" License
3.52k stars 462 forks source link

Title-only submissions #2000

Closed ghost closed 11 months ago

ghost commented 11 months ago

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

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

LilSpazJoekp commented 11 months ago

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

ghost commented 11 months ago

@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!