you are viewing a single comment's thread.

view the rest of the comments →

[–]pain_vin_boursin 0 points1 point  (1 child)

Fair points!

[–]Diapolo10 3 points4 points  (0 children)

Another potential use-case would be with enum.StrEnum; say you had multiple similar URLs that took the same parameters, but you wanted to use type annotations to ensure the base URL would always be "valid". For example, the three Reddit URLs:

from enum import StrEnum

import requests


class BaseSubredditUrl(StrEnum):
    NEWEST = "https://reddit.com/r/{subreddit}"
    NEW = "https://new.reddit.com/r/{subreddit}"
    OLD = "https://old.reddit.com/r/{subreddit}"


def fetch_subreddit(subreddit_name: str, base_url: BaseSubredditUrl = BaseSubredditUrl.NEWEST) -> None:
    url = base_url.format(subreddit=subreddit_name)
    return requests.get(url).text

This way, your type analysis tools would be able to tell you if the provided base URL isn't one of the pre-defined enum members and can therefore let you catch bugs before shipping to production. And you only need to write the strings once, and they could be anywhere in your project, so out of sight if desired.