Skip to content

mypy complains that logging.Formater does not have a converter attr  #720

@jaloren

Description

@jaloren

I have python 2.7 code with a function that creates a logger. I put a type annotation in the comment, where the return type is logging.Logger. I use mypy 0.4.6 on python 3.5.2.

When i run the type checker, i get this error.

mypy --py2 --silent-imports --fast-parser -i script.py
script.py: note: In function "get_logger":
script.py:44: error: "Formatter" has no attribute "converter"

However, this is wrong because the log formatter does in fact have a converter attribute.

Code example below.

   
   import logging
   import time

   def get_logger(loglevel=logging.INFO):
    #type: (...) -> logging.Logger
    global_logger = logging.getLogger("script")
    formatter = logging.Formatter('%(asctime)s UTC - %(levelname)s - %(message)s - func: %(funcName)s - lineno: %(lineno)d')
    formatter.converter = time.gmtime
    ch = logging.StreamHandler()
    ch.setLevel(loglevel)
    ch.setFormatter(formatter)
    global_logger.addHandler(ch)
    global_logger.setLevel(loglevel)
    return global_logger

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