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

`reddit.subreddits.popular()` yields `Iterator[Unknown]` #1992

Closed akd-io closed 7 months ago

akd-io commented 8 months ago

Describe the Bug

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)

Desired Result

reddit.subreddits.popular() yields Iterator[Subreddit]

Code to reproduce the bug

# See code in bug description

The Reddit() initialization in my code example does not include the following parameters to prevent credential leakage:

client_secret, password, or refresh_token.

Relevant Logs

# No 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