From 465163531f9af1c6d42457b9831ea9bde35e909d Mon Sep 17 00:00:00 2001 From: Colin Watson Date: Mon, 3 Jun 2024 12:59:48 +0100 Subject: [PATCH 1/2] Tighten annotation of logging.getLevelName 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. This is all arguably a bit unfortunate in light of https://github.com/python/typeshed/issues/1842#issuecomment-360929192, but I don't want to relitigate that here. I've at least left a comment. --- stdlib/logging/__init__.pyi | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/stdlib/logging/__init__.pyi b/stdlib/logging/__init__.pyi index 8b19444a5d01..28eb36abe305 100644 --- a/stdlib/logging/__init__.pyi +++ b/stdlib/logging/__init__.pyi @@ -572,7 +572,13 @@ 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]: ... From a9605cfe8820cc40af47221e05483b5702b6fd9d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 3 Jun 2024 12:02:27 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks --- stdlib/logging/__init__.pyi | 1 + 1 file changed, 1 insertion(+) diff --git a/stdlib/logging/__init__.pyi b/stdlib/logging/__init__.pyi index 28eb36abe305..f25abff837b7 100644 --- a/stdlib/logging/__init__.pyi +++ b/stdlib/logging/__init__.pyi @@ -574,6 +574,7 @@ def disable(level: int = 50) -> None: ... def addLevelName(level: int, levelName: str) -> None: ... @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.