Skip to content

Module attribute assignment causes attr-defined #19067

Open
@tucked

Description

@tucked

logging.getLevelNamesMapping was added in Python 3.11.
This compatibility code enables its use in earlier versions:

#sscce.py

import logging
import typing as t


def _get_level_names_mapping() -> t.Dict[str, int]:
    return {
        "CRITICAL": logging.CRITICAL,
        "FATAL": logging.FATAL,
        "ERROR": logging.ERROR,
        "WARN": logging.WARNING,
        "WARNING": logging.WARNING,
        "INFO": logging.INFO,
        "DEBUG": logging.DEBUG,
        "NOTSET": logging.NOTSET,
    }


logging.getLevelNamesMapping = getattr(
    logging, "getLevelNamesMapping", _get_level_names_mapping
)  # compat: python < 3.11

If we type-check that with the latest Mypy (1.15.0) in Python 3.11 (or later), there are no errors:

$ mypy sscce.py
Success: no issues found in 1 source file

If we check it in Python 3.10 (or earlier), we get an attr-defined error:

$ mypy sscce.py
sscce.py:18: error: Module has no attribute "getLevelNamesMapping"  [attr-defined]
Found 1 error in 1 file (checked 1 source file)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongpendingIssues that may be closed

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions