From 5f5b2dc2c63514307449030ca7c88ff9d1600138 Mon Sep 17 00:00:00 2001 From: Karan Anand Date: Sun, 15 Feb 2026 15:31:22 -0500 Subject: [PATCH] Add type annotations to `ironicclient/common/i18n.py` This is an example of migrating a module to use type annotations, following the gradual migration strategy. Changes to `i18n.py`: - Add `from __future__ import annotations` - Add `from typing import Callable` import - Add type annotations for translation function and variables - Add type: ignore[no-redef] comment to suppress mypy warning about conditional definition of _ in try/except/else blocks Changes to pyproject.toml: - Add `ironicclient/common/i18n.py` to mypy files list for type checking Change-Id: Iffd9b5aff0530c2ddde0889cc31aa99e64b7f5a2 Signed-off-by: Karan Anand --- ironicclient/common/i18n.py | 12 +++++++++--- ironicclient/exc.py | 5 +++-- pyproject.toml | 4 +++- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/ironicclient/common/i18n.py b/ironicclient/common/i18n.py index 2561853a1..48b8d13ac 100644 --- a/ironicclient/common/i18n.py +++ b/ironicclient/common/i18n.py @@ -13,13 +13,19 @@ # License for the specific language governing permissions and limitations # under the License. +from __future__ import annotations + +from typing import Callable + try: import oslo_i18n except ImportError: - def _(msg): + def _(msg: str) -> str: return msg else: - _translators = oslo_i18n.TranslatorFactory(domain='ironicclient') + _translators: oslo_i18n.TranslatorFactory = oslo_i18n.TranslatorFactory( + domain="ironicclient" + ) # The primary translation function using the well-known name "_" - _ = _translators.primary + _: Callable[[str], str] = _translators.primary # type: ignore[no-redef] diff --git a/ironicclient/exc.py b/ironicclient/exc.py index 594fa1725..aa81914c3 100644 --- a/ironicclient/exc.py +++ b/ironicclient/exc.py @@ -45,8 +45,9 @@ class StateTransitionTimeout(exceptions.ClientException): """Timed out while waiting for a requested provision state.""" -def from_response(response, message=None, traceback=None, method=None, - url=None): +def from_response( # type: ignore[no-redef] + response, message=None, traceback=None, method=None, url=None +): """Return an HttpError instance based on response from httplib/requests.""" error_body = {} diff --git a/pyproject.toml b/pyproject.toml index a8f78b717..3ef2fbfff 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -9,4 +9,6 @@ warn_unused_configs = true ignore_missing_imports = true # Modules that have been fully type-annotated. Add modules here as they are # migrated to enable strict type checking for them. -files = [] \ No newline at end of file +files = [ + "ironicclient/common/i18n.py", +] \ No newline at end of file