Repository: praw-dev/praw

Issue:
IndexError on Invalid URL to Submission
**Describe the bug**
<!-- A clear and concise description of what the bug is. -->

If I give a URL of the form `https://reddit.com/comments` or `https://www.reddit.com/r/test/comments`, it returns an IndexError rather than InvalidURL. Strangely enough `https://redd.it` is not affected.

**To Reproduce**
Steps to reproduce the behavior:
1. `reddit.submission(url="https://www.reddit.com/r/test/comments/")`
2. `reddit.submission(url="https://reddit.com/comments/")`

**Expected behavior**
<!-- A clear and concise description of what you expected to happen. -->
An InvalidURL to be raised.

**Code/Logs**
<!-- include your code, without the Reddit() initialization, so as to not leak private credentials. -->
```python
IndexError                                Traceback (most recent call last)
<ipython-input-14-87a53f614e22> in <module>
----> 1 reddit.submission(url="https://reddit.com/comments/")

~/.local/share/virtualenvs/tmp-agwWamBd/lib/python3.8/site-packages/praw/reddit.py in submission(self, id, url)
    798
    799         """
--> 800         return models.Submission(self, id=id, url=url)

~/.local/share/virtualenvs/tmp-agwWamBd/lib/python3.8/site-packages/praw/models/reddit/submission.py in __init__(self, reddit, id, url, _data)
    538             self.id = id
    539         elif url is not None:
--> 540             self.id = self.id_from_url(url)
    541
    542         self._comments_by_id = {}

~/.local/share/virtualenvs/tmp-agwWamBd/lib/python3.8/site-packages/praw/models/reddit/submission.py in id_from_url(url)
    422                 )
    423         else:
--> 424             submission_id = parts[parts.index("comments") + 1]
    425
    426         if not submission_id.isalnum():

IndexError: list index out of range
```

**System Info**
 - OS: Mac OS 10.15.6
 - Python: 3.8.5
 - PRAW Version: 7.1.0



Discussion:
Is this issue still up for grabs? I'd like to raise a PR fixing this issue if it hasn't been picked up yet.
> Is this issue still up for grabs? I'd like to raise a PR fixing this issue if it hasn't been picked up yet.

Sure.

Resolving patch:
diff --git a/praw/models/reddit/submission.py b/praw/models/reddit/submission.py
--- a/praw/models/reddit/submission.py
+++ b/praw/models/reddit/submission.py
@@ -418,8 +418,13 @@ def id_from_url(url: str) -> str:
                 raise InvalidURL(
                     url, message="Invalid URL (subreddit, not submission): {}"
                 )
+
         elif "gallery" in parts:
             submission_id = parts[parts.index("gallery") + 1]
+
+        elif parts[-1] == "comments":
+            raise InvalidURL(url, message="Invalid URL (submission id not present): {}")
+
         else:
             submission_id = parts[parts.index("comments") + 1]
 


Test patch:
diff --git a/tests/unit/models/reddit/test_submission.py b/tests/unit/models/reddit/test_submission.py
--- a/tests/unit/models/reddit/test_submission.py
+++ b/tests/unit/models/reddit/test_submission.py
@@ -85,6 +85,8 @@ def test_id_from_url__invalid_urls(self):
             "http://reddit.com/comments/_/2gmzqe",
             "https://reddit.com/r/wallpapers/",
             "https://reddit.com/r/wallpapers",
+            "https://www.reddit.com/r/test/comments/",
+            "https://reddit.com/comments/",
         ]
         for url in urls:
             with pytest.raises(ClientException):