Skip to content

Commit 4651635

Browse files
committed
Tighten annotation of logging.getLevelName
To better reflect the implementation's behaviour, python#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 python#1842 (comment), but I don't want to relitigate that here. I've at least left a comment.
1 parent e285e52 commit 4651635

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

stdlib/logging/__init__.pyi

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,13 @@ 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+
# The str -> int case is considered a mistake, but retained for backward
578+
# compatibility. See
579+
# https://docs.python.org/3/library/logging.html#logging.getLevelName.
580+
@overload
581+
def getLevelName(level: str) -> Any: ...
576582

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

0 commit comments

Comments
 (0)