deprecations
griffe_frequenz_core.deprecations ¤
Document deprecations that are expressed as a function call.
The usual way to deprecate a Python API is a decorator, and Griffe already reads
those: griffe-warnings-deprecated
turns @warnings.deprecated and @typing_extensions.deprecated into an admonition
and a label.
Some frequenz-core
helpers cannot use a decorator, so they deprecate through a call instead, and
nothing sees them. This module fills that gap for the two shapes frequenz-core
uses. See DeprecationsExtension
for what they look like and how to enable the extension.
Classes¤
griffe_frequenz_core.deprecations.DeprecationsExtension ¤
Bases: Extension
Mark the deprecations frequenz-core expresses through a call.
Two shapes are recognized, and both end up with the same treatment as a
decorator-based deprecation: a deprecated label, the message on the object's
deprecated field, and an admonition inserted at the top of its docstring.
The first is a table of module-level aliases kept alive by a module
__getattr__:
from typing import TYPE_CHECKING, TypeAlias
from frequenz.core.warnings import deprecated_aliases
if TYPE_CHECKING:
from mypkg.newmod import Widget as _Widget
Widget: TypeAlias = _Widget # plus its usual attribute docstring
else:
__getattr__ = deprecated_aliases(__name__, {"Widget": "mypkg.newmod"})
Every name in that table is marked, whether or not it is declared for type
checkers. A target written as "module:name" renames as well as moves.
The second is an enum member whose value is wrapped:
from frequenz.core.enum import Enum, deprecated_member
class TaskStatus(Enum):
OPEN = 1
PENDING = deprecated_member(1, "PENDING is deprecated, use OPEN instead")
Such a member is marked and its rendered value is rewritten from the wrapper
call back to the real value, so the documentation shows PENDING = 1. The
DeprecatedMember(1, "...") form frequenz-core also accepts is recognized
too: a class and a function are both read as a call.
Warning
Enum messages, alias names, and alias targets must be string literals
written directly in the call. A non-literal enum message or alias entry
leaves that member unmarked; a non-literal alias-table message instead
falls back to default_message. The alias table itself must be a dict
literal passed as the second positional argument or as aliases=; one
held in a constant leaves every alias in it unmarked. The value and the
message of an enum member wrapper must be written out, positionally or
by keyword, not unpacked from *args or **kwargs. Each of these
cases is logged at debug level, which mkdocs -v shows.
Enable it under the mkdocstrings Python handler, alongside the decorator one:
plugins:
- mkdocstrings:
handlers:
python:
options:
extensions:
- griffe_warnings_deprecated
- griffe_frequenz_core.deprecations
The defaults match griffe-warnings-deprecated, so the two render alike with
no configuration. Anything that needs changing is an option:
extensions:
- griffe_frequenz_core.deprecations:
kind: deprecated
alias_table_functions:
- frequenz.core.warnings.deprecated_aliases
- mypkg.compat.moved_to
Note
The paths this extension matches are options rather than constants on
purpose: if frequenz-core renames a helper, point the option at the new
path instead of waiting for a release of this package.
Note
There is one deliberate difference from griffe-warnings-deprecated:
given an empty title, it promotes the message into the admonition's
title, and this extension does not. The message here carries a rendered
cross-reference to the target, which does not belong in a title, and the
title is also what decides whether a docstring already documents its own
deprecation. Please do not "fix" this into a bug.
Source code in src/griffe_frequenz_core/deprecations.py
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 | |
Attributes¤
alias_table_functions
instance-attribute
¤
alias_table_functions = frozenset(alias_table_functions)
The paths of the functions that build a __getattr__ from an alias table.
default_message
instance-attribute
¤
The message template used when the alias table call passes none.
member_wrapper_functions
instance-attribute
¤
member_wrapper_functions = frozenset(
member_wrapper_functions
)
The paths of the callables that wrap a deprecated enum member's value.
show_target
instance-attribute
¤
Whether to render an alias' value as the target it points at.
title
instance-attribute
¤
The title of the admonition inserted in the docstring.
Methods:¤
__init__ ¤
__init__(
kind: str = "danger",
title: str | None = "Deprecated",
label: str | None = "deprecated",
*,
alias_table_functions: Sequence[str] = (
"frequenz.core.warnings.deprecated_aliases",
),
member_wrapper_functions: Sequence[str] = (
"frequenz.core.enum.deprecated_member",
"frequenz.core.enum.DeprecatedMember",
),
default_message: str = "{old} is deprecated. Use {new} instead.",
show_target: bool = True
) -> None
Initialize the extension.
| PARAMETER | DESCRIPTION |
|---|---|
kind
|
The kind of the admonition inserted in the docstring.
TYPE:
|
title
|
The title of that admonition. An empty title renders the admonition without one.
TYPE:
|
label
|
The label added to deprecated objects, or
TYPE:
|
alias_table_functions
|
The fully qualified paths of the functions that
build a module
TYPE:
|
member_wrapper_functions
|
The fully qualified paths of the callables
that wrap an enum member's value to deprecate it. A class works
as well as a function, since both are read as a call. Their
calls are read as if they had the signature of
TYPE:
|
default_message
|
The message template used when the alias table call
does not pass a
TYPE:
|
show_target
|
Whether to render an alias' value as the target it points at, rather than the private name imported for type checkers.
TYPE:
|
Source code in src/griffe_frequenz_core/deprecations.py
on_class_members ¤
Mark every wrapped enum member of the class, if it has any.
| PARAMETER | DESCRIPTION |
|---|---|
cls
|
The class whose members were just collected.
TYPE:
|
**kwargs
|
Everything else Griffe passes, all unused.
TYPE:
|
Source code in src/griffe_frequenz_core/deprecations.py
on_module_members ¤
Mark every name in the module's alias table, if it has one.
| PARAMETER | DESCRIPTION |
|---|---|
mod
|
The module whose members were just collected.
TYPE:
|
**kwargs
|
Everything else Griffe passes, all unused.
TYPE:
|