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.4k stars 453 forks source link

Comment not iterable #2007

Closed david2278 closed 6 months ago

david2278 commented 6 months ago

Describe the Bug

When running fullname.py, it fails saying "Comment not iterable". This is because line 17 expects a string.

17: if "_" in self.id:
18:     return self.id
19: return f"{self._kind}_{self.id}"

In my project, I added self.id = str(self.id) above line 17 to resolve the error. This may have side effects though so a temporary variable might be better.

Possible solution (in fullname.py):

id = str(self.id)
kind = str(self._kind)
if "_" in id:
    return id
return f"{kind}_{id}"

Desired Result

fullname.py gracefully handles non-string input by attempting to convert the object to a string

Code to reproduce the bug

res = prawFunc(*extraArgs, **extraKwArgs)

My code does not include sensitive credentials

Relevant Logs

^CTraceback (most recent call last):
  File "/home/david/wsl_projects/altcointip/src/cointipbot.py", line 586, in main
    self.check_subreddits()
  File "/home/david/wsl_projects/altcointip/src/cointipbot.py", line 368, in check_subreddits
    action = ctb_action.eval_comment(c, self)
  File "/home/david/wsl_projects/altcointip/src/ctb/ctb_action.py", line 1137, in eval_comment
    u_to = ctb_misc.reddit_get_parent_author(comment, ctb.reddit, ctb)
  File "/home/david/wsl_projects/altcointip/src/ctb/ctb_misc.py", line 64, in reddit_get_parent_author
    parentcomment = praw_call(comment.parent)
  File "/home/david/wsl_projects/altcointip/src/ctb/ctb_misc.py", line 38, in praw_call
    res = prawFunc(*extraArgs, **extraKwArgs)
  File "/home/david/wsl_projects/altcointip/.venv/lib/python3.8/site-packages/praw/models/reddit/comment.py", line 253, in parent
    if self.parent_id == self.submission.fullname:
  File "/home/david/wsl_projects/altcointip/.venv/lib/python3.8/site-packages/praw/models/reddit/base.py", line 35, in __getattr__
    self._fetch()
  File "/home/david/wsl_projects/altcointip/.venv/lib/python3.8/site-packages/praw/models/reddit/comment.py", line 188, in _fetch
    data = self._fetch_data()
  File "/home/david/wsl_projects/altcointip/.venv/lib/python3.8/site-packages/praw/models/reddit/base.py", line 87, in _fetch_data
    name, fields, params = self._fetch_info()
  File "/home/david/wsl_projects/altcointip/.venv/lib/python3.8/site-packages/praw/models/reddit/comment.py", line 200, in _fetch_info
    return "info", {}, {"id": self.fullname}
  File "/home/david/wsl_projects/altcointip/.venv/lib/python3.8/site-packages/praw/models/reddit/mixins/fullname.py", line 17, in fullname
    if "_" in self.id:
TypeError: argument of type 'Comment' is not iterable

This code has previously worked as intended

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

Operating System/Environment

Ubuntu 22.0.4 WSL

Python Version

Python 3.8.10

PRAW Version

7.7.1

Links, references, and/or additional comments?

prawFunc is the comment function on an instance of the reddit class

bboe commented 6 months ago

Hi,

The interface we provide expects a string not an object with a __str__ method. Being explicit and less complex follows the Zen of Python so we won't be making such a change. It's the caller's responsibility to pass in the correct type.

bboe commented 6 months ago

FYI, I see you're using altcointip. I had rewrote much of it specific for nyan coin a few years ago. You might find it easier to work from that source code and make adjustments necessary for whatever coin you're supporting (if you only need a single coin support): https://github.com/mathwizard1232/tipnyan

david2278 commented 6 months ago

Sorry, I was under the impression that it was an edge case from within the praw library since the call stack was 5 levels deep within praw. I went and checked the calling function in alttipbot and I think I've resolved this particular issue now though.

Thanks I'll definitely check it out! I've been upgrading altipbot to python3 so I can have the latest version of praw. I don't want to get into a situation where reddit makes an api breaking change and I can't get the latest version of praw because I'm on python2.