Skip to content
Merged
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ from typing import TYPE_CHECKING, TypeAlias

from frequenz.core.warnings import DeprecatedAlias, deprecated_aliases

# List the aliases, or star imports won't see them.
__all__ = ["Decimal", "Rational"]

if TYPE_CHECKING:
# Type checkers can't see the runtime `__getattr__` in the `else` branch.
from decimal import Decimal as _Decimal
Expand Down
10 changes: 10 additions & 0 deletions src/frequenz/core/warnings/_deprecated_aliases.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,14 @@ def deprecated_aliases( # noqa: DOC502
assignment and treat every symbol in the module as [`Any`][typing.Any],
effectively disabling type checking for the entire module.

Warning: Star imports
A star import (`from module import *`) only consults `__getattr__` for
the names listed in the module's `__all__`. Without an `__all__`, it
takes only the names the module defines itself, so the aliases silently
disappear from star imports, and code using them fails with a
[`NameError`][] instead of getting the deprecation warning. Keep every
alias in `__all__` until it is removed.

Example: Usage
This is how a module that used to define `Decimal` itself, and now gets it
from [`decimal`][], keeps the old import path working:
Expand All @@ -313,6 +321,8 @@ def deprecated_aliases( # noqa: DOC502

from frequenz.core.warnings import DeprecatedAlias, deprecated_aliases

__all__ = ["Decimal"] # Keeps the alias in star imports

if TYPE_CHECKING:
# Private import, only for type checkers
from decimal import Decimal as _Decimal
Expand Down
Loading