From ee81dcccb55fe9815a38a69133de65a2d333a804 Mon Sep 17 00:00:00 2001 From: Leandro Lucarella Date: Thu, 5 Nov 2026 15:09:33 +0000 Subject: [PATCH] Document that `deprecated_aliases()` needs `__all__` A star import only asks the module `__getattr__` for names listed in `__all__`, so in a module without one the aliases silently disappear from `from module import *`, and code using them fails with a `NameError` instead of getting the deprecation warning. Warn about it in the docstring, and add `__all__` to the usage example and the README example so copying them does the right thing. Signed-off-by: Leandro Lucarella --- README.md | 3 +++ src/frequenz/core/warnings/_deprecated_aliases.py | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/README.md b/README.md index ea8f76c..47dde13 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/frequenz/core/warnings/_deprecated_aliases.py b/src/frequenz/core/warnings/_deprecated_aliases.py index 80da92b..f8f7dd6 100644 --- a/src/frequenz/core/warnings/_deprecated_aliases.py +++ b/src/frequenz/core/warnings/_deprecated_aliases.py @@ -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: @@ -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