1- from contextlib import suppress
2- from typing import Any , Final
3-
4- from packaging .version import Version
5-
6- from ..._meta import API_VERSION
1+ from typing import Final
72
83#
94# CHANGELOG formatted-messages for API routes
149# - Inspired on this idea https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html#describing-changes-between-versions
1510#
1611
17- # newly created endpoint in given version
12+ # new routes
1813FMSG_CHANGELOG_NEW_IN_VERSION : Final [str ] = "New in *version {}*\n "
1914
20- # changes in given version with message
15+ # new inputs/outputs in routes
16+ FMSG_CHANGELOG_ADDED_IN_VERSION : Final [str ] = "Added in *version {}*: {}\n "
17+
18+ # changes on inputs/outputs in routes
2119FMSG_CHANGELOG_CHANGED_IN_VERSION : Final [str ] = "Changed in *version {}*: {}\n "
2220
23- # marked as deprecated
24- FMSG_CHANGELOG_DEPRECATED_IN_VERSION : Final [str ] = (
21+ # removed on inputs/outputs in routes
22+ FMSG_CHANGELOG_REMOVED_IN_VERSION_FORMAT : Final [str ] = "Removed in *version {}*: {}\n "
23+
24+ FMSG_DEPRECATED_ROUTE_NOTICE : Final [str ] = (
2525 "🚨 **Deprecated**: This endpoint is deprecated and will be removed in a future release.\n "
2626 "Please use `{}` instead.\n \n "
2727)
2828
29- # marked as deprecated and will be removed in given version
30- FMSG_CHANGELOG_REMOVED_IN_VERSION : Final [str ] = "Removed in *version {}*: {}\n "
31-
32-
3329DEFAULT_MAX_STRING_LENGTH : Final [int ] = 500
3430
3531
3632def create_route_description (
3733 * ,
3834 base : str = "" ,
39- changelog : list [ str ] | None = None ,
40- deprecated_in : str | None = None ,
41- alternative : str | None = None ,
35+ deprecated : bool = False ,
36+ alternative : str | None = None , # alternative
37+ changelog : list [ str ] | None = None
4238) -> str :
4339 """
4440 Builds a consistent route description with optional deprecation and changelog information.
4541
4642 Args:
4743 base (str): Main route description.
48- changelog (list[str]): List of formatted changelog strings.
49- deprecated_in (str, optional): Version when this endpoint was deprecated.
50- alternative (str, optional): Alternative route to use if deprecated.
44+ deprecated (tuple): (retirement_date, alternative_route) if deprecated.
45+ changelog (List[str]): List of formatted changelog strings.
5146
5247 Returns:
5348 str: Final description string.
5449 """
5550 parts = []
5651
57- if deprecated_in and alternative :
58- parts .append (FMSG_CHANGELOG_DEPRECATED_IN_VERSION .format (alternative ))
52+ if deprecated :
53+ assert alternative , "If deprecated, alternative must be provided" # nosec
54+ parts .append (FMSG_DEPRECATED_ROUTE_NOTICE .format (alternative ))
5955
6056 if base :
6157 parts .append (base )
@@ -64,68 +60,3 @@ def create_route_description(
6460 parts .append ("\n " .join (changelog ))
6561
6662 return "\n \n " .join (parts )
67-
68-
69- def create_route_config (
70- base_description : str = "" ,
71- * ,
72- changelog : list [str ] | None = None ,
73- ) -> dict [str , Any ]:
74- """
75- Creates route configuration options including description based on changelog history.
76-
77- The function analyzes the changelog to determine if the endpoint:
78- - Is not yet released (if the earliest entry is in a future version)
79- - Is deprecated (if there's a removal notice in the changelog)
80-
81- Args:
82- base_description: Main route description
83- changelog: List of formatted changelog strings indicating version history
84-
85- Returns:
86- dict: Route configuration options that can be used as kwargs for route decorators
87- """
88- route_options : dict [str , Any ] = {}
89- changelog = changelog or []
90-
91- # Parse changelog to determine endpoint state
92- is_deprecated = False
93- alternative = None
94- is_released = False
95- current_version = Version (API_VERSION )
96-
97- for entry in changelog :
98- # Check for deprecation/removal entries
99- if FMSG_CHANGELOG_DEPRECATED_IN_VERSION .split ("{" )[0 ] in entry :
100- is_deprecated = True
101- # Extract alternative from deprecation message if possible
102- with suppress (IndexError , AttributeError ):
103- alternative = entry .split ("Please use `" )[1 ].split ("`" )[0 ]
104-
105- # Check for new version entries to determine if this is unreleased
106- elif FMSG_CHANGELOG_NEW_IN_VERSION .split ("{" )[0 ] in entry :
107- try :
108- version_str = entry .split ("New in *version " )[1 ].split ("*" )[0 ]
109- entry_version = Version (version_str )
110- # If the first/earliest entry version is greater than current API version,
111- # this endpoint is not yet released
112- if current_version < entry_version :
113- is_released = True
114- except (IndexError , ValueError , AttributeError ):
115- pass
116-
117- # Set route options based on endpoint state
118- route_options ["include_in_schema" ] = is_released
119- route_options ["deprecated" ] = is_deprecated
120-
121- # Create description
122- route_options ["description" ] = create_route_description (
123- base = base_description ,
124- changelog = changelog ,
125- deprecated_in = (
126- "Unknown" if is_deprecated else None
127- ), # We don't extract exact version
128- alternative = alternative ,
129- )
130-
131- return route_options
0 commit comments