Skip to content

Commit 97ccd89

Browse files
authored
Tighten annotation of logging.getLevelName (#12088)
To better reflect the implementation's behaviour, #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.
1 parent 6ddf464 commit 97ccd89

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

stdlib/logging/__init__.pyi

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,14 @@ fatal = critical
572572

573573
def disable(level: int = 50) -> None: ...
574574
def addLevelName(level: int, levelName: str) -> None: ...
575-
def getLevelName(level: _Level) -> Any: ...
575+
@overload
576+
def getLevelName(level: int) -> str: ...
577+
578+
# The str -> int case is considered a mistake, but retained for backward
579+
# compatibility. See
580+
# https://docs.python.org/3/library/logging.html#logging.getLevelName.
581+
@overload
582+
def getLevelName(level: str) -> Any: ...
576583

577584
if sys.version_info >= (3, 11):
578585
def getLevelNamesMapping() -> dict[str, int]: ...

0 commit comments

Comments
 (0)