Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ object so `isinstance` keeps working through both paths:
```python
from typing import TYPE_CHECKING, TypeAlias

from frequenz.core.warnings import DeprecatedAlias, deprecated_aliases
from frequenz.core.warnings import DeprecatedAlias, deprecated_symbols

# List the aliases, or star imports won't see them.
__all__ = ["Decimal", "Rational"]
Expand All @@ -211,7 +211,7 @@ else:
# In the `else`, never at module level: a `__getattr__` mypy can see makes
# every name it doesn't know in this module `Any`, typos in downstream
# imports included.
__getattr__ = deprecated_aliases(
__getattr__ = deprecated_symbols(
__name__,
# Warns "<module>.Decimal is deprecated. Use decimal.Decimal instead.",
# `since` is for the documentation.
Expand All @@ -223,6 +223,38 @@ else:
)
```

To deprecate a module constant or type variable, pass a `DeprecatedSymbol` to
`deprecated_symbols()`. In this example, `TIMEOUT_SECONDS` still returns `30.0`,
but the warning tells callers to use `TIMEOUT`:

```python
from datetime import timedelta
from typing import TYPE_CHECKING, Final

from frequenz.core.warnings import DeprecatedSymbol, deprecated_symbols

__all__ = ["TIMEOUT", "TIMEOUT_SECONDS"]

TIMEOUT: Final = timedelta(seconds=30)

# Private: `__getattr__` is only called for names the module doesn't have.
_TIMEOUT_SECONDS: Final = 30.0

if TYPE_CHECKING:
TIMEOUT_SECONDS: Final = _TIMEOUT_SECONDS
else:
__getattr__ = deprecated_symbols(
__name__,
# Warns "mypkg.TIMEOUT_SECONDS is deprecated. Use mypkg.TIMEOUT instead."
DeprecatedSymbol(
"TIMEOUT_SECONDS",
value=_TIMEOUT_SECONDS,
replacement="mypkg.TIMEOUT",
since="v1.5.0",
),
)
```

And check in a test that a piece of code doesn't warn, without the `"error"`
filter that would make `warnings.warn()` raise inside the code under test:

Expand Down
35 changes: 32 additions & 3 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,44 @@

## Summary

<!-- Here goes a general summary of what this release is about -->
You can now warn callers when they use a deprecated module constant, type variable or type alias.

## Upgrading

<!-- Here goes notes on how to upgrade from previous versions, including deprecations and what they should be replaced with -->
- Replace calls to `deprecated_aliases()` with `deprecated_symbols()`. The arguments stay the same. Calling the old function emits a deprecation warning. It will be removed in v2.0.0.

## New Features

<!-- Here goes the main new features and examples or instructions on how to use them -->
- Use `DeprecatedSymbol` for module attributes that cannot take the `@deprecated` decorator. You can pass `DeprecatedSymbol` and `DeprecatedAlias` entries to the same `deprecated_symbols()` call. Here is how to deprecate a timeout constant:

```python
from datetime import timedelta
from typing import TYPE_CHECKING, Final

from frequenz.core.warnings import DeprecatedSymbol, deprecated_symbols

__all__ = ["TIMEOUT", "TIMEOUT_SECONDS"]

TIMEOUT: Final = timedelta(seconds=30)

_TIMEOUT_SECONDS: Final = 30.0

if TYPE_CHECKING:
TIMEOUT_SECONDS: Final = _TIMEOUT_SECONDS
else:
__getattr__ = deprecated_symbols(
__name__,
# Warns "mypkg.TIMEOUT_SECONDS is deprecated. Use mypkg.TIMEOUT instead."
DeprecatedSymbol(
"TIMEOUT_SECONDS",
value=_TIMEOUT_SECONDS,
replacement="mypkg.TIMEOUT",
since="v1.5.0",
),
)
```

Callers still get `value` when they use the deprecated name. Set `replacement` to the fully qualified name callers should use instead. Use `since` to record the version for documentation. To customize the warning, pass a `message` template with `{old}` and, if you provide a replacement, `{new}`. Without a replacement, the default warning is `<old> is deprecated.` Give callers a message that tells them what to do next.

## Bug Fixes

Expand Down
4 changes: 1 addition & 3 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,7 @@ plugins:
paths: ["src"]
options:
extensions:
- griffe_warnings_deprecated:
kind: deprecated
title: Deprecated
- griffe_frequenz_core.deprecations
Comment thread
llucax marked this conversation as resolved.
Resolved
docstring_section_style: spacy
inherited_members: true
merge_init_into_class: false
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ dev-formatting = ["black == 26.10.1", "isort == 9.0.2"]
dev-mkdocs = [
"Markdown == 3.11.2",
"black == 26.10.1",
"griffe-warnings-deprecated == 1.1.2",
"griffe-frequenz-core == 1.1.0",
"mike == 2.2.0",
"mkdocs-gen-files == 0.6.1",
"mkdocs-literate-nav == 0.6.3",
Expand Down
22 changes: 16 additions & 6 deletions src/frequenz/core/warnings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@
common case of a library having to touch a symbol it deprecated itself.

It also provides
[`deprecated_aliases`][frequenz.core.warnings.deprecated_aliases], to keep the old
import path of a symbol that moved to another module, or was renamed, working,
warning whoever uses it, with a
[`DeprecatedAlias`][frequenz.core.warnings.DeprecatedAlias] entry saying where it
is now and since which version it is deprecated, and
[`deprecated_symbols`][frequenz.core.warnings.deprecated_symbols], to keep
module-level symbols that can't take a `@deprecated` decorator working, warning
whoever uses them: a [`DeprecatedAlias`][frequenz.core.warnings.DeprecatedAlias]
entry keeps the old import path of a symbol that moved to another module, or was
renamed, saying where it is now, and a
[`DeprecatedSymbol`][frequenz.core.warnings.DeprecatedSymbol] entry deprecates a
symbol where it is, such as a type variable or a constant. Both say since which
version they are deprecated. There is also
[`asserting_no_warnings`][frequenz.core.warnings.asserting_no_warnings] with its
[`asserting_no_deprecations`][frequenz.core.warnings.asserting_no_deprecations]
shortcut, to check in a test that a piece of code doesn't warn.
Expand Down Expand Up @@ -61,14 +64,21 @@ def convert(value: str) -> int:
"""

from ._asserting import asserting_no_deprecations, asserting_no_warnings
from ._deprecated_aliases import DeprecatedAlias, deprecated_aliases
from ._deprecated_symbols import (
DeprecatedAlias,
DeprecatedSymbol,
deprecated_aliases,
deprecated_symbols,
)
from ._ignoring import ignoring_deprecations, ignoring_warnings

__all__ = [
"DeprecatedAlias",
"DeprecatedSymbol",
"asserting_no_deprecations",
"asserting_no_warnings",
"deprecated_aliases",
"deprecated_symbols",
"ignoring_deprecations",
"ignoring_warnings",
]
Loading