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 <anandkarancompsci@gmail.com>
This commit is contained in:
Karan Anand
2026-02-15 15:31:22 -05:00
parent 8d59cc726c
commit 5f5b2dc2c6
3 changed files with 15 additions and 6 deletions

View File

@@ -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]

View File

@@ -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 = {}

View File

@@ -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 = []
files = [
"ironicclient/common/i18n.py",
]