Skip to content

Annotation for getLevelName in logging does not accept str but the code does #1842

Closed
@gabbard

Description

@gabbard

Currently for getLevelName in logging there is

def getLevelName(lvl: int) -> str: ...

However the code for it (Python 3.6.3) accepts a string argument as well:

def getLevelName(level):
    """
    Return the textual representation of logging level 'level'.

    If the level is one of the predefined levels (CRITICAL, ERROR, WARNING,
    INFO, DEBUG) then you get the corresponding string. If you have
    associated levels with names using addLevelName then the name you have
    associated with 'level' is returned.

    If a numeric value corresponding to one of the defined levels is passed
    in, the corresponding string representation is returned.

    Otherwise, the string "Level %s" % level is returned.
    """
    # See Issues #22386, #27937 and #29220 for why it's this way
    result = _levelToName.get(level)
    if result is not None:
        return result
    result = _nameToLevel.get(level)
    if result is not None:
        return result
    return "Level %s" % level

It is not clear to me from the method's docstring that the behavior of the code with respect to strings is guaranteed, though, so perhaps leaving it as int is safer.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions