From 97ccd8985a3b8fd7b888c0653e92f296db6fd56a Mon Sep 17 00:00:00 2001 From: Colin Watson Date: Tue, 4 Jun 2024 09:16:53 +0100 Subject: [PATCH] Tighten annotation of logging.getLevelName (#12088) To better reflect the implementation's behaviour, https://github.com/python/typeshed/pull/2730 changed `logging.getLevelName` to accept `int | str` and return `Any` (the latter due to the need to avoid union return types). However, this isn't ideal if you're passing in an `int`, in which case the implementation always returns a `str`. Add overloads for this. --- stdlib/logging/__init__.pyi | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/stdlib/logging/__init__.pyi b/stdlib/logging/__init__.pyi index 8b19444a5d01..f25abff837b7 100644 --- a/stdlib/logging/__init__.pyi +++ b/stdlib/logging/__init__.pyi @@ -572,7 +572,14 @@ fatal = critical def disable(level: int = 50) -> None: ... def addLevelName(level: int, levelName: str) -> None: ... -def getLevelName(level: _Level) -> Any: ... +@overload +def getLevelName(level: int) -> str: ... + +# The str -> int case is considered a mistake, but retained for backward +# compatibility. See +# https://docs.python.org/3/library/logging.html#logging.getLevelName. +@overload +def getLevelName(level: str) -> Any: ... if sys.version_info >= (3, 11): def getLevelNamesMapping() -> dict[str, int]: ...