The type returned by reddit.subreddits.popular(limit=10) yields Iterator[Unknown] even though the definition of popular specifies Iterator["praw.models.Subreddit"] like this:
def popular(
self, **generator_kwargs: Union[str, int, Dict[str, str]]
) -> Iterator["praw.models.Subreddit"]:
"""Return a :class:`.ListingGenerator` for popular subreddits.
Additional keyword arguments are passed in the initialization of
:class:`.ListingGenerator`.
"""
return ListingGenerator(
self._reddit, API_PATH["subreddits_popular"], **generator_kwargs
)
You can see how I have to override the Iterator[Unknown] type with Iterator[Subreddit] below:
# Importing libraries
from typing import Iterator
import os
from dotenv import load_dotenv
import praw
from praw.models import Subreddit
# Load environment variables
load_dotenv()
CLIENT_ID = os.getenv('CLIENT_ID')
CLIENT_SECRET = os.getenv('CLIENT_SECRET')
USER_AGENT = os.getenv('USER_AGENT')
# Initializing Reddit API
reddit = praw.Reddit(
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
user_agent=USER_AGENT
)
# Getting all subreddits
subreddits: Iterator[Subreddit] = reddit.subreddits.popular(limit=10)
# Printing all subreddits
for subreddit in subreddits:
print(subreddit.display_name, subreddit.subscribers)
Describe the Bug
The type returned by
reddit.subreddits.popular(limit=10)
yieldsIterator[Unknown]
even though the definition ofpopular
specifiesIterator["praw.models.Subreddit"]
like this:You can see how I have to override the
Iterator[Unknown]
type withIterator[Subreddit]
below:Desired Result
reddit.subreddits.popular()
yieldsIterator[Subreddit]
Code to reproduce the bug
The
Reddit()
initialization in my code example does not include the following parameters to prevent credential leakage:client_secret
,password
, orrefresh_token
.Relevant Logs
This code has previously worked as intended.
Not sure, I haven't used this code before.
Operating System/Environment
macOS Ventura 13.5.2
Python Version
3.11.6
PRAW Version
7.7.1
Prawcore Version
2.4.0
Anything else?
I don't know why this happens, but had some discussion with a better Python dev than me pointing out a few PRs on the Python Discord here: https://discord.com/channels/267624335836053506/1171494426359640064